I'm using StructureMap as my IoC container and NHibernate as my ORM. I found an example online that shows how to have StructureMap build the ISessionFactory and the ISession so the Factory is a singleton and the Session is based on the HttpContext. This works great, but then I started using NH Profiler which told me I should always be explicitly using Transactions. So, I thought, why not let StructureMap handle that for me too? Using the code below, I got that all working fine, except, I don't know how/where to commit/rollback my transaction.
Here is how I initialize StructureMap:
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.ConstructedBy(cfg.BuildSessionFactory);
x.ForRequestedType<ISession>()
.CacheBy(InstanceScope.Hybrid)
.TheDefault.Is.ConstructedBy(context => context.GetInstance<ISessionFactory>().OpenSession());
x.ForRequestedType<ITransaction>()
.CacheBy(InstanceScope.Hybrid)
.TheDefault.Is.ConstructedBy(context => context.GetInstance<ISession>().BeginTransaction());
x.Scan(y =>
{
y.TheCallingAssembly();
y.WithDefaultConventions();
});
});
All my repositories look like the following:
public UserRepository(ISession session, ITransaction transaction)
{
_session = session;
_transaction = transaction;
}
And a typical method inside a repository looks like:
public void Remove(int id)
{
Remove(_session.Get<User>(id));
}
What I'm trying to do is have all the methods that I call in one HttpContext share the same Session and Transaction. Is this possible or is this totally wrong and I'm barking up the wrong tree?
Thanks in advance!
-Dan