简体   繁体   中英

Provide a Repository class for my asp.net mvc web application. How to make sure i am disposing eveything correctly

I have a question about how i need to manage my Dispose method, when i am implementing a repository class inside my asp.net mvc web application.

Currently i have the following:-

  1. i am using VS 2013, and i created a new asp.net mvc-5 web application.
  2. i uses entity framework 6.0 to map my database tables inside my web application and generates a .edmx file.
  3. I created a new Controller class for one of my model objects, using "MVC 5 controller , using entity framework" scaffold .

now inside the generated controller class i got this dispose method:-

 public class DeptsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
//code goes here
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

now i want to move my data access code to be inside a separate Repository class, and to be initiating my DbContext object inside the Repository class rather than inside the controller class.

so i created the following repository class:-

public class repository

    {
        private ApplicationDbContext db = new ApplicationDbContext();
        //code goes here...
        public void Dispose()
        {
            db.Dispose();
        }

and i modified my controller class to be using the repository class as follow:-

 public class DeptsController : Controller
    {
        //private ApplicationDbContext db = new ApplicationDbContext();
        private repository repo = new repository();
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                repo.Dispose();
            }
            base.Dispose(disposing);
        }
    }

so can anyone adivce on the following :-

  1. since i will be moving my data access logic to be inside my repository , so what else i need to dispose other than the DbContext ? i mean before using the Repository , calling base.Dispose(disposing); inside the controller will make sure all the unmanaged resources are disposed correctly.. but when i moved my data access to be inside the repository , how i can make sure that all the unmanaged resources consumed by the repository are disposed ?? and is calling base.Disposed(disposing) inside the controller class , will also dispose any unmanaged resources consumed inside the repository class ?

Can anyone adivce on this ?

As far as I can tell you don't need to do anything else to dispose of DbContext in Repository class.

If you have more resources in Repository class then you will need to add their .Dispose() call in Repository.Dispose and that will be it.

base.Dispose(disposing) in DeptsController class calls Dispose method of it's base class , ie Controller ... that's so base class can dispose of resources it's internally using - nothing you should worry about.

Contexts are not meant to be kept open indefinitely. They are meant to be created when needed, used, and disposed after you're done.

In addition, ASP.NET is a stateless platform, so your context will be garbage collected when the response is done anyways.

So your repository does not need to implement IDispoasable , and thus your controller does not either. Create a DbContext within a using block in your repository, and it will be disposed automatically.

However...

how i can make sure that all the unmanaged resources consumed by the repository are disposed ?

That's what Dispose is for. Whatever creates disposable objects is responsible for disposing of them. So if you create disposable objects in your repository and can't immediately dispose of them (for whatever reason), then you need to dispose of them in the Dispose method. If you don't inherit from a disposable base class, then there's no base dispose to call; otherwise you should call the base dispose to make sure the base class disposes of any resources

is calling base.Disposed(disposing) inside the controller class , will also dispose any unmanaged resources consumed inside the repository class ?

No. Calling base.Dispose calls the base class's Dispose method. The base class has no knowledge of the repository, so calling base.Dispose does nothing in regard to disposing of the respository. That is done in your controller's Dispose method when you call repo.Dispose() .

calling base.Dispose(disposing); inside the controller will make sure all the unmanaged resources are disposed correctly.

Well, it makes sure all unmanaged (and managed IDisposable ) resources created by the base class are disposed of. And resources created by your derived controller should be disposed of in the overridden Dispose method (which you are doing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM