在路上会提供一个
Func<FoodieTenantContext, IRepository<CustomerContactRepository>>
在您的客户群中
public abstract class UnitOfWork : IUnitOfWork
{
public UnitOfWork(FoodieTenantContext context)
{
this.Context = context;
}
// ... rest of the class
}
// usage could be like the following
public class CustomerUOW : UnitOfWork
{
public CustomerService(Func<FoodieTenantContext, IRepository<CustomerRepository>> customerRepo
, Func<FoodieTenantContext, IRepository<CustomerContactRepository>> contactRepo
, FoodieTenantContext context)
: (context)
{
_customerRepo = customerRepo(context);
_contactRepo = contactRepo(context);
}
}
另一个选择是创建RepositoryFactory,但这意味着您必须公开
Context
属性从
IRepository<T>
public class RepositoryFactory
{
IServiceProvider _ioc; // This would be your IoC/DI Container
public RepositoryFactory(IServiceProvider ioc)
{
_ioc = ioc;
}
// Resolve T passing in the provided `FoodieTenantContext` into the constructor
public IRepository<T> CreateRepository<T>(FoodieTenantContext context) =>
_ioc.Resolve<T>(context);
}
另一个解决方案可能是(我最不喜欢的)公开
RepositoryFactory
对于每种类型
在职的
public class RepositoryFactory
{
public IRepository CreateCustomerContactRepository(FoodieTenantContext context) =>
return new CustomerContactRepository(context);
}
在温莎城堡注册func
根据评论,注册
Func<T>
在Castle.Windsor中,您可以尝试以下内容,它是
Anton's answer to Func injecting with Windsor container question.
是的。(我现在无法测试)
Container.Register(
Component.For<Func<FoodieTenantContext, IRepository<CustomerRepository>>>()
.Instance((FoodieTenantContext context) => Container.Resolve<IRepository<CustomerRepository>>(new {context = context}))
)
否则你可以试着玩
AsFactory()
是的。了解更多信息
read Windsor documentation
最后,您可以手动创建工厂或切换支持ioc/di的容器
Func<[TIn1, TIn2, ...], T>
开箱即用,或者至少是本地的。