Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:45:13+00:00 2026-05-26T08:45:13+00:00

I am using asp.net mvc and nhibernate with the unit of work pattern. I

  • 0

I am using asp.net mvc and nhibernate with the unit of work pattern.

I have something like this

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

So in my if statements if something is null and I need it not to be null I just get out of the statement.

This is opposed as having nested if statements that could be like nested 4 or 5 times to do null checks.

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

So I find the first way much easier to read then nested levels of if statements.

My problem is that even if you do a query in nhibernate you must wrap it around in a transaction and at the end do either a rollback or commit.

Option 1

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

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)

So then I though why not put it in a finally like this

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

I like this but then I realized what happens if the commit fails? It won’t rollback and the exception won’t be caught.

So anyone else have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:45:14+00:00Added an answer on May 26, 2026 at 8:45 am

    This code of your looks rather problematic to me, especially when there are nested unit of work calls (how do you handle those?). What I would do is to open the unit of work (and the transactions) in the calling code (the Controller in your case as you use ASP.Net MVC) and not the IsSomething function. It would look something like this:

    try
    {
        unitOfWork.BeginTransaction();
        // some code
        var isSomething = IsSomeThing()
    }
    catch(Exception ex)
    { 
       unitOfWork.RollBack();
    }
    finally
    {
        unitOfWork.Commit();
    }
    

    The IsSomething function would then look simply like this

    public bool IsSomething()
    {
       var myGetStuff = repo.GetStuff(1);
    
       if(myGetStuff == null)
       {
          return false;
       }  
    
       var somethingElse = myGetStuff.GetSomethingElse();
    
       if(somethngElse == null)
       {
    
           return false;
        } 
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC 2 with nhibernate. I have this Sales class. Sales
I have a ASP.NET MVC application using NHibernate and the application runs fine when
I'm developing an application using asp.net mvc, NHibernate and DDD. I have a service
I have a asp.net-mvc website with a SQL server backend (using nhibernate for OR
I have a ASP.NET MVC 3 application using NHibernate over a PostgreSQL database. There
I am using NHibernate and ninject in ASP.Net MVC, using this page as a
I'm using S#arpArchitecture (ASP.NET MVC and Fluent NHibernate). I have an entity as follows:
I'm currently learning ASP.NET MVC and using Nhibernate. I would like to use Cascading
I have about 4-6 years .NET experience. Primarly using ASP.NET MVC/Nhibernate/Castle Windsor/MSpec/etc in visual
I have an asp.net-mvc website and i am using ninject for IOC and nhibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.