Asp.net mvc 如何确定Ninject应该绑定哪个会话

Asp.net mvc 如何确定Ninject应该绑定哪个会话,asp.net-mvc,nhibernate,ninject,Asp.net Mvc,Nhibernate,Ninject,首次使用:Asp.NETMVC、NHibernate(FNH)、DI使用Ninject。我能够使用一个数据库使所有内容都工作,但现在我尝试使用两个数据库(为了eg,使用DB1和DB2)。我有一个SessionFactory字典,它由数据库标识符键入 我不知道如何根据构造函数注入请求选择正确的会话。我见过这个,但我没有让它工作。 publicproductcontroller(DB1.Model.IRepository prodRepo,DB2.Model.IRepository acctRep

首次使用:Asp.NETMVC、NHibernate(FNH)、DI使用Ninject。我能够使用一个数据库使所有内容都工作,但现在我尝试使用两个数据库(为了eg,使用DB1和DB2)。我有一个SessionFactory字典,它由数据库标识符键入

我不知道如何根据构造函数注入请求选择正确的会话。我见过这个,但我没有让它工作。

publicproductcontroller(DB1.Model.IRepository prodRepo,DB2.Model.IRepository acctRepo)
{
[...]
}
NinjectWebCommon.cs代码段

私有静态无效注册服务(IKernel内核)
{            
kernel.Bind().ToMethod(ctx=>NHibernateSessionModule.Provider.GetCurrentSession()).InRequestScope();
Bind(typeof(DB1.Model.IRepository)).To(typeof(NHibernateRepository));
Bind(typeof(DB2.Model.IRepository)).To(typeof(NHibernateRepository));
}
NHibernateSessionModule.cs:UOW是否通过开始/结束请求

公共类NHibernateSessionModule:IHttpModule
{        
公共静态ISessionFactoryProvider提供程序=新的MultipleSessionFactoryProvider();
public void Dispose(){}
公共void Init(HttpApplication上下文)
{
context.BeginRequest+=BeginRequest;
context.EndRequest+=EndRequest;
}
public void BeginRequest(对象发送方,事件参数e)
{
Provider.BindNew();
}
public void EndRequest(对象发送方,事件参数e)
{
Provider.Unbind();
}
}
MultipleSessionFactoryProvider.cs:我不太确定我在这里做的每件事都是正确的

公共类MultipleSessionFactoryProvider:ISessionFactoryProvider
{
公共字典会话工厂{get;private set;}
public static Func InitSessionFactories=GetFactories;
public MultipleSessionFactoryProvider():此(initSessionFactorys())
{
}
公共MultipleSessionFactoryProvider(字典工厂)
{
会话工厂=工厂;
}
公共静态字典getFactorys()
{
字典ret=新字典();
字典连接字符串=新字典();
ConnectionString.Add(ConfigurationManager.ConnectionString[“DB1”]。名称,ConfigurationManager.ConnectionString[“DB1”]。ConnectionString.ToString());
ConnectionString.Add(ConfigurationManager.ConnectionString[“DB2”].Name,ConfigurationManager.ConnectionString[“DB2”].ConnectionString.ToString());
foreach(连接字符串中的KeyValuePair对)
{
//更好的映射方法?
ISessionFactory=fluntly.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(pair.Value))
.Mappings(cfg=>cfg.FluentMappings.Conventions.Setup(x=>x.Add(AutoImport.Never()))
.AddFromAssemblyOf())
.BuildConfiguration()
.CurrentSessionContext().BuildSessionFactory();
ret.Add(成对密钥,工厂);
}
返回ret;
}
新公共图书馆()
{
foreach(会话工厂中的KeyValuePair工厂)
{
绑定(OpenSession(factory.Key));
}            
}
公共无效绑定(ISession会话)
{
CurrentSessionContext.Bind(会话);
}
公共无效解除绑定()
{            
foreach(会话工厂中的KeyValuePair工厂)
{
if(CurrentSessionContext.HasBind(factory.Value))
{
var sess=CurrentSessionContext.Unbind(工厂值);
sess.Dispose();
}
}
}
公共ISession OpenSession(字符串factoryId)
{
返回SessionFactories[factoryId].OpenSession();
}
公共ISession GetCurrentSession()
{

string factoryId=GetIdentifier();//会话需要两个绑定,每个数据库一个

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

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

请不要简单地将“ASP.NET MVC”称为“MVC”。一个是框架,另一个是独立于语言的设计模式。这就像将IE称为“互联网”谢谢,这正是我需要的!