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