Dapper源码讲解

Nuget下载dapper的程序集,浏览公开的方法,如下图所示

 

 

 

 

主要讲解:Execute方法的调用过程,

public static int Execute(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

 

此方法代码:

 

 

内部调用了ExecuteImpl方法

 

 

上图,数字修正为数组

 

下面讲解ExecuteCommand方法

 

 

 

上面的代码中一共出现两个类Identity(标识),CacheInfo(缓存信息)

 先说Identity这个类,是用来对CommandText,CommandType,cnn,param的一个封装,用于唯一标识这些对象。为缓存机制,提供一个唯一标识。

identity = new Identity(command.CommandText, command.CommandType, cnn, null, param.GetType(), null);

 

那么我们再来看这个类CacheInfo,用identity,param,bool参数来构造。

info = GetCacheInfo(identity, param, command.AddToCache);

 

下面看它的构造函数:

 

 

 

 

 我们接下来,查看步骤3中的圈着的方法,是用来生成一个无返回值的委托,参数是ICommand和object

 

posted @ 2017-06-21 22:25  苏子轩2015  阅读(552)  评论(0编辑  收藏  举报