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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:53:10+00:00 2026-06-02T22:53:10+00:00

Nearly all my controllers have a repository instantiated by initializer like this: public class

  • 0

Nearly all my controllers have a repository instantiated by initializer like this:

public class CompanyController : CtsController
{
    ICompanyRepository _repository = new CompanyRepository();

I would like to try and ensure that my repositories are always properly disposed, especially seeing as I do impersonation in them, and I believe one must be careful here. But, I don’t want to have to always wrap each repository call in a new using loop in every action method.

How does MVC3 look after the lifetime and disposal of controller instances, and is it enough if I implement Disposable on them and dispose of my repos in the destructor for the controller?

  • 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-02T22:53:11+00:00Added an answer on June 2, 2026 at 10:53 pm

    The best way to deal with this is to use Dependency Injection (also known as Inversion of Control or IOC) and a custom IControllerFactory so that your repository is created and disposed of automatically. Here is an article explaining how to do it:

    Dependency Injection in ASP.NET MVC: Controllers

    For MVC3, you may find some articles recommending using IDependencyResolver instead but you should be cautious of that because that interface does not have a Release callback and will cause memory leaks (dependent on which IOC container you decide to use).

    If you are new to Dependency Injection, search the web for some articles on the ideas behind it.

    If you don’t want to switch to using IOC, then you can use a base controller that overrides the OnActionExecuted method and disposes of all IDisposables. For example:

    abstract class DisposingController : Controller
    {
        protected IList<IDisposable> Disposables;
    
        protected DisposingController()
        {
            Disposables = new List<IDisposable>();
        }
    
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            foreach (var disposable in Disposables)
            {
                disposable.Dispose();
            }
    
            base.OnActionExecuted(filterContext);
        }
    }
    

    Then in your controller:

    var myRepository = new MyRepository();
    Disposables.Add(myRepository);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious as to why I see nearly all C macros formatted like this:
Many (most? nearly all?) SQL dialects have a way to perform this type of
I've a very basic class, name it Basic, used in nearly all other files
Yes, this is another Pivot question... I've read through nearly all the previous questions
I have a Java Multiproject (About 15 sub projects) in Netbeans 6.9.1. Nearly all
I want to disable viewstate for nearly all of my pages and I have
I'd like to create several modules that will be used in nearly all scripts
I have a tag cloud that I'm including on nearly all of the views
Nearly all programming languages that support threading, have a method called join . I
Using SQLite I need to copy nearly all of an existing row from a

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.