Caliburn Micro IoC/DI 工厂方法

Caliburn Micro IoC/DI FactoryMethod

在 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。此方法允许您传递一个函数,该函数接受一个容器和 returns 一个对象实例 (Func<SimpleContainer, object>)。

因此您可以只使用 RegisterHandler 或通用 Handler 扩展方法来注册您的工厂:

container.Handler<ISomeClass>(container => new SomeClass());

或:

container.RegisterHandler(typeof(ISomeClass), null, container => new SomeClass());