Dapper.net 在Parameterized时对于String的扩展(转)
虽然Dapper通过提供的DbString本身支持对于String的指定Parameterized,但这方法明显不够,当Insert时,我们更希望是把一个Poco直接传递过去,而不是来new一个匿名函数,对于string类型的属性,转化成DbString,然后一个一个的属性再写一遍,这多苦逼
通过代码,可以看到有这么一段方法
public static Action<IDbCommand, object> CreateParamInfoGenerator(Identity identity, bool checkForDuplicates, bool removeUnused)
这段代码就是用来构建Param参数的,内部通过Emit来实现,在里面可以找到遍历属性的代码,其内部有一些判断,这些就是可以直接增加代码来控制生成何种Param的地方,具体是两个地方
if (dbType != DbType.Time)
{
il.Emit(OpCodes.Dup);// stack is now [parameters] [[parameters]] [parameter] [parameter]
EmitInt32(il, (int)dbType);// stack is now [parameters] [[parameters]] [parameter] [parameter] [db-type]
il.EmitCall(OpCodes.Callvirt, typeof(IDataParameter).GetProperty("DbType").GetSetMethod(), null);// stack is now [parameters] [[parameters]] [parameter]
}
if (prop.PropertyType == typeof(string))
{
il.Emit(OpCodes.Dup); // [string] [string]
il.EmitCall(OpCodes.Callvirt, typeof(string).GetProperty("Length").GetGetMethod(), null); // [string] [length]
EmitInt32(il, 4000); // [string] [length] [4000]
il.Emit(OpCodes.Cgt); // [string] [0 or 1]
Label isLong = il.DefineLabel(), lenDone = il.DefineLabel();
il.Emit(OpCodes.Brtrue_S, isLong);
EmitInt32(il, 4000); // [string] [4000]
il.Emit(OpCodes.Br_S, lenDone);
il.MarkLabel(isLong);
EmitInt32(il, -1); // [string] [-1]
il.MarkLabel(lenDone);
il.Emit(OpCodes.Stloc_1); // [string]
}
我们只要修改第一段代码部分的
EmitInt32(il, (int)dbType);
通过修改(int)dbType来控制AnsiString、AnsiStringFixedLength、String、StringFixedLength
通过修改第二段代码部分的两个4000来控制字符串Param长度(其实修改第二个4000就能达到目标,但为啥还要第一个,整段Emit代码又是什么意思。。。完全没看懂!!)
具体怎么改呢
1、可以通过Attribute,这个方法比较简单,但坏处就是相当于破坏了Dapper不需要修改原代码的事实,如果Orm每次通过工具生成一次,就要修改一次Poco,当然你也可以修改Orm的生成工具,为属性加上相应的Length限制,好处就是可以直接应用Dapper自己提供的SqlMapperExtensions,而且相对一致,都是通过Attribute进行控制
2、通过Mapping,这个就是要额外增加控制的类,下面是一个简单的类
public class DapperStringParameterized
{
private Dictionary<string, KeyValuePair<DbType, int>> _dic = new Dictionary<string, KeyValuePair<DbType, int>>();
/// <summary>
/// 添加字符串参数化映射
/// </summary>
/// <param name="name">属性名</param>
/// <param name="type">必须为AnsiString、AnsiStringFixedLength、String、StringFixedLength</param>
/// <param name="len">必须为1~8000</param>
public virtual void Add(string name, DbType type = DbType.AnsiString, int len = 50)
{
if (len <= 0 || len > 8000)
{//长度范围1~8000,此处暂时对应sql,如果其它关系型数据库长度范围与此不一致,可继承修改
throw new ArgumentException("The param len's value must between 1 and 8000.");
}
if (type != DbType.AnsiString && type != DbType.AnsiStringFixedLength && type != DbType.String && type != DbType.StringFixedLength)
{
return;
}
if (!string.IsNullOrEmpty(name))
{
if (this._dic.ContainsKey(name))
{
throw new ArgumentException(string.Format("The param name '{0}' has aready existed!", name));
}
else
{
this._dic.Add(name, new KeyValuePair<DbType, int>(type, len));
}
}
}
public void Remove(string name)
{
if (!string.IsNullOrWhiteSpace(name))
{
if (this._dic.ContainsKey(name))
{
this._dic.Remove(name);
}
}
}
public KeyValuePair<DbType, int>? GetParameterizedData(string name)
{
if (!string.IsNullOrWhiteSpace(name) && this._dic.ContainsKey(name))
{
return this._dic[name];
}
return null;
}
} public class DapperStringParameterizedManager
{
private static readonly DapperStringParameterizedManager manager = new DapperStringParameterizedManager();
private static Dictionary<Type, DapperStringParameterized> dic = new Dictionary<Type, DapperStringParameterized>();
private static object locObj = new object();
private DapperStringParameterizedManager() { } public static DapperStringParameterizedManager Instance
{
get { return manager; }
}
/// <summary>
/// 添加映射关系
/// </summary>
/// <returns></returns>
public void AddMapping<T>(DapperStringParameterized mapping)
where T : class
{
if (mapping != null)
{
lock (locObj)
{
DapperStringParameterized tmpmapping = this.GetMapping(typeof(T));
if (tmpmapping == null)
{
dic.Add(typeof(T), mapping);
}
else
{
throw new ArgumentException(string.Format("The POCO Mapping {0} has aready existed!", typeof(T)));
}
}
}
} public DapperStringParameterized GetMapping(Type type)
{
if (type != null && dic.ContainsKey(type))
{
return dic[type];
}
return null;
}
}
使用时就是在CreateParamInfoGenerator方法中,foreach (var prop in props)之前添加代码
DapperStringParameterized dsp = DapperStringParameterizedManager.Instance.GetMapping(identity.type);
在获取DbType的地方增加代码
DbType dbType = LookupDbType(prop.PropertyType, prop.Name);
KeyValuePair<DbType, int>? kvp = null;
if (dbType == DbType.String && dsp != null)//默认所有字符串在Dapper中被param成 DbType.String
{
kvp = dsp.GetParameterizedData(prop.Name);
}
第一段代码部分修改为
if (dbType != DbType.Time) // https://connect.microsoft.com/VisualStudio/feedback/details/381934/sqlparameter-dbtype-dbtype-time-sets-the-parameter-to-sqldbtype-datetime-instead-of-sqldbtype-time
{
//string parameter extensions 对于字符串参数化的扩展
int dbTypeValue = (int)dbType;
if (kvp.HasValue)
{
dbTypeValue = (int)kvp.Value.Key;
} il.Emit(OpCodes.Dup);// stack is now [parameters] [[parameters]] [parameter] [parameter]
EmitInt32(il, dbTypeValue);// stack is now [parameters] [[parameters]] [parameter] [parameter] [db-type] il.EmitCall(OpCodes.Callvirt, typeof(IDataParameter).GetProperty("DbType").GetSetMethod(), null);// stack is now [parameters] [[parameters]] [parameter]
}
因为我们设定了字符串允许的最大长度,所以第二部分判断大小的代码直接注销,然后将下面另一段判断string的代码
if (prop.PropertyType == typeof(string))
{
var endOfSize = il.DefineLabel();
// don't set if 0
il.Emit(OpCodes.Ldloc_1); // [parameters] [[parameters]] [parameter] [size]
il.Emit(OpCodes.Brfalse_S, endOfSize); // [parameters] [[parameters]] [parameter] il.Emit(OpCodes.Dup);// stack is now [parameters] [[parameters]] [parameter] [parameter]
il.Emit(OpCodes.Ldloc_1); // stack is now [parameters] [[parameters]] [parameter] [parameter] [size]
il.EmitCall(OpCodes.Callvirt, typeof(IDbDataParameter).GetProperty("Size").GetSetMethod(), null); // stack is now [parameters] [[parameters]] [parameter] il.MarkLabel(endOfSize);
}
修改为
if (prop.PropertyType == typeof(string) && kvp.HasValue)
{
il.Emit(OpCodes.Dup);
EmitInt32(il, kvp.Value.Value);
il.EmitCall(OpCodes.Callvirt, typeof(IDbDataParameter).GetProperty("Size").GetSetMethod(), null); // stack is now [parameters] [[parameters]] [parameter]
}
这样如果有设定字符串长度,则此部分代码会进行size设定,否则不设定
实际用的地方只要在static构造函数中添加相应的初始化设定就可以了,建议将此部分代码写在相应的Repository部分,如果是三层则写在DAL部分,比如
static _Default()
{
DapperStringParameterized dsp = new DapperStringParameterized(); DapperStringParameterizedManager manager = DapperStringParameterizedManager.Instance;
manager.AddMapping<Customer>(dsp); dsp.Add("UserName", DbType.String, 20);
dsp.Add("Contact", DbType.String, 25);
}
好吧。。这样子做了之后只针对Query<T>起了作用,对于Execute没起作用,因为这个方法没指定类型T,在创建Identity时它直接将Type设为了null,那就添加ExecuteQ<T>方法,因为指定了T,所以将Execute的代码复制一份,然后将new Identity的地方将cnn后面的第一个null改为typeof(T)就可以了
Dapper.net 在Parameterized时对于String的扩展(转)的更多相关文章
- redis存json数据时选择string还是hash
redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...
- ES6新增语法和内置对象(let,const, Array/String/Set 扩展方法(解构赋值,箭头函数,剩余参数))
1.let ES6中新增的用于声明变量的关键字. let 声明的变量只在所处于的块级有效. 注意:使用 let 关键字声明的变量才具有块级作用域,var 关键字是不具备这个特点的. 1. 防止循环变量 ...
- 关于C#不同位数相与或,或赋值时,隐藏位数扩展该留意的问题
__int64 a; char b; a = b; a |= b; 如上情况,当b的最高位为1时,即b=0x80(或更大)时,b在扩展成64过程中会将最高位向高位扩展变成0xfffffffffffff ...
- ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展
关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...
- 第200天:js---常用string原型扩展
一.常用string原型扩展 1.在字符串末尾追加字符串 /** 在字符串末尾追加字符串 **/ String.prototype.append = function (str) { return t ...
- Dapper官方库 在guid和string互转的问题
之前在和老何谈论他的开源项目Util中使用MySql的过程中发现了官方dapper在转换guid到string时候的一个错误 Error parsing column 0 (ID=6c2adb93-d ...
- Dapper多表查询时子表字段为空
最近在学习使用Dapper时百度了一篇详细使用教程,在做到多表查询的时候,出现如下情况. 使用的SQL如下, SELECT * FROM [Student] AS A INNER JOIN [Juni ...
- sqoop导入时删除string类型字段的特殊字符
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sheismylife/article/details/29384357 假设你指定了\n为sqoop ...
- dapper 写查询sql 时,多条件参数操作方法
var args = new DynamicParameters(new {}); if (obj.orderId != null) { sb.Append(" AND OrderId = ...
随机推荐
- LINQ——语言级集成查询入门指南(1)
本文主要是对语言级集成查询或简称为LINQ做一个介绍,包括LINQ是什么,不是什么,并对它在语言特性方面做一个简短的回顾,然后举一些使用LINQ的实际例子进行说明. 语言级集成查询是什么? 在我过去写 ...
- lintcode 中等题:A + B Problem A + B 问题
题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...
- MIT算法导论——第四讲.Quicksort
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- iOS开发--计时器-NSTimer与CADisplayLink
如果程序要让某个方法重复执行,可以借助定时器来完成.CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器,NSTimer的精确度低了点,比如NSTimer的触发时间 ...
- Photoshop:建议设置
一.新建文档设置: 二.对齐设置 菜单->视图->对齐->全部 使用图层.形状等操作时自动对齐网格,画矢量图不怕模糊边缘,确保每个像素保持清晰. 三.首选项设置 关掉"启用 ...
- 计算机视觉和人工智能的状态:我们已经走得很远了 The state of Computer Vision and AI: we are really, really far away.
The picture above is funny. But for me it is also one of those examples that make me sad about the o ...
- Lucas定理学习小记
(1)Lucas定理:p为素数,则有: (2)证明: n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 = [n/p]*p+a0,m=[m/p]*p+b0其次,我们 ...
- CSS+DIV问题!DIV的最小高度问题!
DIV层的最小高度问题!就是一个DIV有个最小高度,但是如果DIV层中的内容很多,DIV的高度会根据内容而进行拉长!要求IE6.IE7还有firefox都要兼容!我试了很多网上的方法都不好用!请测试后 ...
- Ubuntu 64位系统安装StarUML之最佳实践
preview 相信很多使用Ubuntu的哥们在安装StarUML或者其他软件时都会遇到要求libgcrypt11的依赖.而遗憾的时,这个东西很多人根本找不到. 我将它分享到百度网盘,mark. 一. ...
- MemSQL start[c]up Round 2 - online version(DP)
只有小写字母 那>=2600的直接找单字母串长度大于等于100的就可以了 <2600 的dp找最长回文串 #include <iostream> #include<cst ...