프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ 978488b0

이력 | 보기 | 이력해설 | 다운로드 (6.27 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

    
132
        public async Task<IEnumerable<T>> QueryAsync<T>(string sql)
133
        {
134
            if (this._DbConnection.State != ConnectionState.Open)
135
            {
136
                this._DbConnection.Open();
137
            }
138

    
139
            return await this._DbConnection.QueryAsync<T>(sql);
140
        }
141

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

    
149
            return await this._DbConnection.QueryAsync<T>(sql, param);
150
        }
151

    
152
        public int Execute(string query, IDbTransaction dbTran)
153
        {
154
            return this.Execute(query, null, dbTran);
155
        }
156

    
157
        public int Execute(string query, object param, IDbTransaction dbTran)
158
        {
159
            if (this._DbConnection.State != ConnectionState.Open)
160
            {
161
                this._DbConnection.Open();
162
            }
163

    
164
            return this._DbConnection.Execute(query, param, dbTran);
165
        }
166

    
167

    
168
        public int Execute(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType)
169
        {
170
            if (_DbConnection.State != System.Data.ConnectionState.Open)
171
            {
172
                _DbConnection.Open();
173
            }
174

    
175

    
176
            return _DbConnection.Execute(SQL, parameters, transaction, commandType: commandType);
177
        }
178

    
179
        public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran)
180
        {
181
            if (this._DbConnection.State != ConnectionState.Open)
182
            {
183
                this._DbConnection.Open();
184
            }
185

    
186
            return await this._DbConnection.ExecuteAsync(query, param, dbTran);
187
        }
188

    
189

    
190
        public async Task<int> ExecuteAsync(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType)
191
        {
192
            if (_DbConnection.State != System.Data.ConnectionState.Open)
193
            {
194
                _DbConnection.Open();
195
            }
196

    
197

    
198
            return await _DbConnection.ExecuteAsync(SQL, parameters, transaction, commandType: commandType);
199
        }
200

    
201
        public T ExecuteScalar<T>(string query, object param, IDbTransaction dbTran)
202
        {
203
            if (_DbConnection.State != ConnectionState.Open)
204
            {
205
                _DbConnection.Open();
206
            }
207

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