title: How should I design this? Nhibernate unit of work in a method
body: <p>I am using asp.net mvc and nhibernate with the unit of work pattern.</p>
  
  <p>I have something like this</p>
  
  <pre><code>public bool IsSomething()
  {
     unitOfWork.BeginTransaction();
     var myGetStuff = repo.GetStuff(1);
  
     if(myGetStuff == null)
     {
         return false;
     }  
  
     var somethingElse = myGetStuff.GetSomethingElse();
  
     if(somethngElse == null)
     {
        return false;
     } 
  
     return true;
  }
  </code></pre>
  
  <p>So in my if statements if something is null and I need it not to be null I just get out of the statement. </p>
  
  <p>This is opposed as having nested if statements that could be like nested 4 or 5 times to do null checks.</p>
  
  <pre><code>public bool IsSomething()
  {
     unitOfWork.BeginTransaction();
     var myGetStuff = repo.GetStuff(1);
  
     if(myGetStuff != null)
     {
        var somethingElse = myGetStuff.GetSomethingElse();
  
         if(somethngElse != null)
         {
            // some more check heere ( could have another few if statements null checks here)
         } 
     }  
  }
  </code></pre>
  
  <p>So I find the first way much easier to read then nested levels of if statements.</p>
  
  <p>My problem is that even if you do a query in nhibernate you <strong>must</strong> wrap it around in a transaction and at the end do either a rollback or commit.</p>
  
  <p>Option 1</p>
  
  <pre><code>  public bool IsSomething()
      {
         unitOfWork.BeginTransaction();
         var myGetStuff = repo.GetStuff(1);
  
         if(myGetStuff == null)
         {
             unitOfWork.Commit();
             return false;
         }  
  
         var somethingElse = myGetStuff.GetSomethingElse();
  
         if(somethngElse == null)
         {
          unitOfWork.Commit();
            return false;
         } 
  
         unitOfWork.Commit();
         return true;
      }
  </code></pre>
  
  <p>This way I don't like as you have to keep putting commit everywhere. If possible I would love to have just one commit(unless I have more than one unit of work transaction)</p>
  
  <p>So then I though why not put it in a finally like this</p>
  
  <pre><code>  public bool IsSomething()
      {
       try
       {
             unitOfWork.BeginTransaction();
             var myGetStuff = repo.GetStuff(1);
  
             if(myGetStuff == null)
             {
                 return false;
             }  
  
             var somethingElse = myGetStuff.GetSomethingElse();
  
             if(somethngElse == null)
             {
  
                return false;
             } 
  
             return true;
         }
         catch(Exception ex)
         { 
            unitOfWork.RollBack();
         }
         finally
         {
            unitOfWork.Commit();
         }
      }
  </code></pre>
  
  <p>I like this but then I realized what happens if the commit fails? It won't rollback and the exception won't be caught.</p>
  
  <p>So anyone else have any ideas?</p>