Skip to content

Commit 8195bd5

Browse files
committed
- Json configuration files red by configurator
- EfCore configurations working with DbContext from dependency injection or code - InMemoryCaching working getting MemoryCache from DI. - Caching strategies no longer instantiated by default
1 parent e8fe610 commit 8195bd5

39 files changed

Lines changed: 1108 additions & 793 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,4 @@ _Pvt_Extensions
241241
/packages
242242
/packages
243243
/nlog.txt
244+
/SharpRepository.Tests.Integration/Data/Db4o

NunitTests.nunit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<NUnitProject>
2+
<Settings processModel="Default" domainUsage="Default" appbase="C:/Users/omar.piani/Documents/GitHub/SharpRepository/" activeconfig="DEBUG" />
3+
<Config name="DEBUG">
4+
<assembly path="SharpRepository.Tests.Integration/bin/Debug/SharpRepository.Tests.Integration.dll" />
5+
<assembly path="SharpRepository.Tests/bin/Debug/SharpRepository.Tests.dll" />
6+
<assembly path="SharpRepository.Samples/bin/Debug/SharpRepository.Samples.dll" />
7+
</Config>
8+
</NUnitProject>

SharpRepository.CacheRepository/CacheRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace SharpRepository.CacheRepository
55
{
66
public class CacheRepository<T, TKey> : CacheRepositoryBase<T, TKey> where T : class, new()
77
{
8+
89
public CacheRepository(string prefix, ICachingProvider cachingProvider, ICachingStrategy<T, TKey> cachingStrategy = null)
910
: base(prefix, cachingProvider, cachingStrategy)
1011
{
Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
1-
using SharpRepository.Repository.Configuration;
1+
using SharpRepository.Repository;
2+
using SharpRepository.Repository.Caching;
3+
using SharpRepository.Repository.Configuration;
4+
using System;
25

36
namespace SharpRepository.CacheRepository
47
{
58
public class CacheRepositoryConfiguration : RepositoryConfiguration
69
{
7-
public CacheRepositoryConfiguration(string name, string cachingStrategy = null, string cachingProvider = null)
8-
: this(name, "", cachingStrategy, cachingProvider)
9-
{
10+
protected ICachingProvider CachingProviderInstance { get; set; }
11+
12+
public CacheRepositoryConfiguration(string name, ICachingProvider cachingProviderInstance, string cachingStrategy = null, string cachingProvider = null)
13+
: this(name, "", cachingProviderInstance, cachingStrategy, cachingProvider)
14+
{
1015
}
1116

12-
public CacheRepositoryConfiguration(string name, string prefix, string cachingStrategy=null, string cachingProvider=null)
17+
public CacheRepositoryConfiguration(string name, string prefix, ICachingProvider cachingProviderInstance, string cachingStrategy = null, string cachingProvider = null)
1318
: base(name)
1419
{
1520
Prefix = prefix;
1621
CachingStrategy = cachingStrategy;
1722
CachingProvider = cachingProvider;
23+
CachingProviderInstance = cachingProviderInstance;
1824
Factory = typeof(CacheConfigRepositoryFactory);
1925
}
2026

2127
public string Prefix
2228
{
2329
set { Attributes["prefix"] = value; }
2430
}
31+
32+
public override IRepository<T, TKey> GetInstance<T, TKey>()
33+
{
34+
// load up the factory if it exists and use it
35+
var factory = new CacheConfigRepositoryFactory(this, CachingProviderInstance);
36+
37+
return factory.GetInstance<T, TKey>();
38+
}
39+
40+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
41+
{
42+
// load up the factory if it exists and use it
43+
var factory = new CacheConfigRepositoryFactory(this, CachingProviderInstance);
44+
45+
return factory.GetInstance<T, TKey, TKey2>();
46+
}
47+
48+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
49+
{
50+
// load up the factory if it exists and use it
51+
var factory = new CacheConfigRepositoryFactory(this, CachingProviderInstance);
52+
53+
return factory.GetInstance<T, TKey, TKey2, TKey3>();
54+
}
55+
56+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
57+
{
58+
// load up the factory if it exists and use it
59+
var factory = new CacheConfigRepositoryFactory(this, CachingProviderInstance);
60+
61+
return factory.GetCompoundKeyInstance<T>();
62+
}
2563
}
2664
}

SharpRepository.EfCoreRepository/EfCoreConfigRepositoryFactory.cs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ public EfCoreConfigRepositoryFactory(IRepositoryConfiguration config)
1313
{
1414
}
1515

16-
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
16+
public EfCoreConfigRepositoryFactory(IRepositoryConfiguration config, DbContext dbContext)
17+
: base(config)
1718
{
18-
throw new NotImplementedException();
19+
DbContext = dbContext;
1920
}
2021

22+
protected DbContext DbContext { get; set; }
23+
2124
public override IRepository<T> GetInstance<T>()
2225
{
2326
return new EfCoreRepository<T>(GetDbContext());
@@ -38,14 +41,22 @@ public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKe
3841
return new EfCoreRepository<T, TKey, TKey2, TKey3>(GetDbContext());
3942
}
4043

41-
private DbContext GetDbContext()
44+
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
4245
{
43-
var connectionString = RepositoryConfiguration["connectionString"];
46+
return new EfCoreCompoundKeyRepository<T>(GetDbContext());
47+
}
4448

49+
protected DbContext GetDbContext()
50+
{
4551
// check for required parameters
46-
if (RepositoryDependencyResolver.Current == null && string.IsNullOrEmpty(connectionString))
52+
if (RepositoryDependencyResolver.Current == null && DbContext == null)
4753
{
48-
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the EfCoreRepository via the configuration file, unless you set the RepositoryDependencyResolver to use an Ioc container.");
54+
throw new ConfigurationErrorsException("The EfCore repository Factory gets DbContext or DbContextOptionBuilder from RepositoryDependencyResolver containing the Ioc container passing directly DbContextOptions");
55+
}
56+
57+
if (DbContext != null)
58+
{
59+
return DbContext;
4960
}
5061

5162
Type dbContextType = null;
@@ -55,31 +66,28 @@ private DbContext GetDbContext()
5566
{
5667
dbContextType = Type.GetType(tmpDbContextType);
5768
}
58-
69+
5970
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
60-
DbContext dbContext = null;
71+
DbContext dbContext = dbContextType == null
72+
? RepositoryDependencyResolver.Current?.Resolve<DbContext>()
73+
: (DbContext)RepositoryDependencyResolver.Current?.Resolve(dbContextType);
6174

62-
// if there is an IOC dependency resolver configured then use that one to get the DbContext, this will allow sharing of context across multiple repositories if the IOC is configured that way
63-
if (RepositoryDependencyResolver.Current != null)
75+
// if the Ioc container doesn't throw an error but still returns null we need to alert the consumer
76+
if (dbContext != null)
6477
{
65-
dbContext = dbContextType == null
66-
? RepositoryDependencyResolver.Current.Resolve<DbContext>()
67-
: (DbContext)RepositoryDependencyResolver.Current.Resolve(dbContextType);
68-
69-
// if the Ioc container doesn't throw an error but still returns null we need to alert the consumer
70-
if (dbContext == null)
71-
{
72-
throw new RepositoryDependencyResolverException(typeof(DbContext));
73-
}
78+
return dbContext;
7479
}
75-
else // the default way of getting a DbContext if there is no Ioc container setup
80+
81+
var options = RepositoryDependencyResolver.Current.Resolve<DbContextOptions>();
82+
if (options == null)
7683
{
77-
var options = new DbContextOptionsBuilder();
78-
dbContext = dbContextType == null
79-
? new DbContext(options.Options)
80-
: (DbContext)Activator.CreateInstance(dbContextType, connectionString);
84+
throw new RepositoryDependencyResolverException(typeof(DbContext));
8185
}
8286

87+
dbContext = dbContextType == null
88+
? new DbContext(options)
89+
: (DbContext)Activator.CreateInstance(dbContextType, options);
90+
8391
return dbContext;
8492
}
8593
}
Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
using SharpRepository.Repository.Configuration;
1+
using Microsoft.EntityFrameworkCore;
2+
using SharpRepository.Repository;
3+
using SharpRepository.Repository.Configuration;
24
using System;
35

46
namespace SharpRepository.EfCoreRepository
57
{
68
public class EfCoreRepositoryConfiguration : RepositoryConfiguration
79
{
8-
public EfCoreRepositoryConfiguration(string name) : this(name, null, null)
10+
protected DbContext DbContext { get; set; }
11+
12+
public EfCoreRepositoryConfiguration(string name)
13+
: this(name, null, null)
914
{
1015
}
1116

12-
public EfCoreRepositoryConfiguration(string name, string connectionStringOrName)
13-
: this(name, connectionStringOrName, null)
17+
public EfCoreRepositoryConfiguration(string name, DbContext dbContext)
18+
: this(name, null, null)
19+
{
20+
DbContext = dbContext;
21+
}
22+
23+
public EfCoreRepositoryConfiguration(string name, string cachingStrategy = null, string cachingProvider = null)
24+
: base(name)
1425
{
26+
CachingStrategy = cachingStrategy;
27+
CachingProvider = cachingProvider;
28+
Factory = typeof(EfCoreConfigRepositoryFactory);
1529
}
1630

17-
public EfCoreRepositoryConfiguration(string name, string connectionStringOrName, Type dbContextType, string cachingStrategy = null, string cachingProvider = null)
31+
public EfCoreRepositoryConfiguration(string name, DbContext dbContext, string cachingStrategy = null, string cachingProvider = null)
1832
: base(name)
1933
{
20-
ConnectionStringOrName = connectionStringOrName;
21-
DbContextType = dbContextType;
2234
CachingStrategy = cachingStrategy;
2335
CachingProvider = cachingProvider;
36+
DbContext = dbContext;
2437
Factory = typeof(EfCoreConfigRepositoryFactory);
2538
}
2639

@@ -29,15 +42,44 @@ public string ConnectionStringOrName
2942
set { Attributes["connectionString"] = value; }
3043
}
3144

32-
public Type DbContextType
45+
public override IRepository<T> GetInstance<T>()
46+
{
47+
// load up the factory if it exists and use it
48+
var factory = DbContext != null ?
49+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this, DbContext) :
50+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this);
51+
52+
return factory.GetInstance<T>();
53+
}
54+
55+
public override IRepository<T, TKey> GetInstance<T, TKey>()
56+
{
57+
// load up the factory if it exists and use it
58+
var factory = DbContext != null ?
59+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this, DbContext) :
60+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this);
61+
62+
return factory.GetInstance<T, TKey>();
63+
}
64+
65+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
66+
{
67+
// load up the factory if it exists and use it
68+
var factory = DbContext != null ?
69+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this, DbContext) :
70+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this);
71+
72+
return factory.GetInstance<T, TKey, TKey2>();
73+
}
74+
75+
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
3376
{
34-
set
35-
{
36-
if (value == null)
37-
return;
77+
// load up the factory if it exists and use it
78+
var factory = DbContext != null ?
79+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this, DbContext) :
80+
(IConfigRepositoryFactory)Activator.CreateInstance(Factory, this);
3881

39-
Attributes["dbContextType"] = value.AssemblyQualifiedName;
40-
}
82+
return factory.GetInstance<T, TKey, TKey2, TKey3>();
4183
}
4284
}
4385
}

0 commit comments

Comments
 (0)