我使用 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
,您可以轻松地做到这一点:
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/