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 739709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:25:55+00:00 2026-05-14T08:25:55+00:00

I encapsulate my linq to sql calls in a repository class which is instantiated

  • 0

I encapsulate my linq to sql calls in a repository class which is instantiated in the constructor of my overloaded controller. The constructor of my repository class creates the data context so that for the life of the page load, only one data context is used.

In my destructor of the repository class I explicitly call the dispose of the DataContext though I do not believe this is necessary.

Using performance monitor, if I watch my User Connections count and repeatedly load a page, the number increases once per page load. Connections do not get closed or reused (for about 20 minutes).

I tried putting Pooling=false in my config to see if this had any effect but it did not. In any case with pooling I wouldn’t expect a new connection for every load, I would expect it to reuse connections.

I’ve tried putting a break point in the destructor to make sure the dispose is being hit and sure enough it is. So what’s happening?

Some code to illustrate what I said above:

The controller:

public class MyController : Controller
{
    protected MyRepository rep;

    public MyController ()
    {
        rep = new MyRepository();
    }
}

The repository:

public class MyRepository
{
    protected MyDataContext dc;

    public MyRepository()
    {
        dc = getDC();
    }

    ~MyRepository()
    {
        if (dc != null)
        {
            //if (dc.Connection.State != System.Data.ConnectionState.Closed)
            //{
            //    dc.Connection.Close();
            //}
            dc.Dispose();
        }
    }

    // etc
}

Note: I add a number of hints and context information to the DC for auditing purposes. This is essentially why I want one connection per page load

Update:
After having implemented IDisposable on my repository and on my controller class I couldn’t find a way to specifically call the Dispose method on my controller as the controller is created and destroyed behind the scenes by the MvcHandler. However I did find that my connections were being closed anyway. I wasn’t comfortable knowing that this was working but not knowing why so I did some digging and found an MSDN quote that made me happy:

When execution is complete, the MvcHandler will check if the controller implements the IDisposable interface, and if so, will invoke Dispose on the controller to clean up unmanaged resources.

Final Update:
After a month or so with working this I’ve now removed all this code and gone down the MS advised route of wrapping a “using” statement around the code in my public repository methods and passing this DC into the private methods. This seems a little wasteful and repetitive as well as resulting in a few more connections being opened and closed. But I was getting linq to sql caching that I could only resolve by resetting the DC.

  • 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-14T08:25:55+00:00Added an answer on May 14, 2026 at 8:25 am

    The correct pattern (short but sufficient version) here is:

    public class MyRepository : IDisposable
    {
        ...  // everything except the dtor
    
        public void Dispose()
        {
            if (dc != null)
            {
                dc.Dispose();
            } 
        }
    }
    
    public class MyController : Controller, IDisposable
    {
        protected MyRepository rep;
    
        public MyController ()
        {
            rep = new MyRepository();
        }
    
        public void Dispose()
        {
           if (rep!= null)
           {
              rep.Dispose();
           } 
        }
    }
    

    And now you can (should) use MyController with the using clause:

    using (var ctl = new MyController ())
    {
       // use ctl
    }
    

    Edit:
    Just noticed it cascades to MyController, code added. This shows how indirect ownership of an unmanaged resource spreads out.

    Edit 2:
    This also correct (as would be a try/finally):

    var ctl = GetController ();
    using (ctl)
    {
       // use ctl
    }
    

    If you can’t keep it local to 1 method, just do your best to call ctl.Dispose() in a Closing event or such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are your favorite ways to encapsulate LINQ to SQL entity classes and data-context
I'm trying to properly encapsulate a class A, which should only be operated on
Our current MVC project is set up to have ViewModels that encapsulate the data
Sometimes I think that Controller and Command are the same because they both encapsulate
I have a Linq-to-SQL model that uses stored procs in some places to return
Are there any tools out there that encapsulate the retrieval of SQL Server metadata
I know that properties kind of encapsulate getter and setter methods. So whenever we
I'm using LINQ to Entities on a database which structure is not known in
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I try to encapsulate. Exeption from interface, static inner class working, non-static inner class

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.