[英]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. 我在asp.net mvc Web应用程序中实现存储库类时,有一个关于如何管理我的Dispose方法的问题。
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. 现在,我想将数据访问代码移动到单独的Repository类中,并在Repository类而不是控制器类内部初始化DbContext对象。
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);
我的意思是在使用Repository之前,先调用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 ? 并在controller类内调用base.Disposed(dispose),是否还会处置存储库类内消耗的所有非托管资源? 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. 据我所知,您无需执行其他任何操作DbContext
在Repository
类中处置DbContext
。
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. 如果您有更多的资源 Repository
类,那么你将需要添加自己的.Dispose()
调用在Repository.Dispose
,这将是它。
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. base.Dispose(disposing)
在DeptsController
调用类Dispose
的方法,它的基类 ,即Controller
......是如此的基类可以处理它的内部使用的资源-什么你应该担心。
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. 另外,ASP.NET是一个无状态平台,因此无论如何响应都会被垃圾回收。
So your repository does not need to implement IDispoasable
, and thus your controller does not either. 因此,您的存储库不需要实现IDispoasable
,因此您的控制器也不需要实现。 Create a DbContext
within a using
block in your repository, and it will be disposed automatically. 在您的存储库的using
块内创建一个DbContext
,它将被自动处置。
However... 然而...
how i can make sure that all the unmanaged resources consumed by the repository are disposed ? 我如何确保处置库消耗的所有非托管资源?
That's what Dispose
is for. 这就是Dispose
目的。 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. 因此,如果您在存储库中创建了可抛弃的对象而不能立即处置(出于某种原因),那么您需要在Dispose
方法中处置它们。 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 ? 正在在控制器类内部调用base.Disposed(disposing)
disposebase.Disposed(disposing)
,还将处置存储库类内部消耗的任何非托管资源吗?
No. Calling base.Dispose
calls the base class's Dispose
method. 否。调用base.Dispose
调用基类的Dispose
方法。 The base class has no knowledge of the repository, so calling base.Dispose
does nothing in regard to disposing of the respository. 基类不了解存储库,因此调用base.Dispose
不会处理存储库。 That is done in your controller's Dispose
method when you call repo.Dispose()
. 当调用repo.Dispose()
时,这是在控制器的Dispose
方法中完成的。
calling base.Dispose(disposing); 调用base.Dispose(dispose); 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. 好吧,它可以确保处置基类创建的所有非托管(和托管IDisposable
)资源。 And resources created by your derived controller should be disposed of in the overridden Dispose
method (which you are doing). 由派生的控制器创建的资源应在重写的Dispose
方法中Dispose
(您正在执行)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.