如何使用 Windsor 容器注入 Func<T,U>

how to inject Func<T,U> with Windsor container

 public BranchUOW(Func<FoodieTenantContext,BranchRepository> branchRepository, 
        Func<FoodieTenantContext, BranchContactRepository> branchContactRepository,
        FoodieTenantContext ctx, IMapperService mapperService)
        : base(ctx)
    {
        _ctx = ctx;
        _branchRepository = branchRepository(ctx);
        _branchContactRepository = branchContactRepository(ctx);
        _mapperService = mapperService;
    }

BranchRepository 看起来像

 public BranchRepository(FoodieTenantContext ctx)
        : base(ctx)
    {
    }

尝试了以下排列

  container.Register(
          Component.For<Func<FoodieTenantContext, BranchRepository>>()
          .ImplementedBy(typeof(IRepository<BranchRepository>))
          .LifestyleTransient()
         );

        container.Register(
           Component.For<Func<FoodieTenantContext, BranchContactRepository>>()
           .ImplementedBy(typeof(IRepository<BranchContactRepository>))
           .LifestyleTransient()
          );

但无法实例化并抛出以下错误。

Type foodie.common.Repository.IRepository1[[foodie.tenant.EF.Repository.Domain.BranchRepository, foodie.tenant.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] is abstract.\r\n As such, it is not possible to instansiate it as implementation of service 'foodie.common.Repository.IRepository1[[foodie.tenant.EF.Repository.Domain.BranchRepository, foodie.tenant.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'. Did you forget to proxy it?

谢谢。

你可以试试这个:

container.Register(
    Component.For<BranchRepository>(),
    Component.For<Func<FoodieTenantContext, BranchRepository>>()
    .Instance(c => container.Resolve<BranchRepository>(new { ctx = c }))
    .LifestyleTransient()
);

但我更推荐去打字工厂。

https://github.com/castleproject/Windsor/blob/master/docs/typed-factory-facility-interface-based.md