Skip to content

Commit 2620356

Browse files
author
Jeff Treuting
committed
More flexibility in using the RepositoryFactory
Now you can either load it up from the configuration file, or you can pass in a configuration object that is a similar structure (list of repositories, strategies and providers), The configuration section and the configuration object both use the same interface so the logic for loading the repository is using that interface. This is how enyim's memcached configuration works and I think it provides osme nice options.
1 parent db8de01 commit 2620356

File tree

47 files changed

+829
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+829
-96
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using SharpRepository.Repository.Configuration;
2+
3+
namespace SharpRepository.Caching.Memcached
4+
{
5+
public class MemCachedCachingProviderConfiguration : CachingProviderConfiguration
6+
{
7+
public MemCachedCachingProviderConfiguration(string name, string sectionName)
8+
{
9+
Name = name;
10+
SectionName = sectionName;
11+
Factory = typeof (MemCachedConfigCachingProviderFactory);
12+
}
13+
14+
public string SectionName
15+
{
16+
set { Attributes["sectionName"] = value; }
17+
}
18+
}
19+
}

SharpRepository.Caching.Memcached/MemCachedConfigCachingProviderFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ namespace SharpRepository.Caching.Memcached
66
{
77
public class MemCachedConfigCachingProviderFactory : ConfigCachingProviderFactory
88
{
9-
public MemCachedConfigCachingProviderFactory(CachingProviderElement element)
10-
: base(element)
9+
public MemCachedConfigCachingProviderFactory(ICachingProviderConfiguration config)
10+
: base(config)
1111
{
1212
}
1313

1414
public override ICachingProvider GetInstance()
1515
{
16-
if (String.IsNullOrEmpty(CachingProviderElement["sectionName"]))
16+
if (String.IsNullOrEmpty(CachingProviderConfiguration["sectionName"]))
1717
{
1818
throw new ArgumentException("sectionName is required to load the MemCachedCachingProvider");
1919
}
2020

21-
return new MemcachedCachingProvider(CachingProviderElement["sectionName"]);
21+
return new MemcachedCachingProvider(CachingProviderConfiguration["sectionName"]);
2222
}
2323
}
2424
}

SharpRepository.Caching.Memcached/SharpRepository.Caching.Memcached.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Compile Include="MemCachedCachingProvider.cs" />
44+
<Compile Include="MemCachedCachingProviderConfiguration.cs" />
4445
<Compile Include="MemCachedConfigCachingProviderFactory.cs" />
4546
<Compile Include="Properties\AssemblyInfo.cs" />
4647
</ItemGroup>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using SharpRepository.Repository.Configuration;
2+
3+
namespace SharpRepository.Caching.Redis
4+
{
5+
public class RedisCachingProviderConfiguration : CachingProviderConfiguration
6+
{
7+
public RedisCachingProviderConfiguration(string name)
8+
{
9+
Name = name;
10+
Factory = typeof(RedisConfigCachingProviderFactory);
11+
}
12+
13+
public RedisCachingProviderConfiguration(string name, string host, int port) : this(name, host, port, null)
14+
{
15+
}
16+
17+
public RedisCachingProviderConfiguration(string name, string host, int port, string password)
18+
{
19+
Name = name;
20+
Host = host;
21+
Port = port;
22+
Password = password;
23+
Factory = typeof (RedisConfigCachingProviderFactory);
24+
}
25+
26+
public string Host
27+
{
28+
set { Attributes["host"] = value; }
29+
}
30+
31+
public int Port
32+
{
33+
set { Attributes["post"] = value.ToString(); }
34+
}
35+
36+
public string Password
37+
{
38+
set { Attributes["password"] = value; }
39+
}
40+
}
41+
}

SharpRepository.Caching.Redis/RedisConfigCachingProviderFactory.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace SharpRepository.Caching.Redis
66
{
77
public class RedisConfigCachingProviderFactory : ConfigCachingProviderFactory
88
{
9-
public RedisConfigCachingProviderFactory(CachingProviderElement element)
10-
: base(element)
9+
public RedisConfigCachingProviderFactory(ICachingProviderConfiguration config)
10+
: base(config)
1111
{
1212
}
1313

@@ -16,24 +16,24 @@ public override ICachingProvider GetInstance()
1616
int port;
1717

1818
// this seems like a dumb way to do this :)
19-
if (!String.IsNullOrEmpty(CachingProviderElement["password"]))
19+
if (!String.IsNullOrEmpty(CachingProviderConfiguration["password"]))
2020
{
21-
if (!Int32.TryParse(CachingProviderElement["port"], out port))
21+
if (!Int32.TryParse(CachingProviderConfiguration["port"], out port))
2222
{
2323
throw new ArgumentException("port");
2424
}
2525

26-
return new RedisCachingProvider(CachingProviderElement["host"], port, CachingProviderElement["password"]);
26+
return new RedisCachingProvider(CachingProviderConfiguration["host"], port, CachingProviderConfiguration["password"]);
2727
}
2828

29-
if (Int32.TryParse(CachingProviderElement["port"], out port))
29+
if (Int32.TryParse(CachingProviderConfiguration["port"], out port))
3030
{
31-
return new RedisCachingProvider(CachingProviderElement["host"], port);
31+
return new RedisCachingProvider(CachingProviderConfiguration["host"], port);
3232
}
3333

34-
if (!String.IsNullOrEmpty(CachingProviderElement["host"]))
34+
if (!String.IsNullOrEmpty(CachingProviderConfiguration["host"]))
3535
{
36-
return new RedisCachingProvider(CachingProviderElement["host"]);
36+
return new RedisCachingProvider(CachingProviderConfiguration["host"]);
3737
}
3838

3939
return new RedisCachingProvider();

SharpRepository.Caching.Redis/SharpRepository.Caching.Redis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<ItemGroup>
5151
<Compile Include="RedisCachingProvider.cs" />
5252
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="RedisCachingProviderConfiguration.cs" />
5354
<Compile Include="RedisConfigCachingProviderFactory.cs" />
5455
</ItemGroup>
5556
<ItemGroup>

SharpRepository.Ef5Repository/Ef5ConfigRepositoryFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ namespace SharpRepository.Ef5Repository
88
{
99
public class Ef5ConfigRepositoryFactory : ConfigRepositoryFactory
1010
{
11-
public Ef5ConfigRepositoryFactory(RepositoryElement repositoryElement)
12-
: base(repositoryElement)
11+
public Ef5ConfigRepositoryFactory(IRepositoryConfiguration config)
12+
: base(config)
1313
{
1414
}
1515

1616
public override IRepository<T, TKey> GetInstance<T, TKey>()
1717
{
1818
// check for required parameters
19-
if (String.IsNullOrEmpty(RepositoryElement["connectionString"]))
19+
if (String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
2020
{
2121
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the Ef5Repository via the configuration file.");
2222
}
2323

2424
Type dbContextType = null;
2525

26-
if (!String.IsNullOrEmpty(RepositoryElement["dbContextType"]))
26+
if (!String.IsNullOrEmpty(RepositoryConfiguration["dbContextType"]))
2727
{
28-
dbContextType = Type.GetType(RepositoryElement["dbContextType"]);
28+
dbContextType = Type.GetType(RepositoryConfiguration["dbContextType"]);
2929
}
3030

31-
var connectionString = RepositoryElement["connectionString"];
31+
var connectionString = RepositoryConfiguration["connectionString"];
3232

3333
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
3434
var dbContext = dbContextType == null ?
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using SharpRepository.Repository.Configuration;
3+
4+
namespace SharpRepository.Ef5Repository
5+
{
6+
public class Ef5RepositoryConfiguration : RepositoryConfiguration
7+
{
8+
public Ef5RepositoryConfiguration(string name) : this(name, null, null)
9+
{
10+
}
11+
12+
public Ef5RepositoryConfiguration(string name, string connectionStringOrName)
13+
: this(name, connectionStringOrName, null)
14+
{
15+
}
16+
17+
public Ef5RepositoryConfiguration(string name, string connectionStringOrName, Type dbContextType, string cachingStrategy=null, string cachingProvider=null)
18+
{
19+
Name = name;
20+
ConnectionStringOrName = connectionStringOrName;
21+
DbContextType = dbContextType;
22+
CachingStrategy = cachingStrategy;
23+
CachingProvider = cachingProvider;
24+
Factory = typeof(Ef5ConfigRepositoryFactory);
25+
}
26+
27+
public string ConnectionStringOrName
28+
{
29+
set { Attributes["connectionString"] = value; }
30+
}
31+
32+
public Type DbContextType
33+
{
34+
set
35+
{
36+
if (value == null)
37+
return;
38+
39+
Attributes["dbContextType"] = value.FullName;
40+
}
41+
}
42+
}
43+
}

SharpRepository.Ef5Repository/SharpRepository.Ef5Repository.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Ef5ConfigRepositoryFactory.cs" />
5454
<Compile Include="Ef5Repository.cs" />
5555
<Compile Include="Ef5RepositoryBase.cs" />
56+
<Compile Include="Ef5RepositoryConfiguration.cs" />
5657
<Compile Include="Properties\AssemblyInfo.cs" />
5758
</ItemGroup>
5859
<ItemGroup>

SharpRepository.EfRepository/EfConfigRepositoryFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ namespace SharpRepository.EfRepository
88
{
99
public class EfConfigRepositoryFactory : ConfigRepositoryFactory
1010
{
11-
public EfConfigRepositoryFactory(RepositoryElement repositoryElement)
12-
: base(repositoryElement)
11+
public EfConfigRepositoryFactory(IRepositoryConfiguration config)
12+
: base(config)
1313
{
1414
}
1515

1616
public override IRepository<T, TKey> GetInstance<T, TKey>()
1717
{
1818
// check for required parameters
19-
if (String.IsNullOrEmpty(RepositoryElement["connectionString"]))
19+
if (String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
2020
{
2121
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the EfRepository via the configuration file.");
2222
}
2323

2424
Type dbContextType = null;
2525

26-
if (!String.IsNullOrEmpty(RepositoryElement["dbContextType"]))
26+
if (!String.IsNullOrEmpty(RepositoryConfiguration["dbContextType"]))
2727
{
28-
dbContextType = Type.GetType(RepositoryElement["dbContextType"]);
28+
dbContextType = Type.GetType(RepositoryConfiguration["dbContextType"]);
2929
}
3030

31-
var connectionString = RepositoryElement["connectionString"];
31+
var connectionString = RepositoryConfiguration["connectionString"];
3232

3333
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
3434
var dbContext = dbContextType == null ?

0 commit comments

Comments
 (0)