Skip to content

Commit 569c694

Browse files
author
Jeff Treuting
committed
Ioc and IRepository<T> bug
Fixes SharpRepository#88 I needed to add a specific repository factory override for a single generic type instead of passing typeof(int) into the existing one which in turn returns an IRepository<T,int> which the Ioc can't cast as an IRepository<T>
1 parent 97e978e commit 569c694

18 files changed

Lines changed: 141 additions & 36 deletions

File tree

SharpRepository.CacheRepository/CacheConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public CacheConfigRepositoryFactory(IRepositoryConfiguration config)
1010
{
1111
}
1212

13+
public override IRepository<T> GetInstance<T>()
14+
{
15+
return new CacheRepository<T>(RepositoryConfiguration["prefix"]);
16+
}
17+
1318
public override IRepository<T, TKey> GetInstance<T, TKey>()
1419
{
1520
return new CacheRepository<T, TKey>(RepositoryConfiguration["prefix"]);

SharpRepository.Db4oRepository/Db4oConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public Db4oConfigRepositoryFactory(IRepositoryConfiguration config)
1212
{
1313
}
1414

15+
public override IRepository<T> GetInstance<T>()
16+
{
17+
throw new NotImplementedException("Db4oRepository does not support using IRepository<T> directly to reference a IRepository<T, string>");
18+
}
19+
1520
public override IRepository<T, TKey> GetInstance<T, TKey>()
1621
{
1722
// check for required parameters

SharpRepository.Ef5Repository/Ef5ConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public Ef5ConfigRepositoryFactory(IRepositoryConfiguration config)
1414
{
1515
}
1616

17+
public override IRepository<T> GetInstance<T>()
18+
{
19+
return new Ef5Repository<T>(GetDbContext());
20+
}
21+
1722
public override IRepository<T, TKey> GetInstance<T, TKey>()
1823
{
1924
return new Ef5Repository<T, TKey>(GetDbContext());

SharpRepository.InMemoryRepository/InMemoryConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public InMemoryConfigRepositoryFactory(IRepositoryConfiguration config)
1010
{
1111
}
1212

13+
public override IRepository<T> GetInstance<T>()
14+
{
15+
return new InMemoryRepository<T>();
16+
}
17+
1318
public override IRepository<T, TKey> GetInstance<T, TKey>()
1419
{
1520
return new InMemoryRepository<T, TKey>();

SharpRepository.MongoDbRepository/MongoDbConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public MongoDbConfigRepositoryFactory(IRepositoryConfiguration config)
1212
{
1313
}
1414

15+
public override IRepository<T> GetInstance<T>()
16+
{
17+
throw new NotImplementedException("MongoDbRepository does not support using IRepository<T> directly to reference a IRepository<T, string>");
18+
}
19+
1520
public override IRepository<T, TKey> GetInstance<T, TKey>()
1621
{
1722
// check for required parameters

SharpRepository.RavenDbRepository/RavenDbConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public RavenDbConfigRepositoryFactory(IRepositoryConfiguration config)
1212
{
1313
}
1414

15+
public override IRepository<T> GetInstance<T>()
16+
{
17+
throw new NotImplementedException("RavenDbRepository does not support using IRepository<T> directly to reference a IRepository<T, string>");
18+
}
19+
1520
public override IRepository<T, TKey> GetInstance<T, TKey>()
1621
{
1722
var documentStore =new DocumentStore();

SharpRepository.Repository/Configuration/ConfigRepositoryFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace SharpRepository.Repository.Configuration
33
{
44
public interface IConfigRepositoryFactory
55
{
6+
IRepository<T> GetInstance<T>() where T : class, new();
67
IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new();
78
ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>() where T : class, new();
89
}
@@ -16,6 +17,7 @@ protected ConfigRepositoryFactory(IRepositoryConfiguration config)
1617
RepositoryConfiguration = config;
1718
}
1819

20+
public abstract IRepository<T> GetInstance<T>() where T : class, new();
1921
public abstract IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new();
2022
public abstract ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>() where T : class, new();
2123
}

SharpRepository.Repository/Configuration/ConfigurationHelper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ public static void CheckForInterface(Type type, Type interfaceType)
1313
throw new System.Configuration.ConfigurationErrorsException("The type " + type.AssemblyQualifiedName + " must implement " + interfaceType.AssemblyQualifiedName);
1414
}
1515

16+
public static IRepository<T> GetInstance<T>(ISharpRepositoryConfiguration configuration, string repositoryName) where T : class, new()
17+
{
18+
if (!configuration.HasRepository)
19+
{
20+
throw new Exception("There are no repositories configured");
21+
}
22+
23+
var repositoryConfiguration = configuration.GetRepository(repositoryName);
24+
var repository = repositoryConfiguration.GetInstance<T>();
25+
26+
if (repository == null)
27+
return null;
28+
29+
var strategyConfiguration = configuration.GetCachingStrategy(repositoryConfiguration.CachingStrategy);
30+
if (strategyConfiguration == null)
31+
{
32+
return repository;
33+
}
34+
35+
var cachingStrategy = strategyConfiguration.GetInstance<T, int>();
36+
if (cachingStrategy == null)
37+
{
38+
return repository;
39+
}
40+
41+
var providerConfiguration = configuration.GetCachingProvider(repositoryConfiguration.CachingProvider);
42+
if (providerConfiguration != null)
43+
{
44+
cachingStrategy.CachingProvider = providerConfiguration.GetInstance();
45+
}
46+
47+
repository.CachingStrategy = cachingStrategy;
48+
49+
return repository;
50+
}
51+
1652
public static IRepository<T, TKey> GetInstance<T, TKey>(ISharpRepositoryConfiguration configuration, string repositoryName) where T : class, new()
1753
{
1854
if (!configuration.HasRepository)

SharpRepository.Repository/Configuration/IRepositoryConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IRepositoryConfiguration
1212
IDictionary<string, string> Attributes { get; set; }
1313
string this[string key] { get; }
1414

15+
IRepository<T> GetInstance<T>() where T : class, new();
1516
IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new();
1617
ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>() where T : class, new();
1718
}

SharpRepository.Repository/Configuration/ISharpRepositoryConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface ISharpRepositoryConfiguration
1919
string DefaultCachingProvider { get; set; }
2020
ICachingProviderConfiguration GetCachingProvider(string providerName);
2121

22+
IRepository<T> GetInstance<T>(string repositoryName = null) where T : class, new();
2223
IRepository<T, TKey> GetInstance<T, TKey>(string repositoryName = null) where T : class, new();
2324
ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>(string repositoryName = null) where T : class, new();
2425
}

0 commit comments

Comments
 (0)