Unity InjectionConstructor 何时真正运行?

发布于 2024-11-29 15:51:41 字数 726 浏览 4 评论 0原文

我有以下代码:

IOC.Container.RegisterType<IRepository, GenericRepository>
              ("Customers", new InjectionConstructor(new CustomerEntities()));

我想知道的是,当类型注册发生时,是否会调用 new CustomerEntities() 一次,或者每次 IRepository (名称为“Customers”)解析一个新的 CustomerEntities将被制作。

如果不是后者,那么有没有办法让它更像委托一样工作? (所以每次解析时它都会生成一个新的?)

我找到了这段代码:

IOC.Container.RegisterType<IRepository, GenericRepository>("Customers")
             .Configure<InjectedMembers>()
             .ConfigureInjectionFor<ObjectContext>
              (new InjectionConstructor(new CustomerEntities()));

我不确定这是否可以做到这一点,或者这是否只是我的第一个代码片段的旧方法。

任何建议都会很棒!

I have the following code:

IOC.Container.RegisterType<IRepository, GenericRepository>
              ("Customers", new InjectionConstructor(new CustomerEntities()));

What I am wondering is if the new CustomerEntities() will be called once when the type registration happens OR if every time IRepository (with name "Customers") is resolved a new CustomerEntities will be made.

If it is not the latter, then is there a way to make it work more like a delegate? (So it will make a new one every time it Resolves?)

I found this code:

IOC.Container.RegisterType<IRepository, GenericRepository>("Customers")
             .Configure<InjectedMembers>()
             .ConfigureInjectionFor<ObjectContext>
              (new InjectionConstructor(new CustomerEntities()));

I am not sure if that would do it or if that is just an old way of doing what my first code snippet does.

Any advice would be great!

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

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

发布评论

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

评论(1

唠甜嗑 2024-12-06 15:51:41

您在那里的代码运行一次 - 在注册时创建一个 CustomerEntities 对象,并且该实例作为参数在稍后解析的所有 GenericRepository 对象之间共享。

如果您希望 GenericRepository 的每个实例都有一个单独的 CustomerEntities 实例,那非常简单 - 只需让容器来完成提升即可。在注册中,执行以下操作:

IOC.Container.RegisterType<IRepository, GenericRepository>("Customers", 
    new InjectionConstructor(typeof(CustomerEntities)));

这将告诉容器“解析 IRepository 时,创建 GenericRepository 的实例。调用采用单个 CustomerEntities 参数的构造函数。通过容器解析该参数。

这应该可以解决问题。如果您需要在容器中进行特殊配置来解析 CustomerEntities,只需使用单独的 RegisterType 调用即可完成。

您展示的第二个示例是 Unity 1.0 中过时的 API,请勿使用它,它无法实现。现在,您可以使用 RegisterType 做更多的事情。

The code you have there runs once - a single CustomerEntities object is created at registration time, and that instance is shared as the parameter across all GenericRepository objects resolved later.

If you want a separate instance of CustomerEntities for each instance of GenericRepository, that's pretty easy - just let the container do the lifting. In the registration, do this:

IOC.Container.RegisterType<IRepository, GenericRepository>("Customers", 
    new InjectionConstructor(typeof(CustomerEntities)));

This will tell the container "When resolving IRepository, create an instance of GenericRepository. Call the constructor that takes a single CustomerEntities parameter. Resolve that parameter through the container.

This should do the trick. If you need to do special configuration in the container to resolve CustomerEntities, just do that with a separate RegisterType call.

The second example you showed is the obsolete API from Unity 1.0. Don't use it, it doesn't accomplish anything more than you can do with RegisterType now.

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