简体   繁体   English

Castle Windsor:从一个程序集中自动注册类型,从而实现另一个程序集的接口

[英]Castle Windsor: Auto-register types from one assembly that implement interfaces from another

I use Castle Windsor as my IoC container . 我使用Castle Windsor作为我的IoC容器 I have an application that has a structure similar to the following: 我有一个具有类似于以下结构的应用程序:

  • MyApp.Services.dll MyApp.Services.dll
    • IEmployeeService
    • IContractHoursService
    • ...
  • MyApp.ServicesImpl.dll MyApp.ServicesImpl.dll
    • EmployeeService : MyApp.Services.IEmployeeService
    • ContractHoursService : MyApp.Services.IContractHoursService
    • ...

I use the XML configuration at the moment, and every time I add a new IService/Service pair, I have to add a new component to the XML configuration file. 我目前使用XML配置 ,每次添加新的IService / Service对时,我都需要在XML配置文件中添加一个新组件。 I want to switch all this over to the fluent registration API but haven't worked out exactly the right recipe to do what I want yet. 我想将所有这些切换到流畅的注册API,但还没有找到完全正确的配方来做我想要的。

Can anyone help? 有人可以帮忙吗? The lifestyles will all be singleton . 生活方式都是singleton

Many thanks in advance. 提前谢谢了。

With AllTypes you can easily do this: 使用AllTypes您可以轻松地执行此操作:

From http://stw.castleproject.org/(S(nppam045y0sdncmbazr1ob55))/Windsor.Registering-components-by-conventions.ashx : 来自http://stw.castleproject.org/(S(nppam045y0sdncmbazr1ob55))/Windsor.Registering-components-by-conventions.ashx

Registering components one-by-one can be very repetitive job. 逐个注册组件可能是非常重复的工作。 Also remembering to register each new type you add can quickly lead to frustration. 还记得注册你添加的每个新类型很快就会导致沮丧。 Fortunately, you don't have to do it, at least always. 幸运的是,至少你总是不必这样做。 By using AllTypes entry class you can perform group registration of types based on some specified characteristics you specify. 通过使用AllTypes条目类,您可以根据指定的某些指定特征执行类型的组注册。

I think your registration would look like: 我认为你的注册看起来像:

AllTypes.FromAssembly(typeof(EmployeeService).Assembly)
    .BasedOn<IEmployeeService>()
    .LifeStyle.Singleton

If you implement a base type, like IService on your interfaces, you can register them all at once using the following construct: 如果在接口上实现基本类型(如IService ,则可以使用以下构造一次性注册它们:

AllTypes.FromAssembly(typeof(EmployeeService).Assembly)
    .BasedOn<IService>()
    .WithService.FromInterface()
    .LifeStyle.Singleton

For more examples, see the article. 有关更多示例,请参阅文章。 This has a very good description on what the possibilities are. 这对可能性有很好的描述。

I took Pieter's answer forward just a little bit (the key being, as he suggested, AllTypes ) and have come up with this: 我把Pieter的答案向前推了一点(正如他所建议的那样,关键是AllTypes ),并提出了这个问题:

// Windsor 2.x
container.Register(
    AllTypes.FromAssemblyNamed("MyApp.ServicesImpl")
    .Where(type => type.IsPublic)
    .WithService.FirstInterface()
    );

This goes through all public classes in the MyApp.ServicesImpl.dll assembly and registers each in the container using the first interface it implements. 这将遍历MyApp.ServicesImpl.dll程序MyApp.ServicesImpl.dll所有公共类,并使用它实现的第一个接口在容器中注册每个类。 Because I want all the classes in the services assembly, I need no marker interface. 因为我想要服务程序集中的所有类,所以我不需要标记接口。

The above works for an old version of Windsor. 以上适用于旧版Windsor。 The current Castle Windsor documentation for registering components for the latest version suggests the following: 用于注册最新版本组件的当前Castle Windsor文档建议如下:

// Windsor latest
container.Register(
    AllTypes.FromAssemblyNamed("MyApp.ServicesImpl")
    .Where(type => type.IsPublic) // Filtering on public isn't really necessary (see comments) but you could put additional filtering here
    .WithService.DefaultInterface()
    );

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

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