Reputation: 1
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:-
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 :-
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 ?
Upvotes: 0
Views: 332
Reputation: 152521
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).
Upvotes: 1
Reputation: 20596
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, i.e. Controller
... that's so base class can dispose of resources it's internally using - nothing you should worry about.
Upvotes: 1