温莎城堡:自动注册一个程序集中的类型,该程序集实现另一个程序集中的接口

发布于 2024-09-30 07:06:14 字数 981 浏览 3 评论 0原文

我使用温莎城堡作为我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-10-07 07:06:14

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

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

逐一注册组件可能是一项非常重复的工作。另外,记住注册您添加的每个新类型可能很快就会导致挫败感。幸运的是,您不必这样做,至少始终不必这样做。通过使用AllTypes入口类,您可以根据您指定的某些特定特征执行类型的分组注册。

我认为您的注册如下所示:

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

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

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

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

With AllTypes you can easily do this:

From 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.

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:

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.

南街九尾狐 2024-10-07 07:06:14

我采取了 Pieter 的答案 稍微向前推进一点(关键是,正如他所建议的,AllTypes)并提出了这个:

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

这将遍历 MyApp.ServicesImpl.dll 中的所有公共类 组装并使用其实现的第一个接口将每个组件注册到容器中。因为我想要服务程序集中的所有类,所以我不需要标记接口。

以上适用于旧版本的 Windsor。当前的用于注册组件的Castle Windsor文档< /a> 对于最新版本建议如下:

// 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()
    );

I took Pieter's answer forward just a little bit (the key being, as he suggested, AllTypes) and have come up with this:

// 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. 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:

// 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()
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文