hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ e1377864
이력 | 보기 | 이력해설 | 다운로드 (7.66 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
|
7 |
using System.Data; |
8 |
using System.Data.SqlClient; |
9 |
using System.ComponentModel.DataAnnotations.Schema; |
10 |
#if !MARKUS_IMAGE_CREATE |
11 |
using System.Data.SQLite; |
12 |
#endif |
13 |
using Dapper; |
14 |
|
15 |
namespace ID2.Manager.Dapper.Repository |
16 |
{ |
17 |
public abstract class BaseRepository : IBaseRepository |
18 |
{ |
19 |
protected readonly IDbConnection _DbConnection; |
20 |
protected IDbTransaction _DbTransaction = null; |
21 |
|
22 |
protected BaseRepository(string connectionStr) : this(connectionStr, DatabaseType.MSSQL) { } |
23 |
|
24 |
protected BaseRepository(string connectionStr, DatabaseType dbType) |
25 |
{ |
26 |
switch (dbType) |
27 |
{ |
28 |
#if !MARKUS_IMAGE_CREATE |
29 |
case DatabaseType.SQLITE: |
30 |
this._DbConnection = this.SQLiteDBConnection(connectionStr); |
31 |
break; |
32 |
#endif |
33 |
case DatabaseType.MSSQL: |
34 |
default: |
35 |
this._DbConnection = this.DBConnection(connectionStr); |
36 |
break; |
37 |
} |
38 |
|
39 |
SqlMapper.TypeMapProvider = type => |
40 |
{ |
41 |
// create fallback default type map |
42 |
var fallback = new DefaultTypeMap(type); |
43 |
return new CustomPropertyTypeMap(type, (t, column) => |
44 |
{ |
45 |
var property = t.GetProperties().FirstOrDefault(prop => |
46 |
prop.GetCustomAttributes(typeof(ColumnAttribute), true) |
47 |
.Cast<ColumnAttribute>() |
48 |
.Any(attr => attr.Name == column)); |
49 |
|
50 |
// if no property matched - fall back to default type map |
51 |
if (property == null) |
52 |
{ |
53 |
property = fallback.GetMember(column)?.Property; |
54 |
} |
55 |
|
56 |
return property; |
57 |
}); |
58 |
}; |
59 |
} |
60 |
|
61 |
public SqlConnection DBConnection(string connectionStr) |
62 |
{ |
63 |
return new SqlConnection(connectionStr); |
64 |
} |
65 |
#if !MARKUS_IMAGE_CREATE |
66 |
public SQLiteConnection SQLiteDBConnection(string connectionStr) |
67 |
{ |
68 |
return new SQLiteConnection(connectionStr, true); |
69 |
} |
70 |
#endif |
71 |
|
72 |
public IDbTransaction BeginTransaction() |
73 |
{ |
74 |
if (this._DbConnection.State != ConnectionState.Open) |
75 |
{ |
76 |
this._DbConnection.Open(); |
77 |
} |
78 |
|
79 |
return this._DbConnection.BeginTransaction(); |
80 |
} |
81 |
|
82 |
public void Dispose() |
83 |
{ |
84 |
if (this._DbTransaction != null) |
85 |
{ |
86 |
this._DbTransaction.Commit(); |
87 |
} |
88 |
|
89 |
if (this._DbConnection != null) |
90 |
{ |
91 |
this._DbConnection.Close(); |
92 |
} |
93 |
} |
94 |
|
95 |
public IEnumerable<T> Query<T>(string sql) |
96 |
{ |
97 |
if (this._DbConnection.State != ConnectionState.Open) |
98 |
{ |
99 |
this._DbConnection.Open(); |
100 |
} |
101 |
|
102 |
return this._DbConnection.Query<T>(sql); |
103 |
} |
104 |
|
105 |
public IEnumerable<TReturn> MultiQuery<TFirst, TSecond, TFourth, TReturn>(string sql, Func<TFirst, TSecond, TFourth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) |
106 |
{ |
107 |
if (this._DbConnection.State != ConnectionState.Open) |
108 |
{ |
109 |
this._DbConnection.Open(); |
110 |
} |
111 |
|
112 |
return this._DbConnection.Query<TFirst, TSecond, TFourth, TReturn >(sql, map: map, param: param, transaction: transaction, buffered: buffered, splitOn: splitOn, commandTimeout: commandTimeout, commandType: commandType); |
113 |
} |
114 |
|
115 |
public T QueryFirst<T>(string sql) |
116 |
{ |
117 |
if (this._DbConnection.State != ConnectionState.Open) |
118 |
{ |
119 |
this._DbConnection.Open(); |
120 |
} |
121 |
|
122 |
return this._DbConnection.QueryFirst<T>(sql); |
123 |
} |
124 |
|
125 |
public T QueryFirstOrDefault<T>(string sql, object param, IDbTransaction dbTran = null) |
126 |
{ |
127 |
if (this._DbConnection.State != ConnectionState.Open) |
128 |
{ |
129 |
this._DbConnection.Open(); |
130 |
} |
131 |
|
132 |
return this._DbConnection.QueryFirstOrDefault<T>(sql, param, transaction: dbTran); |
133 |
} |
134 |
|
135 |
|
136 |
public IEnumerable<T> Query<T>(string sql, object param) |
137 |
{ |
138 |
if (this._DbConnection.State != ConnectionState.Open) |
139 |
{ |
140 |
this._DbConnection.Open(); |
141 |
} |
142 |
|
143 |
return this._DbConnection.Query<T>(sql, param); |
144 |
} |
145 |
public IEnumerable<T> Query<T>(string sql, object param, CommandType commandType) |
146 |
{ |
147 |
if (this._DbConnection.State != ConnectionState.Open) |
148 |
{ |
149 |
this._DbConnection.Open(); |
150 |
} |
151 |
|
152 |
return this._DbConnection.Query<T>(sql, param, commandType: commandType); |
153 |
} |
154 |
|
155 |
public async Task<IEnumerable<T>> QueryAsync<T>(string sql) |
156 |
{ |
157 |
if (this._DbConnection.State != ConnectionState.Open) |
158 |
{ |
159 |
this._DbConnection.Open(); |
160 |
} |
161 |
|
162 |
return await this._DbConnection.QueryAsync<T>(sql); |
163 |
} |
164 |
|
165 |
public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param) |
166 |
{ |
167 |
if (this._DbConnection.State != ConnectionState.Open) |
168 |
{ |
169 |
this._DbConnection.Open(); |
170 |
} |
171 |
|
172 |
return await this._DbConnection.QueryAsync<T>(sql, param); |
173 |
} |
174 |
|
175 |
public int Execute(string query, IDbTransaction dbTran) |
176 |
{ |
177 |
return this.Execute(query, null, dbTran); |
178 |
} |
179 |
|
180 |
public int Execute(string query, object param, IDbTransaction dbTran) |
181 |
{ |
182 |
if (this._DbConnection.State != ConnectionState.Open) |
183 |
{ |
184 |
this._DbConnection.Open(); |
185 |
} |
186 |
|
187 |
return this._DbConnection.Execute(query, param, dbTran); |
188 |
} |
189 |
|
190 |
|
191 |
public int Execute(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType) |
192 |
{ |
193 |
if (_DbConnection.State != System.Data.ConnectionState.Open) |
194 |
{ |
195 |
_DbConnection.Open(); |
196 |
} |
197 |
|
198 |
|
199 |
return _DbConnection.Execute(SQL, parameters, transaction, commandType: commandType); |
200 |
} |
201 |
|
202 |
public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran) |
203 |
{ |
204 |
if (this._DbConnection.State != ConnectionState.Open) |
205 |
{ |
206 |
this._DbConnection.Open(); |
207 |
} |
208 |
|
209 |
return await this._DbConnection.ExecuteAsync(query, param, dbTran); |
210 |
} |
211 |
|
212 |
|
213 |
public async Task<int> ExecuteAsync(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType) |
214 |
{ |
215 |
if (_DbConnection.State != System.Data.ConnectionState.Open) |
216 |
{ |
217 |
_DbConnection.Open(); |
218 |
} |
219 |
|
220 |
|
221 |
return await _DbConnection.ExecuteAsync(SQL, parameters, transaction, commandType: commandType); |
222 |
} |
223 |
public T ExecuteScalar<T>(string query, object param) |
224 |
{ |
225 |
if (this._DbConnection.State != ConnectionState.Open) |
226 |
{ |
227 |
this._DbConnection.Open(); |
228 |
} |
229 |
|
230 |
return this._DbConnection.ExecuteScalar<T>(query, param); |
231 |
} |
232 |
|
233 |
|
234 |
public T ExecuteScalar<T>(string query, object param, IDbTransaction dbTran) |
235 |
{ |
236 |
if (_DbConnection.State != ConnectionState.Open) |
237 |
{ |
238 |
_DbConnection.Open(); |
239 |
} |
240 |
|
241 |
return this._DbConnection.ExecuteScalar<T>(query, param, dbTran); |
242 |
} |
243 |
} |
244 |
} |