Repository navigation
Expand file tree
/
Copy pathDatabase.cs
More file actions
256 lines (217 loc) · 9.84 KB
/
Copy pathDatabase.cs
File metadata and controls
256 lines (217 loc) · 9.84 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
namespace DapperExtensions
{
public interface IDatabase
{
bool HasActiveTransaction { get; }
IDbConnection Connection { get; }
void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted);
void Commit();
void Rollback();
void RunInTransaction(Action action);
T RunInTransaction<T>(Func<T> func);
T Get<T>(dynamic id, IDbTransaction transaction, int? commandTimeout = null) where T : class;
T Get<T>(dynamic id, int? commandTimeout = null) where T : class;
void Insert<T>(IEnumerable<T> entities, IDbTransaction transaction, int? commandTimeout = null) where T : class;
void Insert<T>(IEnumerable<T> entities, int? commandTimeout = null) where T : class;
dynamic Insert<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class;
dynamic Insert<T>(T entity, int? commandTimeout = null) where T : class;
bool Update<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class;
bool Update<T>(T entity, int? commandTimeout = null) where T : class;
bool Delete<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class;
bool Delete<T>(T entity, int? commandTimeout = null) where T : class;
bool Delete<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class;
bool Delete<T>(object predicate, int? commandTimeout = null) where T : class;
IEnumerable<T> GetList<T>(object predicate, IList<ISort> sort, IDbTransaction transaction, int? commandTimeout = null, bool buffered = true) where T : class;
IEnumerable<T> GetList<T>(object predicate = null, IList<ISort> sort = null, int? commandTimeout = null, bool buffered = true) where T : class;
IEnumerable<T> GetPage<T>(object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction, int? commandTimeout = null, bool buffered = true) where T : class;
IEnumerable<T> GetPage<T>(object predicate, IList<ISort> sort, int page, int resultsPerPage, int? commandTimeout = null, bool buffered = true) where T : class;
int Count<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class;
int Count<T>(object predicate, int? commandTimeout = null) where T : class;
IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout = null);
IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, int? commandTimeout = null);
void ClearCache();
Guid GetNextGuid();
IClassMapper GetMap<T>() where T : class;
}
public class Database : IDatabase, IDisposable
{
private readonly IDapperImplementor _dapper;
private IDbTransaction _transaction;
public Database(IDbConnection connection, ISqlGenerator sqlGenerator)
{
_dapper = new DapperImplementor(sqlGenerator);
Connection = connection;
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
}
public bool HasActiveTransaction
{
get
{
return _transaction != null;
}
}
public IDbConnection Connection { get; private set; }
public void Dispose()
{
if (Connection.State != ConnectionState.Closed)
{
if (_transaction != null)
{
_transaction.Rollback();
}
Connection.Close();
}
}
public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
_transaction = Connection.BeginTransaction(isolationLevel);
}
public void Commit()
{
_transaction.Commit();
_transaction = null;
}
public void Rollback()
{
_transaction.Rollback();
_transaction = null;
}
public void RunInTransaction(Action action)
{
BeginTransaction();
try
{
action();
Commit();
}
catch (Exception ex)
{
if (HasActiveTransaction)
{
Rollback();
}
throw ex;
}
}
public T RunInTransaction<T>(Func<T> func)
{
BeginTransaction();
try
{
T result = func();
Commit();
return result;
}
catch (Exception ex)
{
if (HasActiveTransaction)
{
Rollback();
}
throw ex;
}
}
public T Get<T>(dynamic id, IDbTransaction transaction, int? commandTimeout) where T : class
{
return (T)_dapper.Get<T>(Connection, id, transaction, commandTimeout);
}
public T Get<T>(dynamic id, int? commandTimeout) where T : class
{
return (T)_dapper.Get<T>(Connection, id, _transaction, commandTimeout);
}
public void Insert<T>(IEnumerable<T> entities, IDbTransaction transaction, int? commandTimeout) where T : class
{
_dapper.Insert<T>(Connection, entities, transaction, commandTimeout);
}
public void Insert<T>(IEnumerable<T> entities, int? commandTimeout) where T : class
{
_dapper.Insert<T>(Connection, entities, _transaction, commandTimeout);
}
public dynamic Insert<T>(T entity, IDbTransaction transaction, int? commandTimeout) where T : class
{
return _dapper.Insert<T>(Connection, entity, transaction, commandTimeout);
}
public dynamic Insert<T>(T entity, int? commandTimeout) where T : class
{
return _dapper.Insert<T>(Connection, entity, _transaction, commandTimeout);
}
public bool Update<T>(T entity, IDbTransaction transaction, int? commandTimeout) where T : class
{
return _dapper.Update<T>(Connection, entity, transaction, commandTimeout);
}
public bool Update<T>(T entity, int? commandTimeout) where T : class
{
return _dapper.Update<T>(Connection, entity, _transaction, commandTimeout);
}
public bool Delete<T>(T entity, IDbTransaction transaction, int? commandTimeout) where T : class
{
return _dapper.Delete(Connection, entity, transaction, commandTimeout);
}
public bool Delete<T>(T entity, int? commandTimeout) where T : class
{
return _dapper.Delete(Connection, entity, _transaction, commandTimeout);
}
public bool Delete<T>(object predicate, IDbTransaction transaction, int? commandTimeout) where T : class
{
return _dapper.Delete<T>(Connection, predicate, transaction, commandTimeout);
}
public bool Delete<T>(object predicate, int? commandTimeout) where T : class
{
return _dapper.Delete<T>(Connection, predicate, _transaction, commandTimeout);
}
public IEnumerable<T> GetList<T>(object predicate, IList<ISort> sort, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
return _dapper.GetList<T>(Connection, predicate, sort, transaction, commandTimeout, buffered);
}
public IEnumerable<T> GetList<T>(object predicate, IList<ISort> sort, int? commandTimeout, bool buffered) where T : class
{
return _dapper.GetList<T>(Connection, predicate, sort, _transaction, commandTimeout, buffered);
}
public IEnumerable<T> GetPage<T>(object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
return _dapper.GetPage<T>(Connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
public IEnumerable<T> GetPage<T>(object predicate, IList<ISort> sort, int page, int resultsPerPage, int? commandTimeout, bool buffered) where T : class
{
return _dapper.GetPage<T>(Connection, predicate, sort, page, resultsPerPage, _transaction, commandTimeout, buffered);
}
public int Count<T>(object predicate, IDbTransaction transaction, int? commandTimeout) where T : class
{
return _dapper.Count<T>(Connection, predicate, transaction, commandTimeout);
}
public int Count<T>(object predicate, int? commandTimeout) where T : class
{
return _dapper.Count<T>(Connection, predicate, _transaction, commandTimeout);
}
public IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout)
{
return _dapper.GetMultiple(Connection, predicate, transaction, commandTimeout);
}
public IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, int? commandTimeout)
{
return _dapper.GetMultiple(Connection, predicate, _transaction, commandTimeout);
}
public void ClearCache()
{
_dapper.SqlGenerator.Configuration.ClearCache();
}
public Guid GetNextGuid()
{
return _dapper.SqlGenerator.Configuration.GetNextGuid();
}
public IClassMapper GetMap<T>() where T : class
{
return _dapper.SqlGenerator.Configuration.GetMap<T>();
}
}
}