将上下文数据传递到依赖关系树

时间:2013-12-04 14:13:09

标签: c# dependency-injection ninject inversion-of-control

我使用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应该在依赖树的某处注入?

0 个答案:

没有答案