c# - 温莎城堡 : Auto-register types from one assembly that implement interfaces from another

标签 c# castle-windsor ioc-container fluent-interface

我使用 Castle Windsor作为我的IoC container .我有一个具有类似于以下结构的应用程序:

  • MyApp.Services.dll
    • IEmployeeService
    • IContractHoursService
    • ...
  • MyApp.ServicesImpl.dll
    • 员工服务:MyApp.Services.IEmployeeService
    • ContractHoursService : MyApp.Services.IContractHoursService
    • ...

我使用 XML configuration目前,每次我添加一个新的 IService/Service 对时,我都必须向 XML 配置文件添加一个新组件。我想将所有这些切换到 fluent registration API但还没有找到完全正确的方法来做我想做的事情。

有人可以帮忙吗?生活方式都将是 singleton

非常感谢。

最佳答案

使用 AllTypes,您可以轻松地做到这一点:

来自 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.FromAssembly(typeof(EmployeeService).Assembly)
    .BasedOn<IEmployeeService>()
    .LifeStyle.Singleton

如果您在接口(interface)上实现了一个基类型,例如 IService,您可以使用以下结构一次性注册它们:

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

有关更多示例,请参阅文章。这很好地描述了可能性。

关于c# - 温莎城堡 : Auto-register types from one assembly that implement interfaces from another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070375/

相关文章:

c# - .NET JIT 如何优化生成的代码布局?

c# - 我能否将 XAML/WPF 窗口复制到第二个窗口中,例如画中画电视?

c# - Windsor 不解析拦截的组件

c# - CaSTLe Windsor 拦截来自类内部的方法调用

c# - LightInject SignalR 缺少 .RegisterHubs 方法

caSTLe-windsor - CaSTLe Windsor Interceptor for private/protected 方法

c# - 满足 subview 模型依赖关系

c# - 如何在 .Net 中创建 Ntlm Type 1 和 Type 3 消息

c# - Azure 云服务部署无需更改现有配置

c# - CaSTLe Windsor - 将泛型实现解析为基类型