c# - Dispose() 如何在我的 Controller 和 Repository 类中工作

标签 c# asp.net .net asp.net-mvc dispose

我正在研究 asp.net mvc-4 网络应用程序和 Entity Framework 5.0。现在我对 Dispose 在我的应用程序中的工作方式感到困惑。目前我有以下设置:-

-我有一个 APIRepository 类,其中包含多个使用 WebClient() 进行外部 API 调用的方法。并且我没有在此类中定义任何 Dispose 方法。

    public class APIRepository
        {

            public string AddTicket(string title, string technichian,string account,string site,string description,string mode,string requestor)
            {

                //code goes here

                        using (var client = new WebClient())
                        {
                        }
                   return result;
            }
//code goes here
        }

-我有一个包含我的数据访问逻辑的 Repository 类,它启动了我的 DbContext,我在这个类中定义了一个 Dispose 方法。

public class Repository
{

        private MyEntities my = new MyEntities();
//code goes here...
        public void Dispose()
        {
           my.Dispose();

        }

-我有一个 Controller 类,它启动两个存储库类:-

[RequireHttps]

public class ServerController : Controller
{

        Repository repository = new Repository();
        APIRepository APIrepository = new APIRepository();

//code goes here
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                repository.Dispose();

            }
            base.Dispose(disposing);
        }

现在我对我当前的项目有以下问题:-

  1. 根据我对 Dispose 工作原理的理解,当操作方法调用 View 时,asp.net mvc 将自动调用我当前 Controller 类中的 Dispose 方法。依次调用我的存储库中的 Dispose 方法,这将确保关闭数据库连接。那么我的理解是否有效?

  2. 在我的例子中,我是否需要在我的 APIRepository() 中有一个 Dispose 方法,正如我提到的,这个存储库只有 WebClient() 调用来与第 3 方应用程序集成,并且它会将对象或简单字符串返回给操作方法。??

  3. 需要处理哪些操作?据我所知,在我的存储库类中调用 my.Dispose(); 将确保关闭数据库连接。但是是否还有其他需要处理的操作?例如通过我的操作方法启动 WebClient() 或返回 JSON?

  4. 除了返回 View 之外还有哪些操作会调用我的 Controller 类中的 Dispose 方法?

最佳答案

如果您使用依赖注入(inject),那么没有一个类会负责创建处理注入(inject)其中的依赖。

取而代之的是:

public class ServerController : Controller
{
    Repository repository = new Repository();
    APIRepository APIrepository = new APIRepository();

ServerController 创建它的依赖项并负责处理它们的地方,这个:

public class ServerController : Controller
{
    private readonly Repository _repository;
    private readonly APIRepository _apiRepository;

    public ServerController(Repository repository, APIRepository apiRepository)
    {
        _repository = repository;
        _apiRepository = apiRepository;
    }

现在创建 ServerController 实例的任何东西都负责实例化这些对象并销毁它们。 “无论什么”通常是您的依赖注入(inject)容器,例如 Windsor 或 Unity。或者甚至更好,在 ASP.NET Core 中它是内置的,您不需要添加单独的容器。

简短版本:容器创建您的存储库,如果需要处置它,它会处置它。您甚至可以指定有关某些依赖项生命周期的详细信息。如果它不是一次性的并且可以重复使用,那么它就是一个单例。您可以在每次需要时创建和处置一个新实例。或者您可以创建一个与给定 Web 请求相关联的新实例。该请求使用它,然后将其处理掉。

关于c# - Dispose() 如何在我的 Controller 和 Repository 类中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213450/

相关文章:

c# - 异步使用 NamedPipeServerStream 和 NamedPipeClientStream

asp.net - GridView 中具有列跨度的标题列

c# - 为什么将委托(delegate)声明为静态会导致编译器错误 "The modifier ' static' is not valid for this item”?

c# - 树结构的序列化/反序列化

c# - 如何传递毫米中可用的矩形大小以使用 iTextsharp 创建文档

javascript - 检查 GridView 中的复选框是否被选中

c# - global_asax 在命名空间 ASP 中不存在

c# - 如何在 C# 中安全地将 System.Object 转换为 `bool`?

c# - 使用围绕对话框形式的语句来确保垃圾收集

C#简单开源应用