Skip to content

Commit de8738e

Browse files
author
Jeff Treuting
committed
Worked the compound key stuff into the configuration and RepositoryFactory
Now you can load a compound key repository via the configuration file or structuremap, etc.
1 parent 65269fe commit de8738e

34 files changed

Lines changed: 345 additions & 59 deletions

SharpRepository.Db4oRepository/Db4oConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
2222

2323
return new Db4oRepository<T, TKey>(RepositoryConfiguration["directory"]);
2424
}
25+
26+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
27+
{
28+
throw new NotImplementedException();
29+
}
2530
}
2631
}

SharpRepository.Ef5Repository/Ef5ConfigRepositoryFactory.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,30 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
3737

3838
return new Ef5Repository<T, TKey>(dbContext);
3939
}
40+
41+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
42+
{
43+
// check for required parameters
44+
if (String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
45+
{
46+
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the Ef5Repository via the configuration file.");
47+
}
48+
49+
Type dbContextType = null;
50+
51+
if (!String.IsNullOrEmpty(RepositoryConfiguration["dbContextType"]))
52+
{
53+
dbContextType = Type.GetType(RepositoryConfiguration["dbContextType"]);
54+
}
55+
56+
var connectionString = RepositoryConfiguration["connectionString"];
57+
58+
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
59+
var dbContext = dbContextType == null ?
60+
new DbContext(connectionString) :
61+
(DbContext)Activator.CreateInstance(dbContextType, connectionString);
62+
63+
return new Ef5Repository<T, TKey, TKey2>(dbContext);
64+
}
4065
}
4166
}

SharpRepository.InMemoryRepository/InMemoryConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
1414
{
1515
return new InMemoryRepository<T, TKey>();
1616
}
17+
18+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
19+
{
20+
return new InMemoryRepository<T, TKey, TKey2>();
21+
}
1722
}
1823
}

SharpRepository.Ioc.StructureMap/StructureMapExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ public static LambdaInstance<object> ForRepositoriesUseSharpRepository(this IIni
1111
{
1212
initialization.Scan(scan => scan.IncludeNamespaceContainingType<IAmInRepository>());
1313

14+
initialization.For(typeof(ICompoundKeyRepository<,,>))
15+
.Use(context =>
16+
{
17+
var entityType = context.BuildStack.Current.RequestedType.GetGenericArguments()[0];
18+
var keyType = context.BuildStack.Current.RequestedType.GetGenericArguments()[1];
19+
var key2Type = context.BuildStack.Current.RequestedType.GetGenericArguments()[2];
20+
21+
return RepositoryFactory.GetInstance(entityType, keyType, key2Type, repositoryName);
22+
}
23+
);
24+
1425
return initialization.For(typeof(IRepository<,>))
1526
.Use(context =>
1627
{
@@ -26,6 +37,17 @@ public static LambdaInstance<object> ForRepositoriesUseSharpRepository(this IIni
2637
{
2738
initialization.Scan(scan => scan.IncludeNamespaceContainingType<IAmInRepository>());
2839

40+
initialization.For(typeof(ICompoundKeyRepository<,,>))
41+
.Use(context =>
42+
{
43+
var entityType = context.BuildStack.Current.RequestedType.GetGenericArguments()[0];
44+
var keyType = context.BuildStack.Current.RequestedType.GetGenericArguments()[1];
45+
var key2Type = context.BuildStack.Current.RequestedType.GetGenericArguments()[2];
46+
47+
return RepositoryFactory.GetInstance(entityType, keyType, key2Type, configuration);
48+
}
49+
);
50+
2951
return initialization.For(typeof(IRepository<,>))
3052
.Use(context =>
3153
{

SharpRepository.MongoDbRepository/MongoDbConfigRepositoryFactory.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Configuration;
4-
using System.Linq;
5-
using System.Text;
63
using SharpRepository.Repository;
74
using SharpRepository.Repository.Configuration;
85

@@ -25,5 +22,10 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
2522

2623
return new MongoDbRepository<T, TKey>(RepositoryConfiguration["connectionString"]);
2724
}
25+
26+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
27+
{
28+
throw new NotImplementedException();
29+
}
2830
}
2931
}

SharpRepository.RavenDbRepository/RavenDbConfigRepositoryFactory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,22 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
2727

2828
return new RavenDbRepository<T, TKey>(documentStore);
2929
}
30+
31+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
32+
{
33+
throw new NotImplementedException();
34+
// var documentStore =new DocumentStore();
35+
//
36+
// if (!String.IsNullOrEmpty(RepositoryConfiguration["connectionStringName"]))
37+
// {
38+
// documentStore.ConnectionStringName = RepositoryConfiguration["connectionStringName"];
39+
// }
40+
// else if (!String.IsNullOrEmpty(RepositoryConfiguration["url"]))
41+
// {
42+
// documentStore.Url = RepositoryConfiguration["url"];
43+
// }
44+
//
45+
// return new RavenDbRepository<T, TKey, TKey2>(documentStore);
46+
}
3047
}
3148
}

SharpRepository.Repository/Caching/NoCachingConfigCachingStrategyFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ public override ICachingStrategy<T, TKey> GetInstance<T, TKey>()
1313
{
1414
return new NoCachingStrategy<T, TKey>();
1515
}
16+
17+
public override ICompoundKeyCachingStrategy<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
18+
{
19+
return new NoCachingStrategy<T, TKey, TKey2>();
20+
}
1621
}
1722
}

SharpRepository.Repository/Caching/StandardConfigCachingStrategyFactory.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,23 @@ public override ICachingStrategy<T, TKey> GetInstance<T, TKey>()
2727

2828
return strategy;
2929
}
30+
31+
public override ICompoundKeyCachingStrategy<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
32+
{
33+
var strategy = new StandardCachingStrategy<T, TKey, TKey2>();
34+
35+
bool enabled;
36+
if (Boolean.TryParse(CachingStrategyConfiguration["generational"], out enabled))
37+
{
38+
strategy.GenerationalCachingEnabled = enabled;
39+
}
40+
41+
if (Boolean.TryParse(CachingStrategyConfiguration["writeThrough"], out enabled))
42+
{
43+
strategy.WriteThroughCachingEnabled = enabled;
44+
}
45+
46+
return strategy;
47+
}
3048
}
3149
}

SharpRepository.Repository/Caching/TimeoutConfigCachingStrategyFactory.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ public override ICachingStrategy<T, TKey> GetInstance<T, TKey>()
2020
throw new ConfigurationErrorsException("The timeout attribute is required in order to use the TimeoutCachingStrategy via the configuration file.");
2121
}
2222

23-
return new TimeoutCachingStrategy<T, TKey>(timeout);
23+
return new TimeoutCachingStrategy<T, TKey>(timeout);
24+
}
25+
26+
public override ICompoundKeyCachingStrategy<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
27+
{
28+
int timeout;
29+
if (!Int32.TryParse(CachingStrategyConfiguration["timeout"], out timeout))
30+
{
31+
32+
throw new ConfigurationErrorsException("The timeout attribute is required in order to use the TimeoutCachingStrategy via the configuration file.");
33+
}
34+
35+
return new TimeoutCachingStrategy<T, TKey, TKey2>(timeout);
2436
}
2537
}
2638
}

SharpRepository.Repository/CompoundKeyRepositoryBase.cs

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,8 @@ protected virtual bool GetPrimaryKey(T entity, out TKey key, out TKey2 key2)
358358
if (propInfo == null || propInfo.Length != 2)
359359
return false;
360360

361-
if (propInfo[0].GetValue(entity, null) is TKey && propInfo[1].GetValue(entity, null) is TKey2)
362-
{
363-
key = (TKey) propInfo[0].GetValue(entity, null);
364-
key2 = (TKey2) propInfo[1].GetValue(entity, null);
365-
}
366-
else
367-
{
368-
key2 = (TKey2)propInfo[0].GetValue(entity, null);
369-
key = (TKey)propInfo[1].GetValue(entity, null);
370-
}
361+
key = (TKey) propInfo[0].GetValue(entity, null);
362+
key2 = (TKey2) propInfo[1].GetValue(entity, null);
371363

372364
return true;
373365
}
@@ -380,16 +372,8 @@ protected virtual bool SetPrimaryKey(T entity, TKey key, TKey2 key2)
380372
if (propInfo == null || propInfo.Length != 2)
381373
return false;
382374

383-
if (propInfo[0].GetValue(entity, null) is TKey && propInfo[1].GetValue(entity, null) is TKey2)
384-
{
385-
propInfo[0].SetValue(entity, key, null);
386-
propInfo[1].SetValue(entity, key2, null);
387-
}
388-
else
389-
{
390-
propInfo[0].SetValue(entity, key2, null);
391-
propInfo[1].SetValue(entity, key, null);
392-
}
375+
propInfo[0].SetValue(entity, key, null);
376+
propInfo[1].SetValue(entity, key2, null);
393377

394378
return true;
395379
}
@@ -400,43 +384,19 @@ protected virtual ISpecification<T> ByPrimaryKeySpecification(TKey key, TKey2 ke
400384
if (propInfo == null || propInfo.Length != 2)
401385
return null;
402386

403-
Expression<Func<T, bool>> lambda, lambda2;
404-
405-
if (propInfo[0].PropertyType == typeof(TKey) && propInfo[1].PropertyType == typeof(TKey2))
406-
{
407-
lambda = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[0].Name, key));
408-
lambda2 = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[1].Name, key2));
409-
}
410-
else
411-
{
412-
lambda = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[1].Name, key));
413-
lambda2 = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[0].Name, key2));
414-
}
387+
var lambda = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[0].Name, key));
388+
var lambda2 = Linq.DynamicExpression.ParseLambda<T, bool>(String.Format("{0} == {1}", propInfo[1].Name, key2));
415389

416390
return new Specification<T>(lambda).And(lambda2);
417391
}
418392

419393
protected PropertyInfo[] GetPrimaryKeyPropertyInfo()
420394
{
421-
// checks for properties in this order that match TKey type
422-
// 1) RepositoryPrimaryKeyAttribute
423-
// 2) Id
424-
// 3) [Type Name]Id
425395
var type = typeof(T);
426-
var keyType = typeof(TKey);
427-
var keyType2 = typeof (TKey2);
428396

429-
return type.GetProperties().Where(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>() && (x.PropertyType == keyType || x.PropertyType == keyType2)).ToArray();
397+
return type.GetProperties().Where(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>()).OrderBy(x => x.GetOneAttribute<RepositoryPrimaryKeyAttribute>().Order).ToArray();
430398
}
431399

432-
// private static PropertyInfo GetPropertyCaseInsensitive(IReflect type, string propertyName, Type propertyType)
433-
// {
434-
// // make the property reflection lookup case insensitive
435-
// const BindingFlags bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance;
436-
//
437-
// return type.GetProperty(propertyName, bindingFlags, null, propertyType, new Type[0], new ParameterModifier[0]);
438-
// }
439-
440400
public abstract IEnumerator<T> GetEnumerator();
441401
IEnumerator IEnumerable.GetEnumerator()
442402
{

0 commit comments

Comments
 (0)