如何实现委托工厂?
Autofac 的文档有一个有趣的页面,描述了它自动生成 委托工厂。它还强烈建议您可以在没有 Autofac 的情况下通过手写获得类似的结果。
我正在使用 Unity 进行 IoC,并且希望避免将容器传递给需要创建其他对象的对象,那么如何在没有 Autofac 的情况下编写委托工厂呢?
The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories. It also strongly suggests that you can get similar results without Autofac by writing them by hand.
I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,到目前为止我从未使用过 Unity,所以我的答案很模糊。
原理很简单。您定义一些代表工厂的委托。然后,您创建一个“工厂”类,其中具有与委托匹配的公共方法。这个类知道容器。现在您注册委托并将该类设置为实现。然后你可以只注入委托。当您调用注入的委托时,将调用工厂类,它知道容器并向容器请求新实例。
首先,定义工厂代表。
您创建一个通用工厂:
现在您注册委托,如下所示:
现在您只需将“Provider”而不是容器注入到其他组件中。
Well I've never used Unity so far, so my answer is quite vague.
The principal is simple. You define some delegates which represent factories. Then you create a ‘factory’-class which has public methods which matches the delegates. This class knows the container. Now you register the delegate and set that class as implementation. Then you can inject only the delegate. When you call the injected delegate, the factory-class is called, which knows the container and asks the container for a new instance.
First you define your factory-delegates.
The you create a generic factory:
Now what you register the delegate, something like this:
Now other you inject to other components just the ‘Provider’ instead of the container.