Skip to content

Commit 92e01c4

Browse files
committed
Fixed bugs associated with CompoundKeyRepositoryBase refactoring.
1 parent fc1c031 commit 92e01c4

2 files changed

Lines changed: 97 additions & 100 deletions

File tree

SharpRepository.Repository/CompoundKeyRepositoryBase.cs

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using System;
2-
using System.Collections;
3-
using System.Reflection;
42
using System.Collections.Generic;
53
using System.Linq;
64
using System.Linq.Expressions;
75
using SharpRepository.Repository.Caching;
86
using SharpRepository.Repository.FetchStrategies;
9-
using SharpRepository.Repository.Helpers;
107
using SharpRepository.Repository.Queries;
118
using SharpRepository.Repository.Specifications;
129
using SharpRepository.Repository.Transactions;
@@ -69,7 +66,30 @@ public override IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>
6966
);
7067
}
7168

72-
public override T Get(params object[] keys)
69+
// These are the actual implementation that the derived class needs to implement
70+
protected abstract T GetQuery(params object[] keys);
71+
72+
public bool Exists(params object[] keys)
73+
{
74+
return TryGet(out T entity, keys);
75+
}
76+
77+
public bool TryGet(out T entity, params object[] keys)
78+
{
79+
entity = default(T);
80+
81+
try
82+
{
83+
entity = Get(keys);
84+
return entity != null;
85+
}
86+
catch (Exception)
87+
{
88+
return false;
89+
}
90+
}
91+
92+
public T Get(params object[] keys)
7393
{
7494
return _queryManager.ExecuteGet(
7595
() => GetQuery(keys),
@@ -167,7 +187,16 @@ public override void Add(T entity)
167187
_queryManager.OnItemAdded(keys, entity);
168188
}
169189

170-
public void Delete(T entity)
190+
public void Delete(params object[] keys)
191+
{
192+
var entity = Get(keys);
193+
194+
if (entity == null) throw new ArgumentException("No entity exists with these keys.", "keys");
195+
196+
Delete(entity);
197+
}
198+
199+
public override void Delete(T entity)
171200
{
172201
if (entity == null) throw new ArgumentNullException("entity");
173202

@@ -193,6 +222,64 @@ public override void Update(T entity)
193222
_queryManager.OnItemUpdated(keys, entity);
194223
}
195224

225+
protected virtual bool GetPrimaryKeys(T entity, out object[] keys)
226+
{
227+
keys = null;
228+
var propInfo = GetPrimaryKeyPropertyInfo();
229+
230+
// if there is no property that matches then return false
231+
if (propInfo == null)
232+
return false;
233+
234+
keys = propInfo.Select(info => info.GetValue(entity, null)).ToArray();
235+
236+
return true;
237+
}
238+
239+
protected virtual bool SetPrimaryKey(T entity, object[] keys)
240+
{
241+
var propInfo = GetPrimaryKeyPropertyInfo();
242+
243+
// if there is no property that matches then return false
244+
if (propInfo == null || keys == null || propInfo.Length != keys.Length)
245+
return false;
246+
247+
var i = 0;
248+
foreach (var key in keys)
249+
{
250+
propInfo[i].SetValue(entity, key, null);
251+
i++;
252+
}
253+
254+
return true;
255+
}
256+
257+
protected virtual ISpecification<T> ByPrimaryKeySpecification(object[] keys)
258+
{
259+
var propInfo = GetPrimaryKeyPropertyInfo();
260+
if (propInfo == null || keys == null || propInfo.Length != keys.Length)
261+
return null;
262+
263+
ISpecification<T> specification = null;
264+
var parameter = Expression.Parameter(typeof(T), "x");
265+
266+
var i = 0;
267+
foreach (var lambda in keys.Select(key => Expression.Lambda<Func<T, bool>>(
268+
Expression.Equal(
269+
Expression.PropertyOrField(parameter, propInfo[i].Name),
270+
Expression.Constant(key)
271+
),
272+
parameter
273+
))
274+
)
275+
{
276+
specification = specification == null ? new Specification<T>(lambda) : specification.And(lambda);
277+
i++;
278+
}
279+
280+
return specification;
281+
}
282+
196283
private void Save()
197284
{
198285
SaveChanges();
@@ -401,7 +488,7 @@ public override void Add(T entity)
401488
_queryManager.OnItemAdded(key, key2, entity);
402489
}
403490

404-
public void Delete(T entity)
491+
public override void Delete(T entity)
405492
{
406493
if (entity == null) throw new ArgumentNullException("entity");
407494

@@ -701,7 +788,7 @@ public override void Add(T entity)
701788
_queryManager.OnItemAdded(key, key2, key3, entity);
702789
}
703790

704-
public void Delete(T entity)
791+
public override void Delete(T entity)
705792
{
706793
if (entity == null) throw new ArgumentNullException("entity");
707794

SharpRepository.Repository/CompoundKeyRepositoryBaseCommon.cs

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,7 @@ public IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selecto
106106
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)
107107
where TInner : class
108108
where TResult : class;
109-
110-
// These are the actual implementation that the derived class needs to implement
111-
protected abstract T GetQuery(params object[] keys);
112-
113-
public bool Exists(params object[] keys)
114-
{
115-
return TryGet(out T entity, keys);
116-
}
117-
118-
public bool TryGet(out T entity, params object[] keys)
119-
{
120-
entity = default(T);
121-
122-
try
123-
{
124-
entity = Get(keys);
125-
return entity != null;
126-
}
127-
catch (Exception)
128-
{
129-
return false;
130-
}
131-
}
132-
133-
public abstract T Get(params object[] keys);
134-
109+
135110
// These are the actual implementation that the derived class needs to implement
136111
protected abstract IQueryable<T> FindAllQuery(ISpecification<T> criteria);
137112
protected abstract IQueryable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions);
@@ -285,6 +260,8 @@ public void Add(IEnumerable<T> entities)
285260
// This is the actual implementation that the derived class needs to implement
286261
protected abstract void DeleteItem(T entity);
287262

263+
public abstract void Delete(T entity);
264+
288265
public void Delete(IEnumerable<T> entities)
289266
{
290267
foreach (var entity in entities)
@@ -293,15 +270,6 @@ public void Delete(IEnumerable<T> entities)
293270
}
294271
}
295272

296-
public void Delete(params object[] keys)
297-
{
298-
var entity = Get(keys);
299-
300-
if (entity == null) throw new ArgumentException("No entity exists with these keys.", "keys");
301-
302-
Delete(entity);
303-
}
304-
305273
public void Delete(Expression<Func<T, bool>> predicate)
306274
{
307275
Delete(new Specification<T>(predicate));
@@ -331,64 +299,6 @@ public void Update(IEnumerable<T> entities)
331299

332300
public abstract void Dispose();
333301

334-
protected virtual bool GetPrimaryKeys(T entity, out object[] keys)
335-
{
336-
keys = null;
337-
var propInfo = GetPrimaryKeyPropertyInfo();
338-
339-
// if there is no property that matches then return false
340-
if (propInfo == null)
341-
return false;
342-
343-
keys = propInfo.Select(info => info.GetValue(entity, null)).ToArray();
344-
345-
return true;
346-
}
347-
348-
protected virtual bool SetPrimaryKey(T entity, object[] keys)
349-
{
350-
var propInfo = GetPrimaryKeyPropertyInfo();
351-
352-
// if there is no property that matches then return false
353-
if (propInfo == null || keys == null || propInfo.Length != keys.Length)
354-
return false;
355-
356-
var i = 0;
357-
foreach (var key in keys)
358-
{
359-
propInfo[i].SetValue(entity, key, null);
360-
i++;
361-
}
362-
363-
return true;
364-
}
365-
366-
protected virtual ISpecification<T> ByPrimaryKeySpecification(object[] keys)
367-
{
368-
var propInfo = GetPrimaryKeyPropertyInfo();
369-
if (propInfo == null || keys == null || propInfo.Length != keys.Length)
370-
return null;
371-
372-
ISpecification<T> specification = null;
373-
var parameter = Expression.Parameter(typeof(T), "x");
374-
375-
var i = 0;
376-
foreach (var lambda in keys.Select(key => Expression.Lambda<Func<T, bool>>(
377-
Expression.Equal(
378-
Expression.PropertyOrField(parameter, propInfo[i].Name),
379-
Expression.Constant(key)
380-
),
381-
parameter
382-
))
383-
)
384-
{
385-
specification = specification == null ? new Specification<T>(lambda) : specification.And(lambda);
386-
i++;
387-
}
388-
389-
return specification;
390-
}
391-
392302
protected virtual PropertyInfo[] GetPrimaryKeyPropertyInfo()
393303
{
394304
var type = typeof(T);

0 commit comments

Comments
 (0)