我使用ninject
进行构造函数注入,我的代码类似于:
public class MyRootClass
{
public MyRootClass(
[SetContext("catSession")] IRepository<Cat, int> catRep,
[SetContext("dogSession")] IRepository<Dog, int> dogRep))
{
//set some stuff here
}
}
public class GenericRepository<TEntity, TID> : IRepository<TEntity, TID>
{
public GenericRepository(IUnitOfWork unitOfWork)
{
//set some stuff here
}
}
public class MyUnitOfWork : IUnitOfWork
{
//sessionFactory should be bound to the either a cat session, or dog
//session depending on what the caller (MyRootClass) has currently
//set in the context
public MyUnitOfWork([ReadContext] ISessionFactory sessionFactory)
{
//set some stuff here
}
}
ISessionFactory
将绑定NinjectModule
中绑定的每个会话(cat和dog)。它们是命名绑定。我本质上希望能够要求将IRepository<TEntity, TID>
注入到调用类中,并让调用类指定存储库应该在幕后使用的会话。
如何在MyRootClass
中设置一些上下文变量,用于确定哪个会话Ninject
应该在依赖树的某处注入?