如何将 ORM 连接到 IoC 容器?

发布于 2024-08-24 04:11:47 字数 637 浏览 3 评论 0原文

一直在搜索很多关于IoC/DI开发的信息,但是我还没有找到太多关于如何将ORM集成到IoC/DI开发中的信息。

  1. IoC 应该了解 ORM 吗?或者反之亦然?
  2. 映射“ORM 类型”时,IoC 配置映射如何工作?

示例:

public class Person : IPerson
{
    private IPersonModel _model;

    public Person(IPersonModel model)
    {
        _model = model;
    }
}    

IoC.Register<IPerson>.As<Person>();    
IPerson = IoC.Resolve<IPerson>();   

这里的 IoC/ORM 是如何工作的,IPersonModel 可以是我中的任何人 数据库(或其他数据存储)?

我觉得必须有一种方法或类似的方法:

IPerson = IcO.Resolve<IPerson>(() => PersonId = 50);

有人想更一般地解释如何将 IoC 与 ORM 集成吗?

been searching a lot information about IoC/DI development, but I haven't found much information how to integrate an ORM into the IoC/DI development.

  1. Should the IoC know about the ORM? Or vice versa?
  2. How does the IoC configuration mapping work when mapping a "ORM type"?

Example:

public class Person : IPerson
{
    private IPersonModel _model;

    public Person(IPersonModel model)
    {
        _model = model;
    }
}    

IoC.Register<IPerson>.As<Person>();    
IPerson = IoC.Resolve<IPerson>();   

How does the IoC/ORM work here, IPersonModel can be any person in my
data base (or other data store) ?

I feel there must a method or such like this:

IPerson = IcO.Resolve<IPerson>(() => PersonId = 50);

Anyone want to explain more general how to integrate IoC with an ORM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-08-31 04:11:47

为什么需要 Person 类和 PersonModel?

我将使用 IoC 容器来解析返回正确 IPerson 的正确存储库。

例如:

IPersonRepository personRepo = ObjectFactory.GetInstanceOf<IPersonRepository>();
int id = 12;
IPerson person = personRepo.GetBy(id);

您的所有 ORM 逻辑都将封装在您的存储库中。
有关存储库模式的更多信息此处

Why do you need a Person class and a PersonModel?

I would use an IoC container to resolve the proper repository that returns the correct IPerson.

For instance:

IPersonRepository personRepo = ObjectFactory.GetInstanceOf<IPersonRepository>();
int id = 12;
IPerson person = personRepo.GetBy(id);

All of your ORM logic would be encapsulated in your repository.
More on the repository pattern Here

羞稚 2024-08-31 04:11:47

ORM 有一个会话(或任何名称)来加载实体。会话确实可以由 DI 框架注入。

因此,在您的示例中,使用 Hibernate 您可能会得到如下结果:

class Service {

    Session session; // could be injected by DI
    ...
    Person p = (Person) session.load( typeof(Person), 50 );
}

ORM 中的实体映射独立于 IoC 配置。

ORM 中的实体以最简单的方式保存数据。您的实体通常不需要有接口。

public class Person 
{    
  Integer id;
  String name;
  // getter and setter for the values
}

可以通过多种方式完成映射。这个想法是将类中的属性映射到数据库中的列。以下是 hibernate 配置的摘录。

 <class name="Person" 
    table="PERSON" >     

    <id name="id" column="PERSON_ID">
       <generator class="native"/>
    </id>

    <property name="name" column="NAME" />           
    ...
  </class>

这只是一般想法(如有错误,敬请谅解)。当然,详细信息取决于您使用的 IoC/DI,以及 ORM。

编辑

这就是我在评论中的意思:

abstract class Person {
   abstract Presenter GetPresenter();
   ...
}

class Person1 : Person { 
  Presenter GetPresenter() { return new PresenterForPerson1() }
  ...
}

// other subclasses

您不使用 IoC 来加载实体,而是使用会话/存储库。

Person person = session.Load( typeof(Person), 50 );

ORM 支持多态实体。因此,加载的 person 对象可以具有具体类型 Person1、Person2 等。

获取 personpresenter >,你使用

Presenter presenter = person.GetPresenter();

That's plain OO。

为了进行测试,您可以使用 PersonXTest 子类化 PersonX 并重写 GetPresenter 以返回模拟演示者。然后,将 ORM 的配置更改为用户 PersonXTest 而不是 PersonX 进行测试。那就不需要DI了。

ORM have a session (or whatever the name is) to load entities. The session can indeed be injected by a DI framework.

So in your example, with Hibernate you could have something like:

class Service {

    Session session; // could be injected by DI
    ...
    Person p = (Person) session.load( typeof(Person), 50 );
}

The entity mapping in the ORM is independent of the IoC configuration.

Entities in an ORM hold data in the simplest way. You usually don't need to have an interface for your entity.

public class Person 
{    
  Integer id;
  String name;
  // getter and setter for the values
}

The mapping can be done in various way. The idea is that you map the properties in the class to columns in the database. Here would be an excerpt of the configuration with the hibernate.

 <class name="Person" 
    table="PERSON" >     

    <id name="id" column="PERSON_ID">
       <generator class="native"/>
    </id>

    <property name="name" column="NAME" />           
    ...
  </class>

This were just the general ideas (sorry if there are mistakes). The details will depend on the IoC/DI you use, as well as the ORM, of course.

EDIT

Here is what I meant in my comment:

abstract class Person {
   abstract Presenter GetPresenter();
   ...
}

class Person1 : Person { 
  Presenter GetPresenter() { return new PresenterForPerson1() }
  ...
}

// other subclasses

You don't use IoC to load entities, you use the session/repository.

Person person = session.Load( typeof(Person), 50 );

ORM support polymoprhic entities. So the person object that is loaded can have concrete type Person1, Person2 etc.

To obtain the presenter for the person, you use

Presenter presenter = person.GetPresenter();

That's plain OO.

For testing, you could subclass PersonX with PersonXTest and override GetPresenter to return mock presenter. Then you change the config of the ORM to user PersonXTest instead of PersonX for testing. No need of DI then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文