프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ a23d0a0c

이력 | 보기 | 이력해설 | 다운로드 (6.95 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)
22
        {
23
            this._DbConnection = this.DBConnection(connectionStr);
24

    
25
            SqlMapper.TypeMapProvider = type =>
26
            {
27
                // create fallback default type map
28
                var fallback = new DefaultTypeMap(type);
29
                return new CustomPropertyTypeMap(type, (t, column) =>
30
                {
31
                    var property = t.GetProperties().FirstOrDefault(prop =>
32
                        prop.GetCustomAttributes(typeof(ColumnAttribute), true)
33
                            .Cast<ColumnAttribute>()
34
                            .Any(attr => attr.Name == column));
35

    
36
                    // if no property matched - fall back to default type map
37
                    if (property == null)
38
                    {
39
                        property = fallback.GetMember(column)?.Property;
40
                    }
41

    
42
                    return property;
43
                });
44
            };
45
        }
46

    
47
        protected BaseRepository(string connectionStr, DatabaseType dbType)
48
        {
49
            switch (dbType)
50
            {
51
                case DatabaseType.SQLITE:
52
                    this._DbConnection = this.SQLiteDBConnection(connectionStr);
53
                    break;
54
                case DatabaseType.MSSQL:
55
                default:
56
                    this._DbConnection = this.DBConnection(connectionStr);
57
                    break;
58
            }
59

    
60
            SqlMapper.TypeMapProvider = type =>
61
            {
62
                // create fallback default type map
63
                var fallback = new DefaultTypeMap(type);
64
                return new CustomPropertyTypeMap(type, (t, column) =>
65
                {
66
                    var property = t.GetProperties().FirstOrDefault(prop =>
67
                        prop.GetCustomAttributes(typeof(ColumnAttribute), true)
68
                            .Cast<ColumnAttribute>()
69
                            .Any(attr => attr.Name == column));
70

    
71
                    // if no property matched - fall back to default type map
72
                    if (property == null)
73
                    {
74
                        property = fallback.GetMember(column)?.Property;
75
                    }
76

    
77
                    return property;
78
                });
79
            };
80
        }
81

    
82
        public SqlConnection DBConnection(string connectionStr)
83
        {
84
            return new SqlConnection(connectionStr);
85
        }
86

    
87
        public SQLiteConnection SQLiteDBConnection(string connectionStr)
88
        {
89
            if (System.IO.Directory.Exists(connectionStr))
90
            {
91
                throw new Exception("ID2 db file does not exist.");
92
            }
93

    
94
            return new SQLiteConnection(connectionStr, true);
95
        }
96

    
97
        public IDbTransaction BeginTransaction()
98
        {
99
            if (this._DbConnection.State != ConnectionState.Open)
100
            {
101
                this._DbConnection.Open();
102
            }
103

    
104
            return this._DbConnection.BeginTransaction();
105
        }
106

    
107
        public void Dispose()
108
        {
109
            if (this._DbTransaction != null)
110
            {
111
                this._DbTransaction.Commit();
112
            }
113

    
114
            if (this._DbConnection != null)
115
            {
116
                this._DbConnection.Close();
117
            }
118
        }
119

    
120
        public IEnumerable<T> Query<T>(string sql)
121
        {
122
            if (this._DbConnection.State != ConnectionState.Open)
123
            {
124
                this._DbConnection.Open();
125
            }
126

    
127
            return this._DbConnection.Query<T>(sql);
128
        }
129

    
130
        public T QueryFirst<T>(string sql)
131
        {
132
            if (this._DbConnection.State != ConnectionState.Open)
133
            {
134
                this._DbConnection.Open();
135
            }
136

    
137
            return this._DbConnection.QueryFirst<T>(sql);
138
        }
139

    
140

    
141
        public IEnumerable<T> Query<T>(string sql, object param)
142
        {
143
            if (this._DbConnection.State != ConnectionState.Open)
144
            {
145
                this._DbConnection.Open();
146
            }
147

    
148
            return this._DbConnection.Query<T>(sql, param);
149
        }
150

    
151
        public async Task<IEnumerable<T>> QueryAsync<T>(string sql)
152
        {
153
            if (this._DbConnection.State != ConnectionState.Open)
154
            {
155
                this._DbConnection.Open();
156
            }
157

    
158
            return await this._DbConnection.QueryAsync<T>(sql);
159
        }
160

    
161
        public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param)
162
        {
163
            if (this._DbConnection.State != ConnectionState.Open)
164
            {
165
                this._DbConnection.Open();
166
            }
167

    
168
            return await this._DbConnection.QueryAsync<T>(sql, param);
169
        }
170

    
171
        public int Execute(string query, IDbTransaction dbTran)
172
        {
173
            return this.Execute(query, null, dbTran);
174
        }
175

    
176
        public int Execute(string query, object param, IDbTransaction dbTran)
177
        {
178
            if (this._DbConnection.State != ConnectionState.Open)
179
            {
180
                this._DbConnection.Open();
181
            }
182

    
183
            return this._DbConnection.Execute(query, param, dbTran);
184
        }
185

    
186

    
187
        public int Execute(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType)
188
        {
189
            if (_DbConnection.State != System.Data.ConnectionState.Open)
190
            {
191
                _DbConnection.Open();
192
            }
193

    
194

    
195
            return _DbConnection.Execute(SQL, parameters, transaction, commandType: commandType);
196
        }
197

    
198
        public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran)
199
        {
200
            if (this._DbConnection.State != ConnectionState.Open)
201
            {
202
                this._DbConnection.Open();
203
            }
204

    
205
            return await this._DbConnection.ExecuteAsync(query, param, dbTran);
206
        }
207

    
208

    
209
        public async Task<int> ExecuteAsync(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType)
210
        {
211
            if (_DbConnection.State != System.Data.ConnectionState.Open)
212
            {
213
                _DbConnection.Open();
214
            }
215

    
216

    
217
            return await _DbConnection.ExecuteAsync(SQL, parameters, transaction, commandType: commandType);
218
        }
219

    
220
        public T ExecuteScalar<T>(string query, object param, IDbTransaction dbTran)
221
        {
222
            if (_DbConnection.State != ConnectionState.Open)
223
            {
224
                _DbConnection.Open();
225
            }
226

    
227
            return this._DbConnection.ExecuteScalar<T>(query, param, dbTran);
228
        }
229
    }
230
}
클립보드 이미지 추가 (최대 크기: 500 MB)