在Castle Windsor注册多个具有多种服务的组件

时间:2014-04-24 07:56:40

标签: c# castle-windsor

我在两个独立的程序集中有一些服务和接口,一个用于接口,另一个用于实现。

我试图使用Castle Windsor注册它们。它完美地使用这样的东西:

 container.Register(Component
.For(IService1)
.ImplementedBy(Service1)
.LifeStyle.Transient());

 container.Register(Component
.For(IService2)
.ImplementedBy(Service2)
.LifeStyle.Transient());

这里的东西是......可以一次注册所有组件吗?无需逐个注册。我已经看到可以将多个接口注册到一个简单的组件,或者单个接口的多个实现,但我还没有看到是否可以从一个组件注册多个接口,而另一个组件具有多个实现组装

我不知道这是否有任何相关性,但所有接口都具有相同的基础(在这种情况下为IService)。

非常感谢。

1 个答案:

答案 0 :(得分:1)

您要找的是Registering components by conventions。基本上你所做的是你声明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()
);

基于会议的注册还有很多其他选项,最好的方法是仔细查看链接页面。