本文介绍了Dapper源代码-这样可以正确处理我的连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看Dappers QueryAsync方法的源代码

Looking at the source code for Dappers QueryAsync method

 private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, Type effectiveType, CommandDefinition command)
    {

        using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
        {
            DbDataReader reader = null;
            try
            {
                if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);

                var func = tuple.Func;

                if (command.Buffered)
                {
                    var buffer = new List<T>();
                    var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
                    while (await reader.ReadAsync(cancel).ConfigureAwait(false))
                    {
                        object val = func(reader);
                        if (val == null || val is T)
                        {
                            buffer.Add((T)val);
                        }
                        else
                        {
                            buffer.Add((T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture));
                        }
                    }
                    while (await reader.NextResultAsync(cancel).ConfigureAwait(false)) { /* ignore subsequent result sets */ }
                    command.OnCompleted();
                    return buffer;
                }
                else
                {
                    // can't use ReadAsync / cancellation; but this will have to do
                    wasClosed = false; // don't close if handing back an open reader; rely on the command-behavior
                    var deferred = ExecuteReaderSync<T>(reader, func, command.Parameters);
                    reader = null; // to prevent it being disposed before the caller gets to see it
                    return deferred;
                }
            }
            finally
            {
                using (reader) { /* dispose if non-null */ }
                if (wasClosed) cnn.Close();
            }
        }
    }

请注意以下这一行(413) :

Note this line (413):

using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))

我的问题是,我的连接是否可以正确处理,而无需将我的代码包装在using块中?

My question is, will my connection be disposed of correctly without me having to wrap my code calling in to this in a using block?

推荐答案

请注意您发布的代码中的以下行:

Note following line in the code you post:

private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn,....

显示 QueryAsync IDbConnection 的扩展方法,这意味着连接实例是在您的代码中的某个地方创建的

That shows QueryAsync is an extension method of IDbConnection. That means, instance of connection is created in your code somewhere.

如所述,有两种方法可以管理与Dapper的连接:

As mentioned here, there are two ways to manage connection with Dapper:


  • 完全管理自己:
    在这里,您完全负责开关闭连接。就像在使用ADO.NET时对待连接一样。

  • Fully manage yourself:Here, you are fully responsible for opening and closing connection. This is just like how you treat connection while working with ADO.NET.

允许Dapper管理它:
Dapper会自动为您打开连接(如果未打开)并关闭(如果它是Dapper打开的)。

Allow Dapper to manage it:Dapper automatically opens the connection (if it was not opened) and closes it (if it was opened by Dapper) for you.

考虑到这一点,剩下的唯一选择就是打开/关闭代码中的连接或允许Dapper为您完成此操作。 apper根本不会干涉。

Considering this, only choice remain here is to open/close connection in your code or allow Dapper to do this for you. If you are doing this yourself then Dapper does not interfere at all.

如果您想让Dapper为您处理打开/关闭连接,并担心是否会正确关闭它,然后是,它将正确关闭。

在您的帖子中查找以下代码:

Find following code in your post:

if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);
....
if (wasClosed) cnn.Close();

Dapper保持状态/标志处于 wasClosed 如果连接是由Dapper打开的,则为变量。您可以在代码中看到该连接最后也已正确关闭。此外,您可以进一步检查Dapper源代码,以查看如何用多种方法处理此问题。专门检查 SqlMapper.Async.cs SqlMapper.cs 文件。

Dapper maintains the state/flag in wasClosed variable if connection was opened by Dapper. You can see in your code that the connection is also properly closed at the end. Further, you can further check the Dapper source code to see how the this is handled in multiple methods. Specially check SqlMapper.Async.cs and SqlMapper.cs files.

现在,这一切都与打开/关闭有关。 处置怎么样?以下是Marc Gravell在:

Now, this is all about open/close. What about Dispose? Following is what Marc Gravell says in one of the comment for this answer: https://stackoverflow.com/a/12629170/5779732

所以,如果您真的想 Dispose 而不是仅仅打开/关闭连接,最好将其包装起来在代码中的 using 块中,并将打开的连接传递给Dapper。如评论中所述,帖子讨论了 Dispose 关闭

So, if you really want to Dispose the connection instead of just open/close, better you wrap it in using block in your code and pass open connection to Dapper. As mentioned in the comment, this post discusses difference between Dispose and Close.

这篇关于Dapper源代码-这样可以正确处理我的连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:14