简体   繁体   English

为我的asp.net mvc Web应用程序提供一个Repository类。 如何确保我正确处理所有东西

[英]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:- 目前我有以下内容:

  1. i am using VS 2013, and i created a new asp.net mvc-5 web application. 我正在使用VS 2013,并且创建了一个新的asp.net mvc-5 Web应用程序。
  2. i uses entity framework 6.0 to map my database tables inside my web application and generates a .edmx file. 我使用实体框架6.0在我的Web应用程序中映射数据库表并生成一个.edmx文件。
  3. I created a new Controller class for one of my model objects, using "MVC 5 controller , using entity framework" scaffold . 我使用“ MVC 5控制器,使用实体框架”支架为我的一个模型对象创建了一个新的Controller类。

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 :- 任何人都可以这样做:

  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 ? 由于我将把数据访问逻辑移到我的存储库中,所以除了DbContext外还需要处理什么? i mean before using the Repository , calling 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. 据我所知,您无需执行其他任何操作DbContextRepository类中处置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) dispose base.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.

相关问题 我的asp.net mvc Web应用程序中的OutputCache设置。 多种语法防止缓存 - OutputCache setting inside my asp.net mvc web application. Multiple syntax to prevent caching 在我的 asp.net 核心 MVC web 应用程序中,我如何使验证码成为必填字段 - Inside my asp.net core MVC web application how i can make the recaptcha a required field 我无法在 ASP.NET MVC 5. 应用程序的视图中转换我的日期时间格式。 我该如何解决? - I can't convert my datetime format in the View of a ASP.NET MVC 5. Application. How do I solve it? 我正在创建一个 ASP.NET MVC web 应用程序 - I am creating an ASP.NET MVC web application 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application ASP.Net核心MVC存储库模式意外处置 - ASP.Net Core MVC Repository Pattern Unexpectedly disposing 为ASP.NET MVC Web应用程序提供API - Provide API with asp.net MVC Web Application Castle Windsor ASP.NET MVC 4和Web API相同的应用程序。 只有MVC控制器没有依赖关系? - Castle Windsor ASP.NET MVC 4 and Web API same application. Only if MVC controllers have no dependencies? 我尝试在 *somee.com* 上托管一个 ASP.NET C# web 应用程序,但我无法正确显示我的主页 - I tried hosting an ASP.NET C# web application on *somee.com* and I am not able to display my homepage correctly 从ASP.Net Web API和MVC Web应用程序访问Microsoft Graph API(用于OneDrive)。 - Accessing Microsoft Graph API (for OneDrive) from ASP.Net Web API & MVC Web Application.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM