简体   繁体   English

在Castle Windsor中向多个服务注册多个组件

[英]Register multiple components with multiple services in Castle Windsor

I have some services and the interfaces for them in two separated assemblies, one for the interfaces and another for the implementations. 我在两个单独的程序集中有一些服务和用于它们的接口,一个用于接口,另一个用于实现。

I'm trying to register them using Castle Windsor. 我正在尝试使用温莎城堡进行注册。 It works perfectly using something like this: 它可以像下面这样完美地工作:

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

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

The thing here is... is possible to register all components at once? 这里的事情是...可以一次注册所有组件吗? Without having to register them one by one. 无需一一注册。 I have seen that is possible to register multiple interfaces to a simple component, o multiple implementations for a single interface, but I haven't seen if it's possible to register multiple interfaces from one assembly with multiple implementations from another assembly. 我已经看到有可能将多个接口注册到一个简单的组件,或者为单个接口提供多个实现,但是我还没有看到是否有可能在一个程序集中注册多个接口并在另一个程序集中注册多个实现。

I don't know if this has any relevance, but all interfaces have the same base(IService in this case). 我不知道这是否有任何相关性,但是所有接口都具有相同的基础(在这种情况下为IService)。

Thanks a lot. 非常感谢。

What you are looking for is Registering components by conventions . 您正在寻找的是按约定注册组件 Basically what you do is that you declare what implementations Castle should consider, and what services it should associate those implementations with. 基本上,您要做的是声明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()
);

There are many other options for convention-based registration, the best way is to take a longer look at the linked page. 基于约定的注册还有许多其他选项,最好的方法是更长久地浏览链接的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM