Dapper扩展之~~~Dapper.Contrib

平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill

上一篇文章:Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

 官方地址:https://github.com/StackExchange/dapper-dot-net/tree/master/Dapper.Contrib

实战案例:https://github.com/dunitian/LoTCode/tree/master/PawChina(更新ing)

注意点:Model里面的Table和Key是Dapper.Contrib.Extensions命名空间下的~~~~如果不是~~请看下篇文章(点我)

 

用法很简单,贴一下帮助类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
///
     /// 扩展方法
     ///
     public  abstract  partial  class  DapperDataAsync
     {
         #region 查询系
         ///
         /// 获取Model-Key为int类型
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task GetAsync( int  id, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.GetAsync(id, transaction, commandTimeout);
             }
         }
         ///
         /// 获取Model-Key为long类型
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task GetAsync( long  id, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.GetAsync(id, transaction, commandTimeout);
             }
         }
         ///
         /// 获取Model-Key为Guid类型
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task GetAsync(System.Guid id, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.GetAsync(id, transaction, commandTimeout);
             }
         }
         ///
         /// 获取Model-Key为string类型
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task GetAsync( string  id, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.GetAsync(id, transaction, commandTimeout);
             }
         }
         ///
         /// 获取Model集合(没有Where条件)
         ///
         ///
         ///
         public  static  async Task> GetAllAsync()  where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.GetAllAsync();
             }
         }
         #endregion
 
         #region 增删改
         ///
         /// 插入一个Model
         ///
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task< int > InsertAsync(T model, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 return  await conn.InsertAsync(model, transaction, commandTimeout);
             }
         }
 
         ///
         /// 更新一个Model
         ///
         ///
         ///
         ///
         ///
         ///
         ///
         public  static  async Task UpdateAsync(T model, IDbTransaction transaction =  null int ? commandTimeout =  null where  T :  class new ()
         {
             using  ( var  conn = ConnFactory.GetConnection())
             {
                 bool  b = await conn.UpdateAsync(model, transaction, commandTimeout);
                 if  (b) {  return  model; }
                 else  return  null ; }
             }
         }
         #endregion
 
         #region 分页查询
         ///
         /// 分页查询(为什么不用out,请参考:http://www.cnblogs.com/dunitian/p/5556909.html)
         ///
         /// 查询语句
         /// 动态参数
         /// total语句
         /// Total动态参数
         ///
         public  static  async Task< string > PageLoadAsync( string  sql,  object  p =  null string  sqlTotal =  "" object  p2 =  null )
         {
             var  rows = await QueryAsync(sql.ToString(), p);
             var  total = rows.Count();
             if  (!sqlTotal.IsNullOrWhiteSpace()) { total = await ExecuteScalarAsync< int >(sqlTotal, p2); }
             return  new  { rows = rows, total = total }.ObjectToJson();
         }
         #endregion
     }

 博客地址:http://dnt.dkill.net


本文转自毒逆天博客园博客,原文链接:http://www.cnblogs.com/dunitian/p/5710382.html,如需转载请自行联系原作者

你可能感兴趣的:(Dapper扩展之~~~Dapper.Contrib)