Named single instance in StructureMap (multiple nHibernate session factories)

I have a script in which I have two NBSNate SessionFactorys. I need to register usage with StructureMap. Only Foo needs mySessionFactory sessions.

Like this:

For<ISessionFactory>().Singleton().Use(NHibernateConfiguration.GetDefaultSessionFactory());
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession());
For<ISessionFactory>().Singleton().Use(AnotherNHibernateConfiguration.GetDefaultSessionFactory).Named("mySessionFactory");
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>("mySessionFactory").OpenSession()).Named("mySession");

For<Foo>()
   .Use<Foo>()
   .Ctor<ISession>("session").Is(x => x.TheInstanceNamed("mySession"));

The problem is that mySessionFactory is now used everywhere when I just wanted it to be used in Foo, and should use my unnamed instance otherwise.

What I do?

+3
source share
1 answer

Use Add. , . ( , Use, ), Add .

+5

Source: https://habr.com/ru/post/1796330/


All Articles