프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ 00d11333

이력 | 보기 | 이력해설 | 다운로드 (6.6 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 fdecb3be yoush97
using System.Data.SQLite;
11 8164f84e yoush97
12
using Dapper;
13
14
namespace ID2.Manager.Dapper.Repository
15
{
16
    public abstract class BaseRepository : IBaseRepository
17
    {
18 fdecb3be yoush97
        protected readonly IDbConnection _DbConnection;
19 d2d4f84b yoush97
        protected IDbTransaction _DbTransaction = null;
20 8164f84e yoush97
21 e8b34346 yoush97
        protected BaseRepository(string connectionStr) : this(connectionStr, DatabaseType.MSSQL) { }
22 8164f84e yoush97
23 fdecb3be yoush97
        protected BaseRepository(string connectionStr, DatabaseType dbType)
24 d2d4f84b yoush97
        {
25 fdecb3be yoush97
            switch (dbType)
26 d2d4f84b yoush97
            {
27 fdecb3be yoush97
                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 d2d4f84b yoush97
            }
35
36 8164f84e yoush97
            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 be2dacfc yoush97
        }
57 fdecb3be yoush97
58 be2dacfc yoush97
        public SqlConnection DBConnection(string connectionStr)
59
        {
60 fdecb3be yoush97
            return new SqlConnection(connectionStr);
61
        }
62
63
        public SQLiteConnection SQLiteDBConnection(string connectionStr)
64
        {
65 be2dacfc yoush97
            return new SQLiteConnection(connectionStr, true);
66 fdecb3be yoush97
        }
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 8164f84e yoush97
        }
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 098748fa taeseongkim
        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 978488b0 yoush97
        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 098748fa taeseongkim
122 8164f84e yoush97
        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 0b008ea6 taeseongkim
        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 8164f84e yoush97
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 709c1971 yoush97
        public int Execute(string query, IDbTransaction dbTran)
162
        {
163 d2d4f84b yoush97
            return this.Execute(query, null, dbTran);
164 709c1971 yoush97
        }
165
166 8164f84e yoush97
        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 c112c3c3 taeseongkim
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 8164f84e yoush97
        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 1a88080d taeseongkim
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 a23d0a0c yoush97
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 8164f84e yoush97
    }
220
}
클립보드 이미지 추가 (최대 크기: 500 MB)