Skip to content

Commit ca1a539

Browse files
Re-introduced the expiry config element for the app.config caching provider
1 parent 50a29ea commit ca1a539

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/DotNetToolkit.Repository/Internal/ConfigFile/ConfigurationSection.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,26 @@ public IReadOnlyDictionary<Type, Func<IRepositoryInterceptor>> GetInterceptors()
8484

8585
class LoggingProviderElement : TypedConfigurationElementBase<ILoggerProvider> { }
8686

87-
class CachingProviderElement : TypedConfigurationElementBase<ICacheProvider> { }
87+
class CachingProviderElement : TypedConfigurationElementBase<ICacheProvider>
88+
{
89+
private const string ExpiryKey = "expiry";
90+
91+
[ConfigurationProperty(ExpiryKey)]
92+
public ExpiryElement Expiry
93+
{
94+
get { return (ExpiryElement)this[ExpiryKey]; }
95+
}
96+
97+
public override ICacheProvider GetTypedValue()
98+
{
99+
var provider = base.GetTypedValue();
100+
101+
if (Expiry != null)
102+
provider.Expiry = Expiry.Value;
103+
104+
return provider;
105+
}
106+
}
88107

89108
class RepositoryContextFactoryElement : TypedConfigurationElementBase<IRepositoryContextFactory> { }
90109

@@ -168,6 +187,18 @@ protected override string ElementName
168187
}
169188
}
170189

190+
class ExpiryElement : ConfigurationElement
191+
{
192+
private const string ValueKey = "value";
193+
194+
[ConfigurationProperty(ValueKey, IsRequired = true)]
195+
public TimeSpan? Value
196+
{
197+
get { return (TimeSpan?)this[ValueKey]; }
198+
set { this[ValueKey] = value; }
199+
}
200+
}
201+
171202
abstract class TypedConfigurationElementBase<T> : ParameterCollection
172203
{
173204
private const string TypeKey = "type";

test/DotNetToolkit.Repository.Test/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<param name="minLogLevel" value="Debug" />
1414
</loggingProvider>
1515
<cachingProvider type="DotNetToolkit.Repository.Caching.InMemory.InMemoryCacheProvider, DotNetToolkit.Repository.Caching.InMemory">
16-
<param name="expiry" value="00:00:30" />
16+
<expiry value="00:00:30" />
1717
</cachingProvider>
1818
<interceptors>
1919
<interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">

test/DotNetToolkit.Repository.Test/Tests/OptionsBuilderTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,32 @@ public void ThrowsIfConfigurationSectionNotFound()
202202

203203
private static void TestConfiguration(RepositoryOptionsBuilder optionsBuilder)
204204
{
205+
// is configured
205206
Assert.True(optionsBuilder.IsConfigured);
206207

208+
// context factory
207209
Assert.NotNull(optionsBuilder.Options.ContextFactory);
210+
211+
var context = optionsBuilder.Options.ContextFactory.Create() as IInMemoryRepositoryContext;
212+
213+
Assert.NotNull(context);
214+
Assert.Equal("__InMemoryDatabaseName__", context.DatabaseName);
215+
216+
// logging provider
208217
Assert.NotNull(optionsBuilder.Options.LoggerProvider);
218+
219+
// caching provider
209220
Assert.NotNull(optionsBuilder.Options.CachingProvider);
210-
Assert.NotNull(optionsBuilder.Options.CachingProvider.Expiry);
221+
Assert.Equal(optionsBuilder.Options.CachingProvider.Expiry, TimeSpan.FromSeconds(30));
211222

212-
Assert.Equal(1, optionsBuilder.Options.Interceptors.Count());
223+
// interceptor
224+
Assert.Single(optionsBuilder.Options.Interceptors);
213225
Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor)));
226+
227+
var interceptor = optionsBuilder.Options.Interceptors[typeof(TestRepositoryInterceptor)].Value as TestRepositoryInterceptor;
228+
229+
Assert.Equal("random param", interceptor.P1);
230+
Assert.True(interceptor.P2);
214231
}
215232
}
216233
}

test/DotNetToolkit.Repository.Test/repository.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<param name="minLogLevel" value="Debug" />
1414
</loggingProvider>
1515
<cachingProvider type="DotNetToolkit.Repository.Caching.InMemory.InMemoryCacheProvider, DotNetToolkit.Repository.Caching.InMemory">
16-
<param name="expiry" value="00:00:30" />
16+
<expiry value="00:00:30" />
1717
</cachingProvider>
1818
<interceptors>
1919
<interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">

0 commit comments

Comments
 (0)