ADO.NET是否会过度使用IDisposable? [英] Does ADO.NET go overboard with IDisposable?

查看:92
本文介绍了ADO.NET是否会过度使用IDisposable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET刚问世时,我就是抱怨.NET缺乏确定性终结(类析构函数以不可预测的时间表被调用)的人之一。微软当时提出的折衷方案是 using 语句。

When .NET first came out, I was one of many who complained about .NET's lack of deterministic finalization (class destructors being called on an unpredictable schedule). The compromise Microsoft came up with at the time was the using statement.

虽然不完美,但我认为使用使用对于确保及时清理非托管资源很重要。

Although not perfect, I think using using is important to ensuring that unmanaged resources are cleaned up in a timely manner.

不过,我在写一些ADO.NET代码,并注意到几乎每个类都实现 IDisposable 。这样会导致代码如下所示。

However, I'm writing some ADO.NET code and noticed that almost every class implements IDisposable. That leads to code that looks like this.

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter))
using (DataSet dataset = new DataSet())
{
    command.Parameters.AddWithValue("@FirstValue", 2);
    command.Parameters.AddWithValue("@SecondValue", 3);

    adapter.Fill(dataset);

    DataTable table = dataset.Tables[0];
    foreach (DataRow row in table.Rows) // search whole table
    {
        if ((int)row["Id"] == 4)
        {
            row["Value2"] = 12345;
        }
        else if ((int)row["Id"] == 5)
        {
            row.Delete();
        }
    }
    adapter.Update(table);
}

强烈怀疑我不需要全部使用语句的。但是,如果不详细了解每个班级的代码,很难确定我可以忽略哪些课程。结果有点丑陋,并且有损于我代码中的主要逻辑。

I strongly suspect I do not need all of those using statements. But without understanding the code for each class in some detail, it's hard to be certain which ones I can leave out. The results is kind of ugly and detracts from the main logic in my code.

有人知道为什么所有这些类都需要实现 IDisposable code>? (Microsoft有许多代码在线示例),而不必担心要处理其中的许多对象。)其他开发人员是否正在为所有这些对象使用语句编写语句?而且,如果没有,那么您如何确定哪些功能可以做到呢?

Does anyone know why all these classes need to implement IDisposable? (Microsoft has many code examples online that don't worry about disposing many of these objects.) Are other developers writing using statements for all of them? And, if not, how do you decide which ones can do without?

推荐答案

这里的问题之一就是ADO。 NET是一种抽象的提供程序模型。我们不知道在处置方面将需要什么特定的实现(特定的ADO.NET提供程序)。当然,我们可以合理地假设需要处理连接和事务,但是命令呢?也许。读者?可能并非唯一,因为命令标志选项之一使您可以将连接的生存期与读取器相关联(因此,连接在读取器关闭时将关闭,从逻辑上讲应扩展为废弃)。

One part of the problem here is that ADO.NET is an abstract provider model. We don't know what a specific implementation (a specific ADO.NET provider) will need with regards to disposal. Sure, we can reasonably assume the connection and transaction need to be disposed, but the command? Maybe. Reader? Probably, not least because one of the command-flags options allows you to associate a connection's lifetime to the reader (so the connection closes when the reader does, which should logically extend to disposal).

所以总的来说,我认为这可能还不错。

So overall, I think it is probably fine.

在大多数情况下,人们不会手动使用ADO.NET和任何ORM工具(或微型ORM工具,例如 Dapper)将为您提供正确的 ,而无需担心。

Most of the time, people aren't messing with ADO.NET by hand, and any ORM tools (or micro-ORM tools, such as "Dapper") will get this right for you without you having to worry about it.

我会公开承认,在少数情况下使用 DataTable (严重的是,这是2018年-不应该是您用于表示数据的默认模型,除了某些特殊情况外):我尚未处理它们。那有点没有道理:)

I will openly confess that on the few occasions when I've used DataTable (seriously, it is 2018 - that shouldn't be your default model for representing data, except for some niche scenarios): I haven't disposed them. That one kinda makes no sense :)

这篇关于ADO.NET是否会过度使用IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆