Ninject + NHibernate с двумя или более базами данных - PullRequest
0 голосов
/ 22 февраля 2012

Я объявил свои привязки Ninject в модуле NinjectModule следующим образом:

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: Ошибка при активации репозитория {ulong, Workflow}), может кто-нибудь обнаружить ошибку в моей конфигурации привязки?

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

1 Ответ

0 голосов
/ 22 февраля 2012

Попробуйте использовать другое имя, например:

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");
...