在 CaSTLe Windsor 中,当我想使用工厂方法时,我会像这样简单地创建容器:
container.Register(Component.For<ISomeClass>().UsingFactoryMethod(() => { return new SomeClass(); } ));
如何使用 caliburn.micro 集成 IoC 容器实现同样的想法?我已经阅读了文档,但我仍然无法弄清楚。
最佳答案
每个 SimpleContainer
方法只是使用 RegisterHandler
的便捷方法.您可以在 the docs 中看到此评论:
Note: All of the above registration methods actually use Handles under the covers.
或者简单地阅读the source .此方法允许您传递一个接受容器并返回对象实例 ( Func<SimpleContainer, object>
) 的函数。
所以你可以只使用RegisterHandler
或通用 Handler
注册工厂的扩展方法:
container.Handler<ISomeClass>(container => new SomeClass());
或者:
container.RegisterHandler(typeof(ISomeClass), null, container => new SomeClass());
关于c# - Caliburn Micro IoC/DI 工厂方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34836886/