Ninject + NHibernate具有两个或多个数据库 [英] Ninject + NHibernate with two or more databases

查看:143
本文介绍了Ninject + NHibernate具有两个或多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在NinjectModule中声明了我的Ninject绑定,如下所示:

I have declared my Ninject bindings in a NinjectModule like this:

public override void Load()
{
    Bind<ISessionFactory>().ToMethod(c => SessionFactory1.SessionFactory).InSingletonScope().Named("d1");
    Bind<ISessionFactory>().ToMethod(c => SessionFactory2.SessionFactory).InSingletonScope().Named("d2");

    Bind<ISession>().ToMethod(c => c.Kernel.Get<ISessionFactory>("d1").OpenSession()).Named("d1");
    Bind<ISession>().ToMethod(c => c.Kernel.Get<ISessionFactory>("d2").OpenSession()).Named("d2");

    Bind(typeof(IReadOnlyRepository<,>)).To(typeof(Repository<,>)).Named("d1").WithConstructorArgument("session", c => c.Kernel.Get<ISession>("d1"));
    Bind(typeof(IReadOnlyRepository<,>)).To(typeof(Repository<,>)).Named("d2").WithConstructorArgument("session", c => c.Kernel.Get<ISession>("d2"));
}

如果要运行一个解析IReadonlyRepository的程序,我会从Ninject获得一个异常(ActivationException:激活Repository {ulong,Workflow}时出错),有人可以在我的绑定配置中发现该错误吗?

If a run a to resolved a IReadonlyRepository I get an exception from Ninject (ActivationException: Error activating Repository{ulong, Workflow}) can anyone spot the error in my binding configuration?

IReadOnlyRepository repository1 = kernel.Get<Repository<UInt64, Workflow>>("d1");

推荐答案

尝试使用以下其他名称:

Try with different name like these:

public override void Load() 
        { 
            Bind<ISessionFactory>().ToMethod(c => SessionFactory1.SessionFactory).InSingletonScope().Named("d1"); 
            Bind<ISessionFactory>().ToMethod(c => SessionFactory2.SessionFactory).InSingletonScope().Named("d2"); 

            Bind<ISession>().ToMethod(c => c.Kernel.Get<ISessionFactory>("d1").OpenSession()).Named("s1"); 
            Bind<ISession>().ToMethod(c => c.Kernel.Get<ISessionFactory>("d2").OpenSession()).Named("s2"); 

            Bind(typeof(IReadOnlyRepository<,>)).To(typeof(Repository<,>)).WithConstructorArgument("session", c => c.Kernel.Get<ISession>("s1")).Named("r1"); 
            Bind(typeof(IReadOnlyRepository<,>)).To(typeof(Repository<,>)).WithConstructorArgument("session", c => c.Kernel.Get<ISession>("s2")).Named("r2"); 
        } 

IReadOnlyRepository repository1 = kernel.Get<IReadOnlyRepository>("r1");

这篇关于Ninject + NHibernate具有两个或多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆