I'm using StructureMap for dependency injection and I want to inject NHibernate sessions with it. I have the following code:
private static Container _container { get; set; }
static MyClass()
{
_container = new Container(r =>
{
r.For<ISessionFactory>().Singleton()
.Use(NHibernate.GetSessionFactory());
r.For<ISession>().HybridHttpOrThreadLocalScoped()
.Use(_container.GetInstance<ISessionFactory>().OpenSession());
});
}
However, I can't help but think that referencing _container from within the _container's initialization seems awkward. Is this an acceptable practice? Is it going to backfire down the road? Is there a better way? How do you handle dependencies that require the creation of another dependency to create themselves?