What you are describing is a common mistake made by new NHibernate
users in Windows Forms or WPF applications.  If you start injecting
sessions into view models and/or presenters, you are asking for
trouble.  If you have any exceptions thrown while working with a
session, then that session can no longer be used.  This would then
require you to throw away your presenter or view model and build a new
one with a fresh session.  I remember trying this years ago when I
first started working with NH so I am speaking from the experience of
having made this mistake and paid the price.

In contrast to desktop apps, web applications contain a natural
boundary due to the HTTP Request pipeline.  HTTP requests create a
natural sense of "conversations" occurring between the client and the
server.  Desktop applications do not have such a natural boundary.
The best solution to problems like what you describe is to craft code
the creates such conversational contexts, preferably based on the
business concerns of your problem domain.  Rather than just thinking
in terms of "data access," taking a conversational approach leads to
more intention revealing code that coheres better with business
concerns.  You may even consider a CQRS approach with a command bus
that execute command handlers within the confines of a single session/
transaction/unit-of-work.

If this seems daunting, then at a minimum, rather than injecting a
session using IoC, you should be injecting a Session Factory and
spinning up Sessions as you need them.  This will allow you to recover
from a failure in one session by spinning up a new one from the
factory.  Sessions are cheap to create.  The rule is "One Session per
Application" (assuming you have one database you're working with) and
many, many, many Sessions.

--Jeff


On Aug 29, 4:22 pm, Kevin Anderson <[email protected]> wrote:
>   What are the implications of having one session (managed by IOC) that
> is shared by the whole application?
>
> We have a WPF, MVVM application that originally used one session for
> each call to the repository, but found that we couldn't lazy load
> anything because the session was gone. We moved the session of the view
> model but found that in some instances a given object was being held in
> two different sessions. We then tried moving the session all the way up
> to the IOC and found that performance was fantastic.
>
> I am concerned that there will be negative impacts from having one
> session open for the lifetime of the application.
>
> Any thoughts?
>
> Thanks.
> Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to