IoC 链中断
我正在构建一个应用程序,它使用抽象工厂模式,允许运行时根据 IHardwareDevice 对识别请求的响应方式来确定要创建的子类。我可能会创建 Hardware1 或 Hardware2。
问题的出现是因为我想在这些 IHardwareDevice 对象中使用状态模式,并且我希望由 IoC 容器创建状态。有没有一种好方法可以做到这一点,而无需直接调用 IoC 容器来解析用于水合 IHardwareDevice 的 State 对象?
或者,我是否以错误的方式思考这个问题?因为我的工厂几乎是这个库的入口点,所以让这个工厂句柄实例化容器以在库中使用是否可以?我计划让客户端应用程序实例化容器,然后使用它来访问它所需的库的任何部分,例如工厂。
我计划使用 Windsor 作为 IoC 容器,但目前处于项目的早期阶段,可以根据需要进行切换。
I am building an application which uses an Abstract Factory pattern to allow runtime determination of which subclass of my IHardwareDevice to create based upon how it responds to a request for identification. I might create Hardware1 or Hardware2.
The problem arises in that I want to use a State pattern in these IHardwareDevice objects and I want to have the State be created by the IoC container. Is there a good way to do this without directly calling the IoC container to resolve the State objects with which to hydrate the IHardwareDevice?
Or, am I thinking about this in the wrong way? Because my Factory is pretty much the entry point into this library, is it okay to have this Factory handle instantiating the container for use in the library? I was planning on having the client application instantiate the container and then use it to gain access to which ever parts of the library it requires, such as the Factory.
I am planning to use Windsor as the IoC container, but am at an early enough stage in the project to switch if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Windsor 有一个非常强大的自动实现抽象工厂的工具 - 类型化工厂工具
它主要是基于约定的,如果默认约定不能满足您的需求,您可以通过提供自定义
ITypedFactoryComponentSelector
来覆盖它。它使您可以访问做出决定时可能需要的所有信息 - 您想要在调用通道中传递哪些内联参数,以及您想要解析哪个组件。
Windsor has a very powerful facility for auto-implementing abstract factories - Typed Factory Facility
It is largely convention based, and if the default convention does not suit your needs, you can override it by providing custom
ITypedFactoryComponentSelector
.It gives you access to all information you may need to make the decision - what inline arguments you want to pass down the invocation lane, and what component you want to resolve.
这是使用 Autofac 2.2 (http://autofac.org) 完成此操作的一种方法:
首先,一个用于区分的枚举状态之间(组成一些可能的值):
然后,状态实现,例如:
接下来,使用相应的枚举键注册每个状态:
最后,硬件设备使用索引来选择状态。
IIndex
的实现是由容器自动提供的:希望这会有所帮助。
缺口
Here's one way it can be done with Autofac 2.2 (http://autofac.org):
First, an enum to discriminate between states (making up some likely values):
Then, the state implementations, like:
Next, register each state with its corresponding enum key:
Finally, the hardware device uses an index to choose states. The implementation of
IIndex
is provided automatically by the container:Hope this helps.
Nick
大多数 IoC 容器(包括 Windsor)都可以选择将显式依赖项(参数)传递给已解析依赖项的构造函数;当您调用
Resolve
方法时。所以是的,您可以将您的 State 对象显式传递给您正在解析的 IHardwareDevice 实例。
Most IoC containers, including Windsor, has the option to pass explicit dependencies (parameters) to the constructors of the resolved dependencies; when you call the
Resolve
method.So yes, you can pass your
State
object explicitly to theIHardwareDevice
instance you are resolving.