Jeremy asked me to help him put together an example of bootstrapping
NHibernate with StructureMap. I was originally going to post my take
here on the mailing list, but found issues trying to format the code.
So, I put together a blog post:
I would appreciate any feedback on my example. You can comment here
or on my blog. However, if we keep the discussion here, then I think
more people will be able to follow it.
I quite like using the feature Current Session Context that allows
getting the "correct" session. For my StructureMap setup it looks
pretty primitive, and for the tests it looks e.g. like this
public class TestNHSessionContext : ICurrentSessionContext
{
private readonly ISessionFactory factory;
private ISession session;
public TestNHSessionContext(ISessionFactory factory)
{
this.factory = factory;
}
It basically makes my session a singleton throughout several tests
(the thing with the in-memory DB that disappears when you close the
session / connection).
Using the session is then via factory.GetCurrentSession();
On Oct 7, 7:52 pm, Weston Binford <wbinf...@gmail.com> wrote:
> Jeremy asked me to help him put together an example of bootstrapping
> NHibernate with StructureMap. I was originally going to post my take
> here on the mailing list, but found issues trying to format the code.
> So, I put together a blog post:
> I would appreciate any feedback on my example. You can comment here
> or on my blog. However, if we keep the discussion here, then I think
> more people will be able to follow it.
InstanceScope.Hybrid means that StructureMap will store the object in
the HttpContext, if available, or in thread local storage as a fall
back.
BTW, I believe that PerRequest, the default, means StructureMap will
give you a new object every time you request one. PerRequest should
not be confused with an HttpRequest. I think that your configuration
below will new up a new ISession every time you request a session.
See here for full documentation on StructureMap scoping:
> I quite like using the feature Current Session Context that allows
> getting the "correct" session. For my StructureMap setup it looks
> pretty primitive, and for the tests it looks e.g. like this
> public class TestNHSessionContext : ICurrentSessionContext
> {
> private readonly ISessionFactory factory;
> private ISession session;
> It basically makes my session a singleton throughout several tests
> (the thing with the in-memory DB that disappears when you close the
> session / connection).
> Using the session is then via factory.GetCurrentSession();
Maybe the InstanceScope.PerRequest should be deprecated and an other name should be introduced with the exact same meaning. Just to avoid any confusion with http requests. A new name could be perhaps InstanceScope.NewInstance.
> InstanceScope.Hybrid means that StructureMap will store the object in
> the HttpContext, if available, or in thread local storage as a fall
> back.
> BTW, I believe that PerRequest, the default, means StructureMap will
> give you a new object every time you request one. PerRequest should
> not be confused with an HttpRequest. I think that your configuration
> below will new up a new ISession every time you request a session.
> See here for full documentation on StructureMap scoping:
> > I quite like using the feature Current Session Context that allows
> > getting the "correct" session. For my StructureMap setup it looks
> > pretty primitive, and for the tests it looks e.g. like this
> > It basically makes my session a singleton throughout several tests
> > (the thing with the in-memory DB that disappears when you close the
> > session / connection).
> > Using the session is then via factory.GetCurrentSession();
I have updated my Bootstrapping NHibernate with StructureMap example
to move the responsibility for committing database changes to the
HttpModule. I got tired of having to call commit in my Action
methods. Is this the right approach? Am worried that my UnitOfWork/
TransactionBoundary class will get too complicated. What do you
think?
> Oh!
> Thank goodness it's just a config change away. To be honest, I didn't
> look into the Scoping Doc, just drew the conclusion too quickly.
> On Oct 8, 11:07 pm, Weston Binford <wbinf...@gmail.com> wrote:
> > InstanceScope.Hybrid means that StructureMap will store the object in
> > the HttpContext, if available, or in thread local storage as a fall
> > back.
> > BTW, I believe that PerRequest, the default, means StructureMap will
> > give you a new object every time you request one. PerRequest should
> > not be confused with an HttpRequest. I think that your configuration
> > below will new up a new ISession every time you request a session.
> > See here for full documentation on StructureMap scoping:
> > > I quite like using the feature Current Session Context that allows
> > > getting the "correct" session. For my StructureMap setup it looks
> > > pretty primitive, and for the tests it looks e.g. like this
> > > It basically makes my session a singleton throughout several tests
> > > (the thing with the in-memory DB that disappears when you close the
> > > session / connection).
> > > Using the session is then via factory.GetCurrentSession();