温莎城堡:自动注册一个程序集中的类型,该程序集实现另一个程序集中的接口
我使用温莎城堡作为我的IoC 容器。我有一个具有类似于以下结构的应用程序:
- MyApp.Services.dll
IEmployeeService
IContractHoursService
...
- MyApp.ServicesImpl.dll
EmployeeService:MyApp.Services.IEmployeeService
ContractHoursService:MyApp.Services.IContractHoursService
...
我使用 XML 配置 此时,每次我添加新的 IService/Service 对时,我必须向 XML 配置文件添加一个新组件。我想将所有这些切换到 Fluent 注册 API 但还没有'还没有找到完全正确的方法来做我想做的事。
有人可以帮忙吗?生活方式都将是单身
。
非常感谢。
I use Castle Windsor as my IoC container. I have an application that has a structure similar to the following:
- MyApp.Services.dll
IEmployeeService
IContractHoursService
...
- 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. 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.
Can anyone help? The lifestyles will all be singleton
.
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
AllTypes
您可以轻松做到这一点:来自 http://stw.castleproject.org/(S(nppam045y0sdncmbazr1ob55))/Windsor.Registering-components-by-conventions.ashx:
我认为您的注册如下所示:
如果您在接口上实现了基本类型,例如
IService
,您可以使用以下构造一次性注册它们:有关更多示例,请参阅文章。这很好地描述了可能性。
With
AllTypes
you can easily do this:From http://stw.castleproject.org/(S(nppam045y0sdncmbazr1ob55))/Windsor.Registering-components-by-conventions.ashx:
I think your registration would look like:
If you implement a base type, like
IService
on your interfaces, you can register them all at once using the following construct:For more examples, see the article. This has a very good description on what the possibilities are.
我采取了 Pieter 的答案 稍微向前推进一点(关键是,正如他所建议的,
AllTypes
)并提出了这个:这将遍历 MyApp.ServicesImpl.dll 中的所有公共类 组装并使用其实现的第一个接口将每个组件注册到容器中。因为我想要服务程序集中的所有类,所以我不需要标记接口。
以上适用于旧版本的 Windsor。当前的用于注册组件的Castle Windsor文档< /a> 对于最新版本建议如下:
I took Pieter's answer forward just a little bit (the key being, as he suggested,
AllTypes
) and have come up with this:This goes through all public classes in the
MyApp.ServicesImpl.dll
assembly and registers each in the container using the first interface it implements. Because I want all the classes in the services assembly, I need no marker interface.The above works for an old version of Windsor. The current Castle Windsor documentation for registering components for the latest version suggests the following: