How to inject different NHibernate Sessions (multi-db) to same repository with Controller controlling which sessions with Ninject

纵饮孤独 提交于 2019-12-10 18:37:10

问题


Using: ASP.NET MVC3 Ninject 2 Fluent nHibernate

I have 2 databases (DB1 & DB2). I have one base repository class (Repository) and many Controllers (Controller1, Controller2).

public MyController(IRepository<SomeModelFromDB1> someModelFromDB1Repository, IRepository<SomeModelFromDB2> someModelFromDB2Repository)
{
    [...]
}

public class Repository<T> : IRepository<T> where T : Entity
{
    private readonly ISession _session;

    public Repository(ISessionFactory sessionFactory)
    {
        _session = sessionFactory.OpenSession();
    }
}

public class DB1SessionFactory : ISessionFactory
{
    private readonly NHibernate.ISessionFactory _sessionFactory;

    private ISession _session;

    public DB1SessionFactory()
    {
        [...]
    }
}

public class DB2SessionFactory : ISessionFactory
{
    private readonly NHibernate.ISessionFactory _sessionFactory;

    private ISession _session;

    public DB2SessionFactory()
    {
        [...]
    }
}

Now when I create MyController. I want my repository injected, but that repository should use DB1 (or DB2, depending on the model) SessionFactory.

I cannot figure how to properly inject it all... When I had only a single SessionFactory (DB1), here what I had with NINJECT:

kernel.Bind<ISessionFactory>().To<DB1SessionFactory>()
            .InRequestScope();

kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));

Edit:

The best would be that the right session is injected in the repository depending on the model. Since some models are from DB1 and others from DB2, the choice should be dependent from it. It would be great also if the controller/view developer not have to bother with anything (like having [Named] in front of the repository) but if it's what it takes. Even with [Named], I couldn't figure how to inject the right session in the repository based on the [Named] repository in the controller...


回答1:


First of all, you should define the session factory in singelton scope and have the session in request scope.

Do the configuration like this:

.Bind<ISessionFactory>().To<DB1SessionFactory>().Named("DB1")
     .InSingletonScope();

.Bind<ISessionFactory>().To<DB2SessionFactory>().Named("DB2")
     .InSingletonScope();

private bool IsOnDB(IRequest request, string dbName)
{
    var repositoryType = request.ParentRequest.Service;
    var modelType = repositoryType.GetGenericArguments()[0];
    var databaseName = this.GetDatabaseForModel(modelType);

    return databaseName == dbName;
}

.Bind<ISession>()
    .ToMethod(ctx => ctx.Kernel.Get<ISessionProvider>("DB1").OpenSession())
    .When(r => this.IsOnDb(r, "DB1"))
    .InRequestScope();
.Bind<ISession>()
    .ToMethod(ctx => ctx.Kernel.Get<ISessionProvider>("DB2").OpenSession())
    .When(r => this.IsOnDb(r, "DB2"))
    .InRequestScope();


来源:https://stackoverflow.com/questions/8262917/how-to-inject-different-nhibernate-sessions-multi-db-to-same-repository-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!