Bootstrapping NHibernate with StructureMap

107 views
Skip to first unread message

Weston Binford

unread,
Oct 7, 2009, 1:52:57 PM10/7/09
to structuremap-users
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:

http://trason.net/journal/2009/10/6/bootstrapping-nhibernate-with-structuremap.html

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.

Thanks,
Weston

Frank L. Quednau

unread,
Oct 8, 2009, 4:02:35 AM10/8/09
to structuremap-users
Hi there,
is InstanceScope.Hybrid like PerRequest one, but with a fallback?

For reference, I have these entries:

ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.ConstructedBy(()=>new SessionFactoryMaker
().CreateFactory());

ForRequestedType<ISession>()
.CacheBy(InstanceScope.PerRequest)
.TheDefault.Is.ConstructedBy(
ctx => ctx.GetInstance<ISessionFactory>().OpenSession());


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;
}

public ISession CurrentSession()
{
return session ?? (session = factory.OpenSession());
}
}

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:
>
> http://trason.net/journal/2009/10/6/bootstrapping-nhibernate-with-str...

Weston Binford

unread,
Oct 8, 2009, 5:07:35 PM10/8/09
to structuremap-users

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:

http://structuremap.sourceforge.net/Scoping.htm

-Weston

Ryan Heath

unread,
Oct 9, 2009, 3:10:34 AM10/9/09
to structure...@googlegroups.com
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.

What do you think?

// Ryan

Frank L. Quednau

unread,
Oct 10, 2009, 12:17:58 PM10/10/09
to structuremap-users
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.

Weston Binford

unread,
Oct 15, 2009, 11:13:04 AM10/15/09
to structuremap-users
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?

http://trason.net/journal/2009/10/10/nhibernate-transactional-boundaries.html

-Weston

On Oct 10, 11:17 am, "Frank L. Quednau" <fqued...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages