Skip to content

Commit 489d8d7

Browse files
committed
Hacked caching plugin implementation (#247)
1 parent a9fede7 commit 489d8d7

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Plugins
22
{
33
using System;
4+
using System.Linq;
45
using Internal;
56
using Internal.Contracts;
67
using Microsoft.Extensions.Caching.Memory;
@@ -17,32 +18,18 @@ public class CachingTestPlugin : IServiceRegistrationPlugin
1718
private readonly Type defaultDistributedCachingServiceType = typeof(IDistributedCache);
1819
private readonly Type defaultDistributedCachingImplementationType = typeof(MemoryDistributedCache);
1920

20-
private bool shouldReplaceMemoryCache = false;
21-
private bool shouldReplaceDistributedCache = false;
22-
2321
public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
2422
=> serviceDescriptor =>
2523
{
26-
var isValidServiceType =
24+
var isSupportedService =
2725
serviceDescriptor.ServiceType == this.defaultCachingServiceType ||
2826
serviceDescriptor.ServiceType == this.defaultDistributedCachingServiceType;
2927

30-
var isValidImplementationType =
28+
var isSupportedImplementation =
3129
serviceDescriptor.ImplementationType == this.defaultCachingImplementationType ||
3230
serviceDescriptor.ImplementationType == this.defaultDistributedCachingImplementationType;
3331

34-
if (serviceDescriptor.ServiceType == this.defaultCachingServiceType)
35-
{
36-
this.shouldReplaceMemoryCache = serviceDescriptor.ImplementationType == defaultCachingImplementationType;
37-
}
38-
39-
if (serviceDescriptor.ServiceType == this.defaultDistributedCachingServiceType)
40-
{
41-
this.shouldReplaceDistributedCache = serviceDescriptor.ImplementationType == defaultDistributedCachingImplementationType;
42-
}
43-
44-
45-
return isValidServiceType && isValidImplementationType;
32+
return isSupportedService && isSupportedImplementation;
4633
};
4734

4835
public Action<IServiceCollection> ServiceRegistrationDelegate
@@ -53,19 +40,29 @@ public Action<IServiceCollection> ServiceRegistrationDelegate
5340
{
5441
CommonValidator.CheckForNullReference(serviceCollection);
5542

56-
if (this.shouldReplaceMemoryCache)
43+
if (this.ShouldReplace(serviceCollection, this.defaultCachingServiceType, this.defaultCachingImplementationType))
5744
{
5845
serviceCollection.ReplaceMemoryCache();
5946
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IMemoryCache>()?.Dispose();
6047
}
6148

62-
if (this.shouldReplaceDistributedCache)
49+
if (this.ShouldReplace(serviceCollection, this.defaultDistributedCachingServiceType, this.defaultDistributedCachingImplementationType))
6350
{
6451
serviceCollection.ReplaceDistributedCache();
6552
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IDistributedCacheMock>()?.Dispose();
6653
}
6754
};
6855
}
6956
}
57+
58+
private bool ShouldReplace(IServiceCollection serviceCollection, Type serviceType, Type implementationType)
59+
{
60+
var existingService = serviceCollection
61+
.FirstOrDefault(s => s.ServiceType == serviceType);
62+
63+
return existingService == null ||
64+
existingService.ImplementationType == implementationType;
65+
}
66+
7067
}
7168
}

0 commit comments

Comments
 (0)