【问题标题】:How do I set the generic parameter to only specific type parameters in this Windsor installer using the fluent API?如何使用 fluent API 在此 Windsor 安装程序中将泛型参数设置为仅特定类型参数?
【发布时间】:2010-11-17 11:23:25
【问题描述】:

我目前有这个安装程序:

class DemiInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, 
        IConfigurationStore store)
    {
        List<Type> types = new List<Type>
        {
            typeof(ServicePlan),
            typeof(AccountGetService),
            typeof(ServiceOrder),
            typeof(WorkRosterHistory),
            typeof(WorkRoster),
            typeof(SmallHoursAmount),
            typeof(Nurse),
            typeof(ServicePlanHistory),
            typeof(ServicePlanLine),
            typeof(ServicePlanLineHistory),
            typeof(AccountGetServiceAbsence),
            typeof(NurseAbsence),
            typeof(Holiday)
        };

        foreach (var type in types)
        {
            container.Register(
                Component
                .For(typeof(IRepository<>)
                    .MakeGenericType(type))
                .ImplementedBy(typeof(ARRepository<>)
                    .MakeGenericType(type)));
        }
    }
}

Windsor 的 Fluent API 中是否有一个函数可以实现这种行为,而不是遍历列表?
我可以根据泛型类型进行其他类型的过滤吗?

【问题讨论】:

  • 为什么?实际情况如何?你想达到什么目标?
  • @Krzysztof Koźmic:我试图只注册一些类型的存储库,因为我不需要全部。
  • @the_drow:如果你注册所有这些会发生什么?这会容易得多,而且不会造成任何伤害。
  • @Mauricio Scheffer:我正在运行一些必须尽快初始化的东西,但我有需要解决的服务。
  • @the_drow:它会注册多少个存储库才能对性能产生重大影响?真正需要多长时间?你量过吗?

标签: c# .net castle-windsor ioc-container


【解决方案1】:

只需注册

container.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(ARRepository<>)));

【讨论】:

  • 这样做了,它注册了程序集中的所有 ARRepsitories。我只需要一些。
  • @the_drow:这单独显式注册所有存储库,它注册了开放的通用服务。它与 roelofb 的解决方案不同。
  • 所以这可以让我只解决我需要的问题,对吧?您能为我解释一下 roelofb 的解决方案吗?
【解决方案2】:
container.Register(AllTypes.FromThisAssembly()
                            .BasedOn(typeof(IRepository<>))
                            .WithService.FromInterface());

【讨论】:

  • 你为什么不使用ImplementedBy()?它如何知道解析为 ARRepository?我怎样才能不在容器中包含没有 Foo、Bar、Baz... 的泛型类型参数的所有内容?
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
相关资源
最近更新 更多