forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryBase.cs
More file actions
370 lines (284 loc) · 13 KB
/
RepositoryBase.cs
File metadata and controls
370 lines (284 loc) · 13 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SharpRepository.Repository.Caching;
using SharpRepository.Repository.Helpers;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
using SharpRepository.Repository.Transactions;
namespace SharpRepository.Repository
{
public abstract partial class RepositoryBase<T, TKey> : IRepository<T, TKey> where T : class
{
// the caching strategy used
private readonly ICachingStrategy<T, TKey> _cachingStrategy;
// the query manager uses the caching strategy to determine if it should check the cache or run the query
private readonly QueryManager<T, TKey> _queryManager;
// just the type name, used to find the primary key if it is [TypeName]Id
private readonly string _typeName;
protected string TypeName
{
get { return _typeName; }
}
public IBatch<T> BeginBatch()
{
// Return the privately scoped batch via the publicly available interface.
// This ensures that a repository alone can initiate a new batch.
return new Batch(this);
}
private bool BatchMode { get; set; }
protected RepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
{
if (typeof(T) == typeof(TKey))
{
// this check is mainly because of the overloaded Delete methods Delete(T) and Delete(TKey), ambiguous reference if the generics are the same
throw new InvalidOperationException("The repository type and the primary key type can not be the same.");
}
_cachingStrategy = cachingStrategy ?? new NoCachingStrategy<T, TKey>();
_typeName = typeof (T).Name;
_queryManager = new QueryManager<T, TKey>(_cachingStrategy);
}
public abstract IQueryable<T> AsQueryable();
// These are the actual implementation that the derived class needs to implement
protected abstract IQueryable<T> GetAllQuery();
protected abstract IQueryable<T> GetAllQuery(IQueryOptions<T> queryOptions);
public IEnumerable<T> GetAll()
{
return GetAll(null);
}
public IEnumerable<T> GetAll(IQueryOptions<T> queryOptions)
{
return _queryManager.ExecuteGetAll(
() => queryOptions == null ? GetAllQuery().ToList() : GetAllQuery(queryOptions).ToList(),
queryOptions
);
}
public IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
{
if (selector == null) throw new ArgumentNullException("selector");
return GetAllQuery(queryOptions)
.Select(selector)
.ToList();
}
// These are the actual implementation that the derived class needs to implement
protected abstract T GetQuery(TKey key);
public abstract IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
where TInner : class
where TResult : class;
public T Get(TKey key)
{
return _queryManager.ExecuteGet(
() => GetQuery(key),
key
);
}
public TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector)
{
if (selector == null) throw new ArgumentNullException("selector");
var result = Get(key);
if (result == null)
return default(TResult);
var results = new [] { result };
return results.AsQueryable().Select(selector).FirstOrDefault();
}
// These are the actual implementation that the derived class needs to implement
protected abstract IQueryable<T> FindAllQuery(ISpecification<T> criteria);
protected abstract IQueryable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions);
public IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
{
if (criteria == null) throw new ArgumentNullException("criteria");
return _queryManager.ExecuteFindAll(
() => queryOptions == null ? FindAllQuery(criteria).ToList() : FindAllQuery(criteria, queryOptions).ToList(),
criteria,
queryOptions
);
}
public IEnumerable<TResult> FindAll<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
{
if (criteria == null) throw new ArgumentNullException("criteria");
return FindAllQuery(criteria, queryOptions).Select(selector).ToList();
}
public IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null)
{
if (predicate == null) throw new ArgumentNullException("predicate");
return FindAll(new Specification<T>(predicate), queryOptions);
}
public IEnumerable<TResult> FindAll<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
{
if (predicate == null) throw new ArgumentNullException("predicate");
if (selector == null) throw new ArgumentNullException("selector");
return FindAll(new Specification<T>(predicate), selector, queryOptions);
}
// These are the actual implementation that the derived class needs to implement
protected abstract T FindQuery(ISpecification<T> criteria);
protected abstract T FindQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions);
public T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
{
if (criteria == null) throw new ArgumentNullException("criteria");
return _queryManager.ExecuteFind(
() => queryOptions == null ? FindQuery(criteria) : FindQuery(criteria, queryOptions),
criteria,
null
);
}
public TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
{
if (criteria == null) throw new ArgumentNullException("criteria");
if (selector == null) throw new ArgumentNullException("selector");
var result = Find(criteria, queryOptions);
if (result == null)
return default(TResult);
var results = new [] { result};
return results.AsQueryable().Select(selector).First();
}
public T Find(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null)
{
if (predicate == null) throw new ArgumentNullException("predicate");
return Find(new Specification<T>(predicate), queryOptions);
}
public TResult Find<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
{
if (predicate == null) throw new ArgumentNullException("predicate");
if (selector == null) throw new ArgumentNullException("selector");
return Find(new Specification<T>(predicate), selector, queryOptions);
}
// This is the actual implementation that the derived class needs to implement
protected abstract void AddItem(T entity);
public void Add(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
ProcessAdd(entity, BatchMode);
}
// used from the Add method above and the Save below for the batch save
private void ProcessAdd(T entity, bool batchMode)
{
AddItem(entity);
if (batchMode) return;
Save();
TKey key;
if (GetPrimaryKey(entity, out key))
_queryManager.OnItemAdded(key, entity);
}
public void Add(IEnumerable<T> entities)
{
if (entities == null) throw new ArgumentNullException("entities");
foreach (var entity in entities)
{
Add(entity);
}
}
// This is the actual implementation that the derived class needs to implement
protected abstract void DeleteItem(T entity);
public void Delete(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
ProcessDelete(entity, BatchMode);
}
// used from the Delete method above and the Save below for the batch save
private void ProcessDelete(T entity, bool batchMode)
{
DeleteItem(entity);
if (batchMode) return;
Save();
TKey key;
if (GetPrimaryKey(entity, out key))
_queryManager.OnItemDeleted(key, entity);
}
public void Delete(IEnumerable<T> entities)
{
foreach (var entity in entities)
{
Delete(entity);
}
}
public void Delete(TKey key)
{
var entity = Get(key);
if (entity == null) throw new ArgumentException("No entity exists with this key.", "key");
Delete(entity);
}
// This is the actual implementation that the derived class needs to implement
protected abstract void UpdateItem(T entity);
public void Update(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
ProcessUpdate(entity, BatchMode);
}
// used from the Update method above and the Save below for the batch save
private void ProcessUpdate(T entity, bool batchMode)
{
UpdateItem(entity);
if (batchMode) return;
Save();
TKey key;
if (GetPrimaryKey(entity, out key))
_queryManager.OnItemUpdated(key, entity);
}
public void Update(IEnumerable<T> entities)
{
if (entities == null) throw new ArgumentNullException("entities");
foreach (var entity in entities)
{
Update(entity);
}
}
protected abstract void SaveChanges();
private void Save()
{
SaveChanges();
_queryManager.OnSaveExecuted();
}
public abstract void Dispose();
protected virtual bool GetPrimaryKey(T entity, out TKey key)
{
key = default(TKey);
var propInfo = GetPrimaryKeyPropertyInfo();
// if there is no property that matches then return false
if (propInfo == null)
return false;
key = (TKey) propInfo.GetValue(entity, null);
return true;
}
protected virtual bool SetPrimaryKey(T entity, TKey key)
{
var propInfo = GetPrimaryKeyPropertyInfo();
// if there is no property that matches then return false
if (propInfo == null)
return false;
propInfo.SetValue(entity, key, null);
return true;
}
protected virtual ISpecification<T> ByPrimaryKeySpecification(TKey key)
{
var propInfo = GetPrimaryKeyPropertyInfo();
var lambda = Linq.DynamicExpression.ParseLambda<T,bool>(String.Format("{0} == {1}", propInfo.Name, key));
return new Specification<T>(lambda);
}
protected PropertyInfo GetPrimaryKeyPropertyInfo()
{
// checks for properties in this order that match TKey type
// 1) RepositoryPrimaryKeyAttribute
// 2) Id
// 3) [Type Name]Id
var type = typeof(T);
var keyType = typeof(TKey);
return type.GetProperties().FirstOrDefault(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>() && x.GetType() == keyType)
?? GetPropertyCaseInsensitive(type, "Id", keyType)
?? GetPropertyCaseInsensitive(type, _typeName + "Id", keyType);
}
private static PropertyInfo GetPropertyCaseInsensitive(IReflect type, string propertyName, Type propertyType)
{
// make the property reflection lookup case insensitive
const BindingFlags bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance;
return type.GetProperty(propertyName, bindingFlags, null, propertyType, new Type[0], new ParameterModifier[0]);
}
public abstract IEnumerator<T> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}