csharpnumpty
csharpnumpty

Reputation: 3723

Named singleton instance in StructureMap with multiple nHibernate Session factories (StructureMap 2.5.4)

I have the exact identical scenario as described in Named singleton instance in StructureMap (Multiple nHibernate session factories)

If I implement this using StructureMap 2.6.2 - great!

However, I have to currently use StructureMap 2.5.4 due to various factors involved in legacy projects not being able to use up-to-date .NET framework versions as well as a few other reasons. Assume for this question that I cannot upgrade for the forseeable future.

2.5.4 is obviously quite different in syntax to 2.6.2 and I can't work out how to implement the same thing using it's API.

In particular, it's this stuff

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"MyName")).Named("MySessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("MyName");

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"My2ndName")).Named("My2ndSessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("My2ndName");

...with a method called CreateSessionFactory(string) that creates the relevant configuration.

The "Add" and subsequent "GetInstance" are invalid and need to be refactored to work with 2.5.4 - I'm just too dumb to work out how, or if indeed you can achieve the same thing.

Cheers

Upvotes: 0

Views: 1036

Answers (1)

PHeiberg
PHeiberg

Reputation: 29811

To my rememberance your syntax should work in 2.5.4. For 2.5.3 the following syntax should work:

c.ForRequestedType<ISessionFactory>().CacheBy(InstanceScope.Singleton).
   AddInstances(
     x =>
     {
       x.ConstructedBy(() => CreateSessionFactory("MyName"))
         .WithName("MyName");
       x.ConstructedBy(() => CreateSessionFactory("My2ndName"))
         .WithName("My2ndName");
     });

Upvotes: 3

Related Questions