![](/img/trans.png)
[英]How can I register multiple generic components with multiple generic services in castle windsor?
[英]Register multiple components with multiple services in Castle Windsor
我在两个单独的程序集中有一些服务和用于它们的接口,一个用于接口,另一个用于实现。
我正在尝试使用温莎城堡进行注册。 它可以像下面这样完美地工作:
container.Register(Component
.For(IService1)
.ImplementedBy(Service1)
.LifeStyle.Transient());
container.Register(Component
.For(IService2)
.ImplementedBy(Service2)
.LifeStyle.Transient());
这里的事情是...可以一次注册所有组件吗? 无需一一注册。 我已经看到有可能将多个接口注册到一个简单的组件,或者为单个接口提供多个实现,但是我还没有看到是否有可能在一个程序集中注册多个接口并在另一个程序集中注册多个实现。
我不知道这是否有任何相关性,但是所有接口都具有相同的基础(在这种情况下为IService)。
非常感谢。
您正在寻找的是按约定注册组件 。 基本上,您要做的是声明Castle应该考虑哪些实现以及与这些实现相关联的服务。
container.Register(
// we want all concrete classes in this assembly
Classes.FromThisAssembly()
// but we filter them to keep only the ones in a specific namespace
.InSameNamespaceAs<RootComponent>()
// we register thoses classes to interfaces that match the classes names
.WithService.DefaultInterfaces()
// setting the lifestyles for all components in this batch
.LifestyleTransient()
);
基于约定的注册还有许多其他选项,最好的方法是更长久地浏览链接的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.