如何将 ORM 连接到 IoC 容器?
一直在搜索很多关于IoC/DI开发的信息,但是我还没有找到太多关于如何将ORM集成到IoC/DI开发中的信息。
- IoC 应该了解 ORM 吗?或者反之亦然?
- 映射“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.
- Should the IoC know about the ORM? Or vice versa?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要 Person 类和 PersonModel?
我将使用 IoC 容器来解析返回正确 IPerson 的正确存储库。
例如:
您的所有 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:
All of your ORM logic would be encapsulated in your repository.
More on the repository pattern Here
ORM 有一个会话(或任何名称)来加载实体。会话确实可以由 DI 框架注入。
因此,在您的示例中,使用
Hibernate
您可能会得到如下结果:ORM 中的实体映射独立于 IoC 配置。
ORM 中的实体以最简单的方式保存数据。您的实体通常不需要有接口。
可以通过多种方式完成映射。这个想法是将类中的属性映射到数据库中的列。以下是
hibernate
配置的摘录。这只是一般想法(如有错误,敬请谅解)。当然,详细信息取决于您使用的 IoC/DI,以及 ORM。
编辑
这就是我在评论中的意思:
您不使用 IoC 来加载实体,而是使用会话/存储库。
ORM 支持多态实体。因此,加载的
person
对象可以具有具体类型Person1、Person2
等。获取
person
的presenter
>,你使用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: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.
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
.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:
You don't use IoC to load entities, you use the session/repository.
ORM support polymoprhic entities. So the
person
object that is loaded can have concrete typePerson1, Person2
etc.To obtain the
presenter
for theperson
, you useThat's plain OO.
For testing, you could subclass
PersonX
withPersonXTest
and overrideGetPresenter
to return mock presenter. Then you change the config of the ORM to userPersonXTest
instead ofPersonX
for testing. No need of DI then.