在 mvc3 控制器上实现 Idisposable

发布于 2024-11-30 17:44:42 字数 1108 浏览 5 评论 0原文

我有一个为 EF 实例化数据库上下文的控制器。 (我确信大多数都没有实现存储库模式。)

当我对项目进行代码分析时,它建议实现 IDisposable,因此我编写了以下代码。

    #region Implementation of IDisposable
    public void Dispose()
    {
        Console.WriteLine("Dispose");
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    // The bulk of the clean-up code is implemented in Dispose(bool)
    protected override void Dispose(bool disposing)
    {
        Console.WriteLine("Dispose(disposing)");
        if (disposing)
        {
            // free managed resources
            if (_dataService != null)
            {
                ((IDisposable)_dataService).Dispose();
                _dataService = null;
            }
            // free managed resources
            if (_db != null)
            {
                ((IDisposable)_db).Dispose();
                _db = null;
            }

        }
        base.Dispose(disposing);

    }
    #endregion

我也尝试过做。

protected new virtual void Dispose(bool disposing)

但我的 console.writeline 语句永远不会执行。我做错了什么?为什么我的控制器上没有调用 Dispose()?

I have a controller that instantiates a database context for EF. (As I'm sure most that aren't implementing the repository pattern do.)

When I ran code analysis on my project it recommended implementing IDisposable so I wrote the following code.

    #region Implementation of IDisposable
    public void Dispose()
    {
        Console.WriteLine("Dispose");
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    // The bulk of the clean-up code is implemented in Dispose(bool)
    protected override void Dispose(bool disposing)
    {
        Console.WriteLine("Dispose(disposing)");
        if (disposing)
        {
            // free managed resources
            if (_dataService != null)
            {
                ((IDisposable)_dataService).Dispose();
                _dataService = null;
            }
            // free managed resources
            if (_db != null)
            {
                ((IDisposable)_db).Dispose();
                _db = null;
            }

        }
        base.Dispose(disposing);

    }
    #endregion

I've also tried doing.

protected new virtual void Dispose(bool disposing)

But my console.writeline statements never execute. What am I doing wrong? Why isn't Dispose() being called on my controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-12-07 17:44:42

你没有做错任何事。您会得到建议,因为您在实现 IDisposable 的类 (cotnroller) 中保留了一些字段。该框架将调用“重载”版本,因此只需将所有代码移到那里即可。

MSDN 上的详细信息

顺便说一句,您将看不到 console-writeline - 使用改为 System.Diagnostic.Debug.WriteLine

you don't do anything wrong. You get the recommentation because you hold some fields in your class (cotnroller) that implements IDisposable. The framework will call the "overload"-version so just move all your code in there.

Details on MSDN

BTW you won't see the console-writeline - use System.Diagnostic.Debug.WriteLine instead

や三分注定 2024-12-07 17:44:42

就我个人而言,我认为在这种情况下最好的做法是将数据库上下文从控制器中分离出来,并将其移至服务层。您可以使该类实现 IDisposable 并将对服务层类的引用传递给控制器​​以使用它。

这样做时,您将创建一个不依赖于数据库的控制器。术语瘦控制器、胖模型适用于此。在这种情况下,服务层将充当模型。您还可以彼此隔离地对服务层和控制器(如果需要)进行单元测试

Personally I think the best thing to do in this situation is to split out the database context away from the controller and move it to a service layer. You can them make that class implement IDisposable and pass the reference to the service layer class to the controller to use it

In doing this you will create a controller that is not dependant on the database. The term skinny controllers, fat models applies here. the service layer will act as a model in this instance. You can also unit test the service layer and controller (if you want) in isolation from each other

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文