我将Castle Windsor用作我的IoC container。我有一个具有类似于以下结构的应用程序:
IEmployeeService
IContractHoursService
...
EmployeeService : 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:
我认为您的注册应如下所示:
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/