|
1 | 1 | namespace MyTested.AspNetCore.Mvc.Plugins
|
2 | 2 | {
|
3 | 3 | using System;
|
4 |
| - using System.Linq; |
5 | 4 | using Internal;
|
6 | 5 | using Internal.Contracts;
|
7 | 6 | using Microsoft.Extensions.Caching.Memory;
|
8 | 7 | using Microsoft.Extensions.DependencyInjection;
|
9 | 8 | using Internal.Services;
|
10 | 9 | using Microsoft.Extensions.Caching.Distributed;
|
11 |
| - using Utilities.Validators; |
12 | 10 |
|
13 | 11 | public class CachingTestPlugin : IServiceRegistrationPlugin
|
14 | 12 | {
|
15 |
| - private readonly Type defaultCachingServiceType = typeof(IMemoryCache); |
16 |
| - private readonly Type defaultCachingImplementationType = typeof(MemoryCache); |
| 13 | + private readonly Type defaultMemoryCacheServiceType = typeof(IMemoryCache); |
| 14 | + private readonly Type defaultMemoryCacheImplementationType = typeof(MemoryCache); |
17 | 15 |
|
18 |
| - private readonly Type defaultDistributedCachingServiceType = typeof(IDistributedCache); |
19 |
| - private readonly Type defaultDistributedCachingImplementationType = typeof(MemoryDistributedCache); |
| 16 | + private readonly Type defaultDistributedCacheServiceType = typeof(IDistributedCache); |
| 17 | + private readonly Type defaultDistributedCacheImplementationType = typeof(MemoryDistributedCache); |
| 18 | + |
| 19 | + private bool replaceMemoryCache = false; |
| 20 | + private bool replaceDistributedCache = false; |
20 | 21 |
|
21 | 22 | public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
|
22 | 23 | => serviceDescriptor =>
|
23 | 24 | {
|
24 |
| - var isSupportedService = |
25 |
| - serviceDescriptor.ServiceType == this.defaultCachingServiceType || |
26 |
| - serviceDescriptor.ServiceType == this.defaultDistributedCachingServiceType; |
| 25 | + this.replaceMemoryCache = this.replaceMemoryCache || |
| 26 | + (serviceDescriptor.ServiceType == this.defaultMemoryCacheServiceType && |
| 27 | + serviceDescriptor.ImplementationType == this.defaultMemoryCacheImplementationType); |
27 | 28 |
|
28 |
| - var isSupportedImplementation = |
29 |
| - serviceDescriptor.ImplementationType == this.defaultCachingImplementationType || |
30 |
| - serviceDescriptor.ImplementationType == this.defaultDistributedCachingImplementationType; |
| 29 | + this.replaceDistributedCache = this.replaceDistributedCache || |
| 30 | + (serviceDescriptor.ServiceType == this.defaultDistributedCacheServiceType && |
| 31 | + serviceDescriptor.ImplementationType == this.defaultDistributedCacheImplementationType); |
31 | 32 |
|
32 |
| - return isSupportedService && isSupportedImplementation; |
| 33 | + return replaceMemoryCache || replaceDistributedCache; |
33 | 34 | };
|
34 | 35 |
|
35 | 36 | public Action<IServiceCollection> ServiceRegistrationDelegate
|
36 |
| - { |
37 |
| - get |
| 37 | + => serviceCollection => |
38 | 38 | {
|
39 |
| - return serviceCollection => |
| 39 | + if (this.replaceMemoryCache) |
40 | 40 | {
|
41 |
| - CommonValidator.CheckForNullReference(serviceCollection); |
42 |
| - |
43 |
| - if (this.ShouldReplace(serviceCollection, this.defaultCachingServiceType, this.defaultCachingImplementationType)) |
44 |
| - { |
45 |
| - serviceCollection.ReplaceMemoryCache(); |
46 |
| - TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IMemoryCache>()?.Dispose(); |
47 |
| - } |
48 |
| - |
49 |
| - if (this.ShouldReplace(serviceCollection, this.defaultDistributedCachingServiceType, this.defaultDistributedCachingImplementationType)) |
50 |
| - { |
51 |
| - serviceCollection.ReplaceDistributedCache(); |
52 |
| - TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IDistributedCacheMock>()?.Dispose(); |
53 |
| - } |
54 |
| - }; |
55 |
| - } |
56 |
| - } |
| 41 | + serviceCollection.ReplaceMemoryCache(); |
| 42 | + TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IMemoryCache>()?.Dispose(); |
| 43 | + } |
57 | 44 |
|
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 |
| - |
| 45 | + if (this.replaceDistributedCache) |
| 46 | + { |
| 47 | + serviceCollection.ReplaceDistributedCache(); |
| 48 | + TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IDistributedCacheMock>()?.Dispose(); |
| 49 | + } |
| 50 | + }; |
67 | 51 | }
|
68 | 52 | }
|
0 commit comments