hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ d2d4f84b
이력 | 보기 | 이력해설 | 다운로드 (4.01 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 |
|
11 |
using Dapper; |
12 |
|
13 |
namespace ID2.Manager.Dapper.Repository |
14 |
{ |
15 |
public abstract class BaseRepository : IBaseRepository |
16 |
{ |
17 |
protected readonly IDbConnection _DbConnection;//readonly 삭제해야하는지 확인 |
18 |
protected IDbTransaction _DbTransaction = null; |
19 |
|
20 |
public SqlConnection DBConnection(string connectionStr) |
21 |
{ |
22 |
return new SqlConnection(connectionStr); |
23 |
} |
24 |
|
25 |
public IDbTransaction BeginTransaction() |
26 |
{ |
27 |
if (this._DbConnection.State != ConnectionState.Open) |
28 |
{ |
29 |
this._DbConnection.Open(); |
30 |
} |
31 |
|
32 |
return this._DbConnection.BeginTransaction(); |
33 |
} |
34 |
|
35 |
public void Dispose() |
36 |
{ |
37 |
if (this._DbTransaction != null) |
38 |
{ |
39 |
this._DbTransaction.Commit(); |
40 |
} |
41 |
|
42 |
if (this._DbConnection != null) |
43 |
{ |
44 |
this._DbConnection.Close(); |
45 |
} |
46 |
} |
47 |
|
48 |
protected BaseRepository(string connectionStr) |
49 |
{ |
50 |
this._DbConnection = this.DBConnection(connectionStr); |
51 |
|
52 |
SqlMapper.TypeMapProvider = type => |
53 |
{ |
54 |
// create fallback default type map |
55 |
var fallback = new DefaultTypeMap(type); |
56 |
return new CustomPropertyTypeMap(type, (t, column) => |
57 |
{ |
58 |
var property = t.GetProperties().FirstOrDefault(prop => |
59 |
prop.GetCustomAttributes(typeof(ColumnAttribute), true) |
60 |
.Cast<ColumnAttribute>() |
61 |
.Any(attr => attr.Name == column)); |
62 |
|
63 |
// if no property matched - fall back to default type map |
64 |
if (property == null) |
65 |
{ |
66 |
property = fallback.GetMember(column)?.Property; |
67 |
} |
68 |
|
69 |
return property; |
70 |
}); |
71 |
}; |
72 |
} |
73 |
|
74 |
public IEnumerable<T> Query<T>(string sql) |
75 |
{ |
76 |
if (this._DbConnection.State != ConnectionState.Open) |
77 |
{ |
78 |
this._DbConnection.Open(); |
79 |
} |
80 |
|
81 |
return this._DbConnection.Query<T>(sql); |
82 |
} |
83 |
|
84 |
public IEnumerable<T> Query<T>(string sql, object param) |
85 |
{ |
86 |
if (this._DbConnection.State != ConnectionState.Open) |
87 |
{ |
88 |
this._DbConnection.Open(); |
89 |
} |
90 |
|
91 |
return this._DbConnection.Query<T>(sql, param); |
92 |
} |
93 |
|
94 |
public async Task<IEnumerable<T>> QueryAsync<T>(string sql) |
95 |
{ |
96 |
if (this._DbConnection.State != ConnectionState.Open) |
97 |
{ |
98 |
this._DbConnection.Open(); |
99 |
} |
100 |
|
101 |
return await this._DbConnection.QueryAsync<T>(sql); |
102 |
} |
103 |
|
104 |
public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param) |
105 |
{ |
106 |
if (this._DbConnection.State != ConnectionState.Open) |
107 |
{ |
108 |
this._DbConnection.Open(); |
109 |
} |
110 |
|
111 |
return await this._DbConnection.QueryAsync<T>(sql, param); |
112 |
} |
113 |
|
114 |
public int Execute(string query, IDbTransaction dbTran) |
115 |
{ |
116 |
return this.Execute(query, null, dbTran); |
117 |
} |
118 |
|
119 |
public int Execute(string query, object param, IDbTransaction dbTran) |
120 |
{ |
121 |
if (this._DbConnection.State != ConnectionState.Open) |
122 |
{ |
123 |
this._DbConnection.Open(); |
124 |
} |
125 |
|
126 |
return this._DbConnection.Execute(query, param, dbTran); |
127 |
} |
128 |
|
129 |
public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran) |
130 |
{ |
131 |
if (this._DbConnection.State != ConnectionState.Open) |
132 |
{ |
133 |
this._DbConnection.Open(); |
134 |
} |
135 |
|
136 |
return await this._DbConnection.ExecuteAsync(query, param, dbTran); |
137 |
} |
138 |
} |
139 |
} |