如何识别Session Ninject应绑定的内容

时间:2013-01-18 13:59:35

标签: asp.net-mvc nhibernate ninject

第一次使用:Asp.NET MVC,NHibernate(FNH),DI使用Ninject。我能够使用一个数据库完成所有工作,但现在我正在尝试使用两个数据库(例如DB1和DB2)。我有一个SessionFactory的字典,它由数据库标识符键入。

我无法弄清楚如何根据构造函数注入请求的内容选择正确的Session。我见过这个 How to inject different NHibernate Sessions (multi-db) to same repository with Controller controling which sesions with Ninject,但我没有看到它。

public ProductController(DB1.Model.IRepository<Product> prodRepo, DB2.Model.IRepository<Account> acctRepo)
{
    [...]
}

NinjectWebCommon.cs片段

private static void RegisterServices(IKernel kernel)
{            
    kernel.Bind<ISession>().ToMethod(ctx => NHibernateSessionModule.Provider.GetCurrentSession()).InRequestScope();             
    kernel.Bind(typeof(DB1.Model.IRepository<>)).To(typeof(NHibernateRepository<>));
    kernel.Bind(typeof(DB2.Model.IRepository<>)).To(typeof(NHibernateRepository<>));
}

NHibernateSessionModule.cs:通过开始/结束请求执行UOW

public class NHibernateSessionModule : IHttpModule
{        
    public static ISessionFactoryProvider Provider = new MultipleSessionFactoryProvider();

    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += BeginRequest;
        context.EndRequest += EndRequest;
    }

    public void BeginRequest(object sender, EventArgs e)
    {
        Provider.BindNew();
    }

    public void EndRequest(object sender, EventArgs e)
    {
        Provider.Unbind();
    }
}

MultipleSessionFactoryProvider.cs:我不太确定我在这里做的一切都是正确的。

public class MultipleSessionFactoryProvider : ISessionFactoryProvider
{
    public Dictionary<string, ISessionFactory> SessionFactories { get; private set; }

    public static Func<Dictionary<string,ISessionFactory>> InitSessionFactories = GetFactories;

    public MultipleSessionFactoryProvider() : this(InitSessionFactories())
    {

    }

    public MultipleSessionFactoryProvider(Dictionary<string, ISessionFactory> factories)
    {
        SessionFactories = factories;            
    }

    public static Dictionary<string, ISessionFactory> GetFactories()
    {
        Dictionary<string, ISessionFactory> ret = new Dictionary<string, ISessionFactory>();
        Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
        connectionStrings.Add(ConfigurationManager.ConnectionStrings["DB1"].Name, ConfigurationManager.ConnectionStrings["DB1"].ConnectionString.ToString());
        connectionStrings.Add(ConfigurationManager.ConnectionStrings["DB2"].Name, ConfigurationManager.ConnectionStrings["DB2"].ConnectionString.ToString());

        foreach (KeyValuePair<string, string> pair in connectionStrings)
        {
            //Better way to do the mapping?
            ISessionFactory factory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(pair.Value))
            .Mappings(cfg => cfg.FluentMappings.Conventions.Setup(x => x.Add(AutoImport.Never()))
            .AddFromAssemblyOf<ProductMap>())
            .BuildConfiguration()
            .CurrentSessionContext<WebSessionContext>().BuildSessionFactory();               

            ret.Add(pair.Key, factory);
        }

        return ret;
    }

    public void BindNew()
    {
        foreach (KeyValuePair<string, ISessionFactory> factory in SessionFactories)
        {
            Bind(OpenSession(factory.Key));
        }            
    }

    public void Bind(ISession session)
    {
        CurrentSessionContext.Bind(session);
    }

    public void Unbind()
    {            
        foreach (KeyValuePair<string, ISessionFactory> factory in SessionFactories)
        {
            if (CurrentSessionContext.HasBind(factory.Value))
            {
                var sess = CurrentSessionContext.Unbind(factory.Value);
                sess.Dispose(); 
            }
        }
    }

    public ISession OpenSession(string factoryId)
    {
        return SessionFactories[factoryId].OpenSession();
    }


    public ISession GetCurrentSession()
    {

        string factoryId = GetIdentifier(); //<--- How to implement this

        return SessionFactories[factoryId].GetCurrentSession();
    }

    public String GetIdentifier()
    {
        return "DB1"; //Hardcoded for example

    }

那么,我该如何实现GetIdentifier(),或者改变我的Ninject绑定。我确实看过Ninject命名绑定,但在绑定Session时不明白如何使用它。

public ProductController([Name("DB1")]DB1.Model.IRepository<Product> prodRepo, [Name("DB2")]DB2.Model.IRepository<Account> acctRepo)

由于这是我第一次使用这些技术,如果我正在做任何不实践或可能引起关注的事情,请告诉我。我想继续使用HttpModule。

1 个答案:

答案 0 :(得分:2)

您需要两个会话绑定,每个数据库一个

kernel.Bind<ISession>().ToMethod(ctx => GetSessionForDB1()).WhenInjectedInto(typeof(DB1.Model.IRepository<>)).InRequestScope();

kernel.Bind<ISession>().ToMethod(ctx => GetSessionForDB2()).WhenInjectedInto(typeof(DB2.Model.IRepository<>)).InRequestScope();