使用IoC容器时,如何将动态数据/对象传递给类?
我有一个带有以下构造函数的 Order 类
public Order(IProduct product, short count)
{
this._product = product;
this._count = count;
}
,我正在尝试设置 Unity IoC 容器,显然要构造订单对象,我需要知道计数和产品,但这些是在运行时确定的;计数可以是任何值,产品可以是任何产品,例如胡萝卜、甜瓜等。
那么如何为此应用 IoC?
我认为的一种方法是我的构造函数只接受对象依赖项,然后使用新的 Initialize() 方法添加任何其他必需的属性:
public Order (IProduct product)
{
this._product = product;
}
public Initialize(short count)
{
this._count = count;
}
这样,无论谁创建 Order 对象,都必须随后调用 Initialize() 方法,以便其他属性将 IoC 容器无法处理的内容添加到其中。
这是您推荐/使用的方法吗?
I have an Order class with the below constructor
public Order(IProduct product, short count)
{
this._product = product;
this._count = count;
}
I'm trying to setup Unity IoC container and obviously to construct the order object I'd need to know the count and product but these are determined at run time; the count could be any value and product can be any product e.g. Carrot, Melon, etc.
So how to apply IoC for this?
One approach I thought was that my constructor only accepts object dependencies then any other required property to be added using a new Initialize() method:
public Order (IProduct product)
{
this._product = product;
}
public Initialize(short count)
{
this._count = count;
}
in this way whoever creates the Order object, has to call the Initialize() method afterwards so that other properties that can't be handled by the IoC container be added to it.
Is it the approach that you recommend/use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说,这似乎不适合 IoC 容器。据推测,订单可以在应用程序的整个生命周期中根据用户的行为定期实例化不同的产品(和数量),这表明需要为可能创建订单的每个上下文配置一个容器 - 也就是说,每个产品页面。
当您可以很少且尽早地(例如在应用程序启动时)就依赖关系做出决策时,IoC 容器的工作效果最佳。如果决定注入哪个依赖项总是与创建依赖对象同时发生,那么 IoC 容器只会增加不必要的复杂性。
This doesn't seem like an appropriate situation for an IoC container to me. Presumably Orders can be instantiated regularly with different Products (and counts) throughout the life of the application based on the behavior of the user, which suggests a container would need to be configured for each context in which Orders might be created - that is, for each Product page.
IoC containers work best when you can make decisions about dependencies seldom and early on, say at application start-up. If the decision on which dependency to inject always takes place at about the same time as the creation of the dependent object, then an IoC container just adds unnecessary complexity.
使用 Unity,您应该能够设置 ParameterOverride 来传递额外的参数:
好的,或者创建一个工厂类:
使用容器来解析工厂。然后使用工厂来解决您的订单。
With Unity you should be able to set up a ParameterOverride to pass in your extra parameters :
Ok, alternatively create a factory class :
Use the container to resolve the factory. Then use the factory to resolve your orders.
计数可能应该只是订单的属性。计数代表什么?订单行数还是产品数量?我不太确定你打算如何在这里实现 IoC 容器。
像这样的事情:
Count should probably just be a property of Order. What does count represent? The number of order lines or the quantity of product? I'm not quite sure how you plan on implementing and IoC container here.
Something like this:
@斯科特姆
这是我的 aspx 页面中的实际代码:
@scottm
This is the actual code that I have in my aspx page: