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

  • SEARCH
  • Home
  • 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 8181099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:24:25+00:00 2026-06-07T00:24:25+00:00

Each request in my web app can get one data access object instance (of

  • 0

Each request in my web app can get one data access object instance (of type UnitofWork), via MVC3’s own dependency injection mechanism. So far so good.

I am creating an Idisposable UnitofWorkScope object to aggregate some store calls on this data access object, and to then call them together. Actually the UnitofWorkScope only controls the UnitofWork object, which has the facility for adding stores to a list and calling them later. I believe that the UnitofWorkScope object should have exclusive access the data access object.

Now the question: I am wondering anyone has any objections to an exclusive lock being obtained in the constructor, using Monitor.Enter(), and then released in the dispose method, using Monitor.Exit();

I have muddied the waters a buit by describing why I am asking this, but feel free to comment on anything that I have put here.

public class UnitofWorkScope : IDisposable
{
    public UnitofWorkScope(UnitOfWork UnitofWork)
    {
        if (UnitofWork == null)
        {
            throw new ArgumentException("UnitofWork argument null");
        }  
        this._unitofWork = UnitofWork;
        Monitor.Enter(_unitofWork); // obtaining exclusive access to the DAO of this request
        this._unitofWork.AggregateDbChanges = true; //switched back off in dispose method
    }

    private readonly UnitOfWork _unitofWork;

    bool _disposed;

    public void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            _unitofWork.CallFuncList();
            Monitor.Exit(_unitofWork); //releasing the lock
            _disposed = true;
            GC.SuppressFinalize(this);
        }
    }

    public void Dispose()
    {
        Dispose(true);
    }

    ~UnitofWorkScope()
    {
        if (!_disposed)
        {
            Dispose(false);
        }
    }
}

The idea is to use this UnitofWorkScope in like this:

UnitofWork _unitofWork = Resolver.GetService<UnitofWork>(); //gets the UnitofWork DAO

using (UnitofWorkScope UnitofWorkScope = new UnitofWorkScope(_unitOfWork))
{
    // do a store

    _unitofWork.Store<SomeClass>(_someInstance);

   // do some more stores 

   try
   {
        UnitofWorkScope.Dispose(true); 
   }
   catch (exception ex)
   {
     //try to undo those stores.
   }
} 
  • 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-06-07T00:24:28+00:00Added an answer on June 7, 2026 at 12:24 am

    Yes, this is not a bad pattern for implementing locks. However: I would recommend a slightly different version of Dispose to guarantee the lock is released even if _unitofWork.CallFuncList() throws an exception, which you are relying on to detect the need to perform some kind of rollback.

    private void Dispose(bool disposing) 
    { 
        if (!_disposed) 
        { 
            try
            {
                _disposed = true; 
                _unitofWork.CallFuncList(); 
            }
            finally
            {
                Monitor.Exit(_unitofWork); //releasing the lock 
                GC.SuppressFinalize(this); 
            }
        } 
    } 
    

    However, you may want to separate out the “commit” from the lock “release” logic so that you don’t have to explicitly call Dispose(), which the using statement will do automatically for you.

    public void Commit()
    {
        _unitofWork.CallFuncList(); 
    }
    
    private void Dispose(bool disposing) 
    { 
        if (!_disposed) 
        { 
            try
            {
                _disposed = true; 
            }
            finally
            {
                Monitor.Exit(_unitofWork); //releasing the lock 
                GC.SuppressFinalize(this); 
            }
        } 
    }
    

    You can then use it like this:

    using (var unitofWorkScope = new UnitofWorkScope(_unitOfWork))     
    {     
        // do a store     
    
        _unitofWork.Store<SomeClass>(_someInstance);     
    
       // do some more stores      
    
       try     
       {     
            unitofWorkScope.Commit();
       }     
       catch (exception ex)     
       {     
         //try to undo those stores.     
       }     
    }  // unitofWorkScope.Dispose() automatically called here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why i cant get the desired delay between each request? They
I'll try to describe the situation. We have a web service; on each request
I am calling 5 external servers to retrieve XML-based data for each request for
I'm using Symfony 1.2.7. My web is in several languages, each of one is
In my web app , i am using jquery ui autocomplete which data are
I have code that sends web requests through two parallel for each loops. Will
I have a function that returns the request parameters for each request: private function
Since REST is stateless, each request that comes in has no knowledge of the
I've written a high-throughput server that handles each request in its own thread. For
I need to send a request to a quote server. Each request should have

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.