title: Using Unit of Work design pattern / NHibernate Sessions in an MVVM WPF
body: <p>I think I am stuck in the paralysis of analysis. Please help!</p>
  
  <p>I currently have a project that</p>
  
  <ul>
  <li>Uses NHibernate on SQLite</li>
  <li>Implements Repository and Unit of Work pattern: <a href="http://www.nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx" rel="nofollow noreferrer">http://www.nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx</a></li>
  <li>MVVM strategy in a WPF app</li>
  </ul>
  
  <p>Unit of Work implementation in my case supports one NHibernate session at a time. I thought at the time that this makes sense; it hides inner workings of NHibernate session from ViewModel. </p>
  
  <p>Now, according to Oren Eini (Ayende): <a href="http://msdn.microsoft.com/en-us/magazine/ee819139.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/ee819139.aspx</a></p>
  
  <p>He convinces the audience that NHibernate sessions should be created / disposed when the view associated with the presenter / viewmodel is disposed. He presents issues why you don't want one session per windows app, nor do you want a session to be created / disposed per transaction. This unfortunately poses a problem because my UI can easily have 10+ view/viewmodels present in an app. He is presenting using a MVP strategy, but does his advice translate to MVVM?</p>
  
  <p>Does this mean that I should scrap the unit of work and have viewmodel create NHibernate sessions directly? Should a WPF app only have one working session at a time? If that is true, when should I create / dispose a NHibernate session? </p>
  
  <p>And I still haven't considered how NHibernate Stateless sessions fit into all this! My brain is going to explode. Please help!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I found Ayende's Unit of Work implementation in Rhino Tools. I discovered that there are significant differences between his implementation and the one I did. His definitely supported multiple sessions. After further research I think it is best that I do the following:</p>
  
  <ul>
  <li>Scrap my implementation of Unit of Work</li>
  <li>Resign to using NHibernate's ISession and IStatelessSession objects directly from viewmodel. While in my opinion it's not ideal, I've already spent too much time on Unit of Work and it's not shaping up to what it is. Gotta apply KISS and YAGNI at some point. I can at least take solace in the fact that Ayende's article and a few others point out that using those directly is OK.</li>
  <li>If I really really don't want to expose ISession, I can always use Castle.ActiveRecord, but I think that's not necessary.</li>
  <li>I can re-use the Session Factory code, so the Unit of Work implementation is not a total waste.</li>
  <li>Refactored my repositories to allow injection of both StatelessSession and Session, and use stateless if it's available: otherwise use regular session.</li>
  </ul>
  
  <p><em>After all that, then I can apply the strategy of opening one session / stateless session per viewmodel and when view is disposed, have viewmodel flush / dispose the session / stateless session.</em></p>
  
  <p>Sounds like a plan?</p>