Skip to content

Commit aa22d61

Browse files
author
Jeff Treuting
committed
NoCachingStrategy configuration factory
Created a factory for the NoCachingStrategy. This allows there to be a default caching strategy in place and then for a specific repository give it a no caching strategy to override that default. Without this factory there was no way to do that.
1 parent 26622ec commit aa22d61

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SharpRepository.Repository.Configuration;
2+
3+
namespace SharpRepository.Repository.Caching
4+
{
5+
public class NoCachingConfigCachingStrategyFactory : ConfigCachingStrategyFactory
6+
{
7+
public NoCachingConfigCachingStrategyFactory(CachingStrategyElement element)
8+
: base(element)
9+
{
10+
}
11+
12+
public override ICachingStrategy<T, TKey> GetInstance<T, TKey>()
13+
{
14+
return new NoCachingStrategy<T, TKey>();
15+
}
16+
}
17+
}

SharpRepository.Repository/SharpRepository.Repository.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="Caching\Hash\LocalCollectionExpander.cs" />
4646
<Compile Include="Caching\Hash\Utility.cs" />
4747
<Compile Include="Caching\InMemoryConfigCachingProviderFactory.cs" />
48+
<Compile Include="Caching\NoCachingConfigCachingStrategyFactory.cs" />
4849
<Compile Include="Caching\StandardConfigCachingStrategyFactory.cs" />
4950
<Compile Include="Caching\TimeoutCachingStrategyBase.cs" />
5051
<Compile Include="Caching\NoCachingStrategyBase.cs" />

SharpRepository.Tests/App.config

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<sharpRepository>
1515
<repositories default="inMemory">
1616
<repository name="inMemory" factory="SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository" />
17-
<repository name="efRepos" factory="SharpRepository.EfRepository.EfConfigRepositoryFactory, SharpRepository.EfRepository" connectionString="EfConnectionString" cachingStrategy="timeout"/>
17+
<repository name="efRepos" factory="SharpRepository.EfRepository.EfConfigRepositoryFactory, SharpRepository.EfRepository" connectionString="EfConnectionString" dbContextType="SharpRepository.Tests.TestObjects.TestObjectEntities, SharpRepository.Tests" cachingStrategy="timeout"/>
18+
19+
<repository name="inMemoryNoCaching" factory="SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository" cachingStrategy="none" />
1820
</repositories>
1921
<cachingProviders default="inMemoryProvider">
2022
<cachingProvider name="inMemoryProvider" factory="SharpRepository.Repository.Caching.InMemoryConfigCachingProviderFactory, SharpRepository.Repository"/>
@@ -24,6 +26,7 @@
2426
<cachingStrategies default="standard">
2527
<cachingStrategy name="standard" factory="SharpRepository.Repository.Caching.StandardConfigCachingStrategyFactory, SharpRepository.Repository" generational="true" writeThrough="false"/>
2628
<cachingStrategy name="timeout" factory="SharpRepository.Repository.Caching.TimeoutConfigCachingStrategyFactory, SharpRepository.Repository" timeout="200"/>
29+
<cachingStrategy name="none" factory="SharpRepository.Repository.Caching.NoCachingConfigCachingStrategyFactory, SharpRepository.Repository"/>
2730
</cachingStrategies>
2831
</sharpRepository>
2932

SharpRepository.Tests/Configuration/ConfigurationTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Configuration;
33
using NUnit.Framework;
4+
using SharpRepository.Repository.Caching;
45
using SharpRepository.Repository.Configuration;
56
using SharpRepository.Tests.TestObjects;
67
using SharpRepository.InMemoryRepository;
@@ -74,5 +75,23 @@ public void LoadConfigurationRepositoryBySectionAndRepositoryName()
7475
throw new Exception("Not EfRepository");
7576
}
7677
}
78+
79+
[Test]
80+
public void LoadRepositoryDefaultStrategyAndOverrideNone()
81+
{
82+
var repos = RepositoryFactory.GetInstance<Contact, string>();
83+
84+
if (!(repos.CachingStrategy is StandardCachingStrategy<Contact, string>))
85+
{
86+
throw new Exception("Not standard caching default");
87+
}
88+
89+
repos = RepositoryFactory.GetInstance<Contact, string>("inMemoryNoCaching");
90+
91+
if (!(repos.CachingStrategy is NoCachingStrategy<Contact, string>))
92+
{
93+
throw new Exception("Not the override of default for no caching");
94+
}
95+
}
7796
}
7897
}

0 commit comments

Comments
 (0)