1
1
namespace MyTested . AspNetCore . Mvc . Plugins
2
2
{
3
3
using System ;
4
+ using System . Linq ;
4
5
using Internal ;
5
6
using Internal . Contracts ;
6
7
using Microsoft . Extensions . Caching . Memory ;
@@ -17,32 +18,18 @@ public class CachingTestPlugin : IServiceRegistrationPlugin
17
18
private readonly Type defaultDistributedCachingServiceType = typeof ( IDistributedCache ) ;
18
19
private readonly Type defaultDistributedCachingImplementationType = typeof ( MemoryDistributedCache ) ;
19
20
20
- private bool shouldReplaceMemoryCache = false ;
21
- private bool shouldReplaceDistributedCache = false ;
22
-
23
21
public Func < ServiceDescriptor , bool > ServiceSelectorPredicate
24
22
=> serviceDescriptor =>
25
23
{
26
- var isValidServiceType =
24
+ var isSupportedService =
27
25
serviceDescriptor . ServiceType == this . defaultCachingServiceType ||
28
26
serviceDescriptor . ServiceType == this . defaultDistributedCachingServiceType ;
29
27
30
- var isValidImplementationType =
28
+ var isSupportedImplementation =
31
29
serviceDescriptor . ImplementationType == this . defaultCachingImplementationType ||
32
30
serviceDescriptor . ImplementationType == this . defaultDistributedCachingImplementationType ;
33
31
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 ;
46
33
} ;
47
34
48
35
public Action < IServiceCollection > ServiceRegistrationDelegate
@@ -53,19 +40,29 @@ public Action<IServiceCollection> ServiceRegistrationDelegate
53
40
{
54
41
CommonValidator . CheckForNullReference ( serviceCollection ) ;
55
42
56
- if ( this . shouldReplaceMemoryCache )
43
+ if ( this . ShouldReplace ( serviceCollection , this . defaultCachingServiceType , this . defaultCachingImplementationType ) )
57
44
{
58
45
serviceCollection . ReplaceMemoryCache ( ) ;
59
46
TestHelper . GlobalTestCleanup += ( ) => TestServiceProvider . GetService < IMemoryCache > ( ) ? . Dispose ( ) ;
60
47
}
61
48
62
- if ( this . shouldReplaceDistributedCache )
49
+ if ( this . ShouldReplace ( serviceCollection , this . defaultDistributedCachingServiceType , this . defaultDistributedCachingImplementationType ) )
63
50
{
64
51
serviceCollection . ReplaceDistributedCache ( ) ;
65
52
TestHelper . GlobalTestCleanup += ( ) => TestServiceProvider . GetService < IDistributedCacheMock > ( ) ? . Dispose ( ) ;
66
53
}
67
54
} ;
68
55
}
69
56
}
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
+
70
67
}
71
68
}
0 commit comments