hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ 709c1971
이력 | 보기 | 이력해설 | 다운로드 (3.72 KB)
1 | 8164f84e | yoush97 | 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 | 482f6326 | yoush97 | protected readonly IDbConnection _DbConnection;//readonly 삭제해야하는지 확인 |
18 | 8164f84e | yoush97 | |
19 | public SqlConnection DBConnection(string connectionStr) |
||
20 | { |
||
21 | return new SqlConnection(connectionStr); |
||
22 | } |
||
23 | |||
24 | public void Dispose() |
||
25 | { |
||
26 | if (this._DbConnection != null) |
||
27 | { |
||
28 | this._DbConnection.Close(); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | protected BaseRepository(string connectionStr) |
||
33 | { |
||
34 | this._DbConnection = this.DBConnection(connectionStr); |
||
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 IEnumerable<T> Query<T>(string sql) |
||
59 | { |
||
60 | if (this._DbConnection.State != ConnectionState.Open) |
||
61 | { |
||
62 | this._DbConnection.Open(); |
||
63 | } |
||
64 | |||
65 | return this._DbConnection.Query<T>(sql); |
||
66 | } |
||
67 | |||
68 | public IEnumerable<T> Query<T>(string sql, object param) |
||
69 | { |
||
70 | if (this._DbConnection.State != ConnectionState.Open) |
||
71 | { |
||
72 | this._DbConnection.Open(); |
||
73 | } |
||
74 | |||
75 | return this._DbConnection.Query<T>(sql, param); |
||
76 | } |
||
77 | |||
78 | public async Task<IEnumerable<T>> QueryAsync<T>(string sql) |
||
79 | { |
||
80 | if (this._DbConnection.State != ConnectionState.Open) |
||
81 | { |
||
82 | this._DbConnection.Open(); |
||
83 | } |
||
84 | |||
85 | return await this._DbConnection.QueryAsync<T>(sql); |
||
86 | } |
||
87 | |||
88 | public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param) |
||
89 | { |
||
90 | if (this._DbConnection.State != ConnectionState.Open) |
||
91 | { |
||
92 | this._DbConnection.Open(); |
||
93 | } |
||
94 | |||
95 | return await this._DbConnection.QueryAsync<T>(sql, param); |
||
96 | } |
||
97 | |||
98 | 709c1971 | yoush97 | public int Execute(string query, IDbTransaction dbTran) |
99 | { |
||
100 | if (this._DbConnection.State != ConnectionState.Open) |
||
101 | { |
||
102 | this._DbConnection.Open(); |
||
103 | } |
||
104 | |||
105 | return this._DbConnection.Execute(query, dbTran); |
||
106 | } |
||
107 | |||
108 | 8164f84e | yoush97 | public int Execute(string query, object param, IDbTransaction dbTran) |
109 | { |
||
110 | if (this._DbConnection.State != ConnectionState.Open) |
||
111 | { |
||
112 | this._DbConnection.Open(); |
||
113 | } |
||
114 | |||
115 | return this._DbConnection.Execute(query, param, dbTran); |
||
116 | } |
||
117 | |||
118 | public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran) |
||
119 | { |
||
120 | if (this._DbConnection.State != ConnectionState.Open) |
||
121 | { |
||
122 | this._DbConnection.Open(); |
||
123 | } |
||
124 | |||
125 | return await this._DbConnection.ExecuteAsync(query, param, dbTran); |
||
126 | } |
||
127 | } |
||
128 | } |