某天在园里看到
Dapper.net , 终于发现找到我一致梦寐的 OM (Object Mapping) (某人,你知道的....).
从Java到.Net , 从spring.net 0.6 到 spring.net 1.3.2 , 一直在使用Hibernate, 但这个 ORM - Relation 处理却一直被我唾弃.
如今终于如愿以偿, 立即开始对它进行简单的封装.
功能扩展:
1.ValueType类型隐式转换, 如某一个numeric 在Oracle中是Int64,但Sql Server中确是 decimal, 需要在DynamicMethod中加入隐式转换.
2.Clob大字段支持. 靠Dapper的 XML类型是不行的.
3.参数,结果集 弱类型转换,如 IDictionary 作为请求参数 或DTO
4.增加非泛型接口查询
5.存储过程返回结果集时,处理输出参数 (Oracle Cursor处理及output参数读取)
6.简单sql,自动组装分页语句处理 (top order,rownum, skip/take)
7.根据DbProvider自动替换参数prefix
8. 实现SQL,SP调用完全透明兼容Oracle/SQL Server
9. 对象转SQL CRUD语句,以及 partial update 语句自动生成.
10. 支持OSIV模式的Connection以及Transaction管理 (模仿Spring.Net SessionScope)
-----------
//
Delete a user
MyUser u = new MyUser() { Id = " testId " };
dao.Execute(QueryInfo.GetSQLUpdate( typeof(MyUser)),u);
MyUser u = new MyUser() { Id = " testId " };
dao.Execute(QueryInfo.GetSQLUpdate( typeof(MyUser)),u);
1
//
SP返回2个结果集10行 Users,Offices,同时返回两表总函数
2 QueryInfo info = new QueryInfo();
3 info.NamedQuery = " TEST_DAPPER_USERS ";
4 info.AddParam( " UserCode ", " y% ");
5 info.AddParam( " out_TotalCount ", 1);
6 info.AddParam( " out_TotalCount2 ", 1);
7 info.AddParam( " out_cursor1 ", " CURSOR ");
8 info.AddParam( " out_cursor2 ", " CURSOR ");
9
10 var reader = dao.QueryMultiple(info);
11
12 var user = reader.Read<MyUser>();
13 var office = reader.Read<MyOffice>();
14
15 object i = reader.OutputParams[ " out_TotalCount "];
16 object j = reader.OutputParams[ " out_TotalCount2 "];
2 QueryInfo info = new QueryInfo();
3 info.NamedQuery = " TEST_DAPPER_USERS ";
4 info.AddParam( " UserCode ", " y% ");
5 info.AddParam( " out_TotalCount ", 1);
6 info.AddParam( " out_TotalCount2 ", 1);
7 info.AddParam( " out_cursor1 ", " CURSOR ");
8 info.AddParam( " out_cursor2 ", " CURSOR ");
9
10 var reader = dao.QueryMultiple(info);
11
12 var user = reader.Read<MyUser>();
13 var office = reader.Read<MyOffice>();
14
15 object i = reader.OutputParams[ " out_TotalCount "];
16 object j = reader.OutputParams[ " out_TotalCount2 "];
IDictionary<
string,
object> param =
new Dictionary<
string,
object>();
param.Add( " UserCode_RLK ", " chen "); // ==> and UserCode like :UserCode /'chen%'
param.Add( " CreatedOn_GEQ ", " 2011-09-01 "); // ==> and CreatedOn >=:CreatedOn
param.Add( " IsActive ", 1); // ==> and IsActive=:IsActive
QueryInfo info = new QueryInfo( " SYS_USER u ");
info.OrderBy.Add( " Name ");
info.AddParam(param);
info.TotalCount = 1; // 分页请求
info.PageSize = 15;
info.StartRecord = 10;
info.MappingType= typeof(MyUser);
info = dao.Query(info);
// info.List=IList[10] Results of(MyUser)
// info.TotalCount= counts of the table
param.Add( " UserCode_RLK ", " chen "); // ==> and UserCode like :UserCode /'chen%'
param.Add( " CreatedOn_GEQ ", " 2011-09-01 "); // ==> and CreatedOn >=:CreatedOn
param.Add( " IsActive ", 1); // ==> and IsActive=:IsActive
QueryInfo info = new QueryInfo( " SYS_USER u ");
info.OrderBy.Add( " Name ");
info.AddParam(param);
info.TotalCount = 1; // 分页请求
info.PageSize = 15;
info.StartRecord = 10;
info.MappingType= typeof(MyUser);
info = dao.Query(info);
// info.List=IList[10] Results of(MyUser)
// info.TotalCount= counts of the table
------------
Dapper实现非常不错,但结合我的应用场景有些缺陷, 而且静态方法实现导致扩展代码难免侵入,
可能造成后期Dapper同步升级一些隐患. 目前靠10个 NUnit用例保障.
有兴趣用Dapper的朋友,对扩展,应用有什么疑问我乐于帮忙~~