Skip to content

Commit 6036231

Browse files
committed
Merge pull request #143 from fiorebat/master
RepositoryFactory methods to get all existing compound key repository available
2 parents c16f86b + ac45a2a commit 6036231

40 files changed

Lines changed: 793 additions & 9 deletions

File tree

SharpRepository.CacheRepository/CacheCompoundKeyRepositoryBase.cs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,164 @@ public override string ToString()
294294
}
295295
}
296296

297+
298+
public abstract class CacheCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> : LinqCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> where T : class, new()
299+
{
300+
private struct CompoundKey
301+
{
302+
public TKey Key1 { private get; set; }
303+
public TKey2 Key2 { private get; set; }
304+
public TKey3 Key3 { private get; set; }
305+
306+
public override int GetHashCode()
307+
{
308+
return Key1.GetHashCode() ^ Key2.GetHashCode() ^ Key3.GetHashCode();
309+
}
310+
311+
public override bool Equals(object obj)
312+
{
313+
if (obj is CompoundKey)
314+
{
315+
var compositeKey = (CompoundKey)obj;
316+
317+
return Key1.Equals(compositeKey.Key1) && Key2.Equals(compositeKey.Key2) && Key3.Equals(compositeKey.Key2);
318+
}
319+
320+
return false;
321+
}
322+
}
323+
324+
private ConcurrentDictionary<CompoundKey, T> Items
325+
{
326+
get
327+
{
328+
ConcurrentDictionary<CompoundKey, T> items = null;
329+
330+
if (!_cachingProvider.Exists(_prefix + ".CacheRepository.Items"))
331+
{
332+
items = new ConcurrentDictionary<CompoundKey, T>();
333+
_cachingProvider.Set(_prefix + ".CacheRepository.Items", items);
334+
}
335+
else
336+
{
337+
if (!_cachingProvider.Get(_prefix + ".CacheRepository.Items", out items))
338+
{
339+
items = new ConcurrentDictionary<CompoundKey, T>();
340+
}
341+
}
342+
343+
return items;
344+
}
345+
set
346+
{
347+
_cachingProvider.Set(_prefix + ".CacheRepository.Items", value);
348+
}
349+
}
350+
351+
private readonly string _prefix;
352+
private readonly ICachingProvider _cachingProvider;
353+
354+
internal CacheCompoundKeyRepositoryBase(string prefix, ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> cachingStrategy = null)
355+
: this(prefix, new InMemoryCachingProvider(), cachingStrategy)
356+
{
357+
}
358+
359+
internal CacheCompoundKeyRepositoryBase(string prefix, ICachingProvider cachingProvider, ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> cachingStrategy = null)
360+
: base(cachingStrategy)
361+
{
362+
_prefix = prefix;
363+
_cachingProvider = cachingProvider;
364+
}
365+
366+
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
367+
{
368+
return CloneDictionary(Items).AsQueryable();
369+
}
370+
371+
protected override T GetQuery(TKey key, TKey2 key2, TKey3 key3)
372+
{
373+
T result;
374+
var compoundKey = new CompoundKey { Key1 = key, Key2 = key2, Key3 = key3 };
375+
Items.TryGetValue(compoundKey, out result);
376+
377+
return result;
378+
}
379+
380+
private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<CompoundKey, T> list)
381+
{
382+
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
383+
// since we can't really put those constraints on T I'm going to do it via reflection
384+
385+
var type = typeof(T);
386+
var properties = type.GetProperties();
387+
388+
var clonedList = new List<T>(list.Count);
389+
390+
foreach (var keyValuePair in list)
391+
{
392+
var newItem = new T();
393+
foreach (var propInfo in properties)
394+
{
395+
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
396+
}
397+
398+
clonedList.Add(newItem);
399+
}
400+
401+
return clonedList;
402+
}
403+
404+
protected override void AddItem(T entity)
405+
{
406+
TKey key;
407+
TKey2 key2;
408+
TKey3 key3;
409+
GetPrimaryKey(entity, out key, out key2, out key3);
410+
411+
var compoundKey = new CompoundKey { Key1 = key, Key2 = key2, Key3 = key3 };
412+
Items[compoundKey] = entity;
413+
}
414+
415+
protected override void DeleteItem(T entity)
416+
{
417+
TKey key;
418+
TKey2 key2;
419+
TKey3 key3;
420+
GetPrimaryKey(entity, out key, out key2, out key3);
421+
422+
T tmp;
423+
var compoundKey = new CompoundKey { Key1 = key, Key2 = key2, Key3 = key3 };
424+
Items.TryRemove(compoundKey, out tmp);
425+
}
426+
427+
protected override void UpdateItem(T entity)
428+
{
429+
TKey key;
430+
TKey2 key2;
431+
TKey3 key3;
432+
GetPrimaryKey(entity, out key, out key2, out key3);
433+
434+
var compoundKey = new CompoundKey { Key1 = key, Key2 = key2, Key3 = key3 };
435+
Items[compoundKey] = entity;
436+
}
437+
438+
protected override void SaveChanges()
439+
{
440+
441+
}
442+
443+
public override void Dispose()
444+
{
445+
446+
}
447+
448+
public override string ToString()
449+
{
450+
return "SharpRepository.InMemoryRepository";
451+
}
452+
}
453+
454+
297455
public abstract class InMemoryCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> : LinqCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> where T : class, new()
298456
{
299457
private struct CompoundKey

SharpRepository.CacheRepository/CacheConfigRepositoryFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@ public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey
2424
{
2525
return new CacheRepository<T, TKey, TKey2>(RepositoryConfiguration["prefix"]);
2626
}
27+
28+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
29+
{
30+
return new CacheRepository<T, TKey, TKey2, TKey3>(RepositoryConfiguration["prefix"]);
31+
}
32+
33+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
34+
{
35+
return new CacheCompoundKeyRepository<T>(RepositoryConfiguration["prefix"]);
36+
}
2737
}
2838
}

SharpRepository.CacheRepository/CacheRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public CacheRepository(string prefix, ICompoundKeyCachingStrategy<T, TKey, TKey2
4444
{
4545
}
4646
}
47+
48+
public class CacheRepository<T, TKey, TKey2, TKey3> : CacheCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> where T : class, new()
49+
{
50+
public CacheRepository(string prefix, ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> cachingStrategy = null)
51+
: base(prefix, cachingStrategy)
52+
{
53+
}
54+
}
4755
}

SharpRepository.CouchDbRepository/SharpRepository.CouchDbRepository.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
</ProjectReference>
6767
</ItemGroup>
6868
<ItemGroup>
69-
<None Include="packages.config" />
69+
<None Include="packages.config">
70+
<SubType>Designer</SubType>
71+
</None>
7072
</ItemGroup>
7173
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7274
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="4.5.10" targetFramework="net40" />
43
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
5-
<package id="Remotion.Linq" version="1.13.171" targetFramework="net40" />
64
<package id="Remotion.Linq" version="1.13.183.0" targetFramework="net40" />
75
</packages>

SharpRepository.Db4oRepository/Db4oConfigRepositoryFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,15 @@ public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey
3232
{
3333
throw new NotImplementedException();
3434
}
35+
36+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
37+
{
38+
throw new NotImplementedException();
39+
}
40+
41+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
42+
{
43+
throw new NotImplementedException();
44+
}
3545
}
3646
}

SharpRepository.EfRepository/EfConfigRepositoryFactory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey
2929
return new EfRepository<T, TKey, TKey2>(GetDbContext());
3030
}
3131

32+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
33+
{
34+
return new EfRepository<T, TKey, TKey2, TKey3>(GetDbContext());
35+
}
36+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
37+
{
38+
return new EfCompoundKeyRepository<T>(GetDbContext());
39+
}
40+
3241
private DbContext GetDbContext()
3342
{
3443
var connectionString = RepositoryConfiguration["connectionString"];

SharpRepository.EfRepository/EfRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ public EfRepository(DbContext dbContext, ICompoundKeyCachingStrategy<T, TKey, TK
1313
}
1414
}
1515

16+
public class EfRepository<T, TKey, TKey2, TKey3> : EfCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> where T : class, new()
17+
{
18+
public EfRepository(DbContext dbContext, ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> cachingStrategy = null)
19+
: base(dbContext, cachingStrategy)
20+
{
21+
}
22+
}
23+
1624
public class EfCompoundKeyRepository<T> : EfCompoundKeyRepositoryBase<T> where T : class, new()
1725
{
1826
public EfCompoundKeyRepository(DbContext dbContext, ICompoundKeyCachingStrategy<T> cachingStrategy = null)

SharpRepository.InMemoryRepository/InMemoryConfigRepositoryFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@ public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey
2424
{
2525
return new InMemoryRepository<T, TKey, TKey2>();
2626
}
27+
28+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
29+
{
30+
return new InMemoryRepository<T, TKey, TKey2, TKey3>();
31+
}
32+
33+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
34+
{
35+
return new InMemoryCompoundKeyRepository<T>();
36+
}
2737
}
2838
}

SharpRepository.InMemoryRepository/InMemoryRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ public InMemoryRepository(ICompoundKeyCachingStrategy<T, TKey, TKey2> cachingStr
3434
{
3535
}
3636
}
37+
38+
public class InMemoryRepository<T, TKey, TKey2, TKey3> : InMemoryCompoundKeyRepositoryBase<T, TKey, TKey2, TKey3> where T : class, new()
39+
{
40+
public InMemoryRepository(ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> cachingStrategy = null)
41+
: base(cachingStrategy)
42+
{
43+
}
44+
}
3745
}

0 commit comments

Comments
 (0)