In my DefaultRegistry I have this configuration:
ForRequestedType<INHUnitOfWork>().CacheBy(InstanceScope.HttpContext)
.TheDefault.Is.OfConcreteType<NHibernateUnitOfWork>();
At some point in the web application flow I want to change the InstanceScope to HttpSession to obtain a long conversation, so I do this:
PluginTypeConfiguration config = ObjectFactory.Model.PluginTypes.FirstOrDefault(p => p.PluginType.FullName.Contains("INHUnitOfWork"));
config.Lifecycle.EjectAll();
config.Lifecycle = StructureMap.Pipeline.Lifecycles.GetLifecycle(InstanceScope.HttpSession);
This seems to replace the initial InstanceScope, unfortunately it lasts only for the current request. When the next request arrives, the initial configuration is active again and the session information is lost.
Later I also want to be able to revert the change with something like this:
PluginTypeConfiguration config = ObjectFactory.Model.PluginTypes.FirstOrDefault(p => p.PluginType.FullName.Contains("INHUnitOfWork"));
config.Lifecycle.EjectAll();
config.Lifecycle = StructureMap.Pipeline.Lifecycles.GetLifecycle(InstanceScope.HttpContext);
but if I will make it work in one direction it will probably work in both.
Is it possible to replace the initial InstanceScope permanently at runtime? How should this be implemented? Also, do you think this a good way to obtain a long conversation or there is a better / simpler way to do it with StructureMap & NHibernate?