简体   繁体   English

应该使用“使用”声明吗?

[英]Should dapper use a “using” statement?

I have seen examples where someone is doing: 我见过有人在做的例子:

IDbConnection db = new MySqlConnection(conn);

var people = db.Query<People>("SELECT * FROM PEOPLE").ToList();

or is the above a bad practice and should all queries be put in using statements like so: 或者上面是一个不好的做法,并且所有查询都应该使用如下语句:

using (var db = new MySqlConnection(conn))
{
var people = db.Query<People>("SELECT * FROM PEOPLE").ToList();
}

As others have correctly noted, the best practice in general is to use using any time an object implements IDisposable and you know that the lifetime of the object is going to be short -- that is, not longer than the duration of the current method. 正如其他人已经正确指出的那样,一般的最佳实践是在对象实现IDisposable任何时候using并且您知道对象的生命周期将会很短 - 也就是说,不会超过当前方法的持续时间。 Doing so ensures that scarce operating system resources are cleaned up in a timely manner. 这样做可确保及时清理稀缺的操作系统资源。 Even if an object's disposal is "backstopped" by its finalizer, you don't want to be in a situation where, say, you have a lock on a file or database or something that is not going to be released until the finalizer runs several billion nanoseconds from now. 即使对象的处理由其终结器“支持”,您也不希望处于这样的情况:例如,您对文件或数据库有锁定,或者在终结器运行几次之前不会释放从现在起十亿纳秒。

However I would temper that advice by saying that there are a small number of types that implement IDisposable for reasons other than timely disposal of unmanaged resources. 但是,我会通过说除了及时处理非托管资源之外的其他原因而实施IDisposable的少数类型,这样可以缓解这一建议。 In some very specific cases you can safely skip the using . 在某些非常具体的情况下,您可以安全地跳过using However, it is almost never wrong to use using , even when it is not strictly speaking necessary , so my advice to you is to err on the side of caution and over-use, rather than under-use using . 然而,这几乎是没有任何错误使用using ,即使它不是严格意义上讲必要的 ,所以我给你的建议是要慎之又慎的侧面和过度使用,而不是在使用using

Dapper doesn't have a view on this; Dapper对此没有看法; what you are using here is the database connection. 你在这里using是数据库连接。 If you have finished with the connection: you have finished with the connection. 如果您已完成连接:您已完成连接。 Basically, yes, you should probably be using using . 基本上,是的,您可能应该using

The main purpose use of using statments is to release unmanaged resources.When an object is no longer used The garbage collector automatically releases the memory allocated to it but sometimes the garbage collector does not release resources such as files, streams or db connection like in your example. 使用语句的主要目的是释放非托管资源。当不再使用对象时垃圾收集器会自动释放分配给它的内存,但有时垃圾收集器不会释放文件,流或数据库连接等资源。例。

Think of it of a way to explicitly dispose objects rather than leave it up to the compiler so you can say it's better practice. 可以想象它是一种显式处理对象的方法,而不是将它留给编译器,因此你可以说它是更好的实践。

In my experience with Sql Server and Oracle (using the ODP.Net drivers and the MS drivers), you need to use using around Connections, Commands and Transactions or you will soon exhaust your connection pool if you do anything but the simplest database interaction in a production environment (the connection pool is typically ~50 - 200 connections). 根据我使用Sql Server和Oracle的经验(使用ODP.Net驱动程序和MS驱动程序),您需要使用Connections,Commands和Transactions,否则如果除了最简单的数据库交互之外,您将很快耗尽连接池。生产环境(连接池通常约为50 - 200个连接)。

You may not notice the behaviour in a development environment because debug = a lot of restarts, which clears the pool. 您可能不会注意到开发环境中的行为,因为debug =大量重新启动会清除池。

As Eric Lippert above said, it is good practice in general to use using around IDisposable objects. 正如上面的Eric Lippert所说,使用IDisposable对象是一般的好习惯。

In practice, you can skip "using" on Parameters for SqlServer and Oracle. 实际上,您可以在SqlServer和Oracle的参数上跳过“使用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM