Repository navigation
Expand file tree
/
Copy pathDapperExtensions.cs
More file actions
254 lines (226 loc) · 10.6 KB
/
Copy pathDapperExtensions.cs
File metadata and controls
254 lines (226 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using DapperExtensions.Sql;
using DapperExtensions.Mapper;
namespace DapperExtensions
{
public static class DapperExtensions
{
private readonly static object _lock = new object();
private static Func<IDapperExtensionsConfiguration, IDapperImplementor> _instanceFactory;
private static IDapperImplementor _instance;
private static IDapperExtensionsConfiguration _configuration;
/// <summary>
/// Gets or sets the default class mapper to use when generating class maps. If not specified, AutoClassMapper<T> is used.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static Type DefaultMapper
{
get
{
return _configuration.DefaultMapper;
}
set
{
Configure(value, _configuration.MappingAssemblies, _configuration.Dialect);
}
}
/// <summary>
/// Gets or sets the type of sql to be generated.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static ISqlDialect SqlDialect
{
get
{
return _configuration.Dialect;
}
set
{
Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, value);
}
}
/// <summary>
/// Get or sets the Dapper Extensions Implementation Factory.
/// </summary>
public static Func<IDapperExtensionsConfiguration, IDapperImplementor> InstanceFactory
{
get
{
if (_instanceFactory == null)
{
_instanceFactory = config => new DapperImplementor(new SqlGeneratorImpl(config));
}
return _instanceFactory;
}
set
{
_instanceFactory = value;
Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, _configuration.Dialect);
}
}
/// <summary>
/// Gets the Dapper Extensions Implementation
/// </summary>
private static IDapperImplementor Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = InstanceFactory(_configuration);
}
}
}
return _instance;
}
}
static DapperExtensions()
{
Configure(typeof(AutoClassMapper<>), new List<Assembly>(), new SqlServerDialect());
}
/// <summary>
/// Add other assemblies that Dapper Extensions will search if a mapping is not found in the same assembly of the POCO.
/// </summary>
/// <param name="assemblies"></param>
public static void SetMappingAssemblies(IList<Assembly> assemblies)
{
Configure(_configuration.DefaultMapper, assemblies, _configuration.Dialect);
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(IDapperExtensionsConfiguration configuration)
{
_instance = null;
_configuration = configuration;
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(Type defaultMapper, IList<Assembly> mappingAssemblies, ISqlDialect sqlDialect)
{
Configure(new DapperExtensionsConfiguration(defaultMapper, mappingAssemblies, sqlDialect));
}
/// <summary>
/// Executes a query for the specified id, returning the data typed as per T
/// </summary>
public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var result = Instance.Get<T>(connection, id, transaction, commandTimeout);
return (T)result;
}
/// <summary>
/// Executes an insert query for the specified entity.
/// </summary>
public static void Insert<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
Instance.Insert<T>(connection, entities, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity, returning the primary key.
/// If the entity has a single key, just the value is returned.
/// If the entity has a composite key, an IDictionary<string, object> is returned with the key values.
/// The key value for the entity will also be updated if the KeyType is a Guid or Identity.
/// </summary>
public static dynamic Insert<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Insert<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes an update query for the specified entity.
/// </summary>
public static bool Update<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false) where T : class
{
return Instance.Update<T>(connection, entity, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes a delete query for the specified entity.
/// </summary>
public static bool Delete<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Delete<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query using the specified predicate.
/// </summary>
public static bool Delete<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Delete<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public static IEnumerable<T> GetList<T>(this IDbConnection connection, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetList<T>(connection, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public static IEnumerable<T> GetPage<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetPage<T>(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified firstResult and maxResults.
/// </summary>
public static IEnumerable<T> GetSet<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetSet<T>(connection, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a query using the specified predicate, returning an integer that represents the number of rows that match the query.
/// </summary>
public static int Count<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Count<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query for multiple objects, returning IMultipleResultReader for each predicate.
/// </summary>
public static IMultipleResultReader GetMultiple(this IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.GetMultiple(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Gets the appropriate mapper for the specified type T.
/// If the mapper for the type is not yet created, a new mapper is generated from the mapper type specifed by DefaultMapper.
/// </summary>
public static IClassMapper GetMap<T>() where T : class
{
return Instance.SqlGenerator.Configuration.GetMap<T>();
}
/// <summary>
/// Clears the ClassMappers for each type.
/// </summary>
public static void ClearCache()
{
Instance.SqlGenerator.Configuration.ClearCache();
}
/// <summary>
/// Generates a COMB Guid which solves the fragmented index issue.
/// See: http://davybrion.com/blog/2009/05/using-the-guidcomb-identifier-strategy
/// </summary>
public static Guid GetNextGuid()
{
return Instance.SqlGenerator.Configuration.GetNextGuid();
}
}
}