Skip to content

Commit 7a30af6

Browse files
committed
refactor: IClient support Isolation
1 parent ed1f72d commit 7a30af6

File tree

41 files changed

+169
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+169
-188
lines changed

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Default/CacheClientFactoryBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.Caching;
55

6-
public abstract class CacheClientFactoryBase<TService> : MasaFactoryBase<TService, CacheRelationOptions<TService>>,
6+
public abstract class CacheClientFactoryBase<TService> : MasaFactoryBase<TService, MasaRelationOptions<TService>>,
77
ICacheClientFactory<TService> where TService : class
88
{
99
protected CacheClientFactoryBase(IServiceProvider serviceProvider) : base(serviceProvider)

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Default/DefaultDistributedCacheClientFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protected override string DefaultServiceNotFoundMessage
1010

1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] DistributedCache, it was not found";
1212

13-
protected override MasaFactoryOptions<CacheRelationOptions<IManualDistributedCacheClient>> FactoryOptions => _optionsMonitor.CurrentValue;
13+
protected override MasaFactoryOptions<MasaRelationOptions<IManualDistributedCacheClient>> FactoryOptions => _optionsMonitor.CurrentValue;
1414

1515
private readonly IOptionsMonitor<DistributedCacheFactoryOptions> _optionsMonitor;
1616

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Default/DefaultMultilevelCacheClientFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protected override string DefaultServiceNotFoundMessage
1010

1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] MultilevelCache, it was not found";
1212

13-
protected override MasaFactoryOptions<CacheRelationOptions<IManualMultilevelCacheClient>> FactoryOptions
13+
protected override MasaFactoryOptions<MasaRelationOptions<IManualMultilevelCacheClient>> FactoryOptions
1414
=> _optionsMonitor.CurrentValue;
1515

1616
private readonly IOptionsMonitor<MultilevelCacheFactoryOptions> _optionsMonitor;

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/DistributedCacheBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void UseCustomDistributedCache(
1616
if (options.Options.Any(opt => opt.Name == distributedCacheBuilder.Name))
1717
return;
1818

19-
options.Options.Add(new CacheRelationOptions<IManualDistributedCacheClient>(distributedCacheBuilder.Name, func));
19+
options.Options.Add(new MasaRelationOptions<IManualDistributedCacheClient>(distributedCacheBuilder.Name, func));
2020
});
2121

2222
distributedCacheBuilder.Services.TryAddDistributedCache(distributedCacheBuilder.Name);

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/MultilevelCacheBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void UseCustomMultilevelCache(
1616
if (options.Options.Any(opt => opt.Name == multilevelCacheBuilder.Name))
1717
return;
1818

19-
var cacheRelationOptions = new CacheRelationOptions<IManualMultilevelCacheClient>(multilevelCacheBuilder.Name, func.Invoke);
19+
var cacheRelationOptions = new MasaRelationOptions<IManualMultilevelCacheClient>(multilevelCacheBuilder.Name, func.Invoke);
2020
options.Options.Add(cacheRelationOptions);
2121
});
2222

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs

+31-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ public static void TryAddDistributedCache(
1919
MasaApp.TrySetServiceCollection(services);
2020

2121
services.TryAddTransient<IDistributedCacheClientFactory, DefaultDistributedCacheClientFactory>();
22-
services.TryAddSingleton(serviceProvider
23-
=> serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create());
24-
services.TryAddSingleton(typeof(IDistributedCacheClient), serviceProvider
22+
services.TryAddTransient<IManualDistributedCacheClient>(serviceProvider =>
23+
{
24+
if (serviceProvider.EnableIsolation())
25+
return serviceProvider.GetRequiredService<ScopedService<IManualDistributedCacheClient>>().Service;
26+
27+
return serviceProvider.GetRequiredService<SingletonService<IManualDistributedCacheClient>>().Service;
28+
});
29+
services.TryAddTransient<IDistributedCacheClient>(serviceProvider
2530
=> serviceProvider.GetRequiredService<IManualDistributedCacheClient>());
2631

32+
services.AddCaching();
2733
services.AddTypeAlias(name);
2834
}
2935

@@ -34,11 +40,17 @@ public static void TryAddMultilevelCache(
3440
MasaApp.TrySetServiceCollection(services);
3541

3642
services.TryAddTransient<IMultilevelCacheClientFactory, DefaultMultilevelCacheClientFactory>();
37-
services.TryAddSingleton(serviceProvider
38-
=> serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create());
39-
services.TryAddSingleton(typeof(IMultilevelCacheClient), serviceProvider
43+
services.TryAddTransient<IManualMultilevelCacheClient>(serviceProvider =>
44+
{
45+
if (serviceProvider.EnableIsolation())
46+
return serviceProvider.GetRequiredService<ScopedService<IManualMultilevelCacheClient>>().Service;
47+
48+
return serviceProvider.GetRequiredService<SingletonService<IManualMultilevelCacheClient>>().Service;
49+
});
50+
services.TryAddTransient<IMultilevelCacheClient>(serviceProvider
4051
=> serviceProvider.GetRequiredService<IManualMultilevelCacheClient>());
4152

53+
services.AddCaching();
4254
services.AddTypeAlias(name);
4355
}
4456

@@ -49,4 +61,17 @@ private static void AddTypeAlias(
4961
services.TryAddSingleton<ITypeAliasFactory, DefaultTypeAliasFactory>();
5062
services.Configure<TypeAliasFactoryOptions>(options => options.TryAdd(name));
5163
}
64+
65+
private static void AddCaching(this IServiceCollection services)
66+
{
67+
services.TryAddSingleton<SingletonService<IManualDistributedCacheClient>>(serviceProvider =>
68+
new SingletonService<IManualDistributedCacheClient>(serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create()));
69+
services.TryAddScoped<ScopedService<IManualDistributedCacheClient>>(serviceProvider =>
70+
new ScopedService<IManualDistributedCacheClient>(serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create()));
71+
72+
services.TryAddSingleton<SingletonService<IManualMultilevelCacheClient>>(serviceProvider =>
73+
new SingletonService<IManualMultilevelCacheClient>(serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create()));
74+
services.TryAddSingleton<ScopedService<IManualMultilevelCacheClient>>(serviceProvider =>
75+
new ScopedService<IManualMultilevelCacheClient>(serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create()));
76+
}
5277
}

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Masa.BuildingBlocks.Caching.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\..\Data\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
15+
<ProjectReference Include="..\..\Isolation\Masa.BuildingBlocks.Isolation\Masa.BuildingBlocks.Isolation.csproj" />
1516
</ItemGroup>
1617
</Project>

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/MultilevelCacheClientBase.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,19 @@ public void Remove<T>(params string[] keys)
7373
=> Remove<T>(GetKeys(keys));
7474

7575
public override void Remove<T>(string key, Action<CacheOptions>? action = null)
76-
=> Remove<T>(new[] { key }, action);
76+
=> Remove<T>(new[]
77+
{
78+
key
79+
}, action);
7780

7881
public Task RemoveAsync<T>(params string[] keys)
7982
=> RemoveAsync<T>(GetKeys(keys));
8083

8184
public override Task RemoveAsync<T>(string key, Action<CacheOptions>? action = null)
82-
=> RemoveAsync<T>(new[] { key }, action);
85+
=> RemoveAsync<T>(new[]
86+
{
87+
key
88+
}, action);
8389

8490
public virtual void Set<T>(string key,
8591
T value,

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Options/CacheRelationOptions.cs

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
// ReSharper disable once CheckNamespace
5+
46
namespace Masa.BuildingBlocks.Caching;
57

6-
public class DistributedCacheFactoryOptions : MasaFactoryOptions<CacheRelationOptions<IManualDistributedCacheClient>>
8+
public class DistributedCacheFactoryOptions : MasaFactoryOptions<MasaRelationOptions<IManualDistributedCacheClient>>
79
{
810

911
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
// ReSharper disable once CheckNamespace
5+
46
namespace Masa.BuildingBlocks.Caching;
57

6-
public class MultilevelCacheFactoryOptions: MasaFactoryOptions<CacheRelationOptions<IManualMultilevelCacheClient>>
8+
public class MultilevelCacheFactoryOptions : MasaFactoryOptions<MasaRelationOptions<IManualMultilevelCacheClient>>
79
{
810

911
}

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/_Imports.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
global using Masa.BuildingBlocks.Caching;
55
global using Masa.BuildingBlocks.Data;
6+
global using Masa.BuildingBlocks.Isolation;
67
global using Microsoft.Extensions.DependencyInjection;
78
global using Microsoft.Extensions.DependencyInjection.Extensions;
89
global using Microsoft.Extensions.Options;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Caching")]
5+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.RulesEngine")]
6+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.SearchEngine.AutoComplete")]
7+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Storage.ObjectStorage")]
8+
9+
// ReSharper disable once CheckNamespace
10+
11+
namespace Masa.BuildingBlocks.Isolation;
12+
13+
internal class ScopedService<TService>
14+
{
15+
public TService Service { get; }
16+
17+
public ScopedService(TService service)
18+
{
19+
Service = service;
20+
}
21+
}

src/BuildingBlocks/Isolation/Masa.BuildingBlocks.Isolation/Internal/ServiceProviderExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Caching")]
45
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.RulesEngine")]
5-
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Storage.ObjectStorage")]
66
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.SearchEngine.AutoComplete")]
7+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Storage.ObjectStorage")]
78

89
// ReSharper disable once CheckNamespace
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Caching")]
5+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.RulesEngine")]
6+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.SearchEngine.AutoComplete")]
7+
[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Storage.ObjectStorage")]
8+
9+
// ReSharper disable once CheckNamespace
10+
11+
namespace Masa.BuildingBlocks.Isolation;
12+
13+
internal class SingletonService<TService>
14+
{
15+
public TService Service { get; }
16+
17+
public SingletonService(TService service)
18+
{
19+
Service = service;
20+
}
21+
}

src/BuildingBlocks/RulesEngine/Masa.BuildingBlocks.RulesEngine/Extensions/ServiceCollectionExtensions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ public static IServiceCollection AddRulesEngine(this IServiceCollection services
1515
MasaApp.TrySetServiceCollection(services);
1616
services.TryAddTransient<IRulesEngineFactory, DefaultRulesEngineFactory>();
1717

18-
services.TryAddSingleton<SingletonRulesEngine>(serviceProvider
19-
=> new SingletonRulesEngine(serviceProvider.GetRequiredService<IRulesEngineFactory>().Create()));
20-
services.TryAddScoped<ScopedRulesEngine>(serviceProvider
21-
=> new ScopedRulesEngine(serviceProvider.GetRequiredService<IRulesEngineFactory>().Create()));
18+
services.TryAddSingleton<SingletonService<IRulesEngineClient>>(serviceProvider
19+
=> new SingletonService<IRulesEngineClient>(serviceProvider.GetRequiredService<IRulesEngineFactory>().Create()));
20+
services.TryAddScoped<ScopedService<IRulesEngineClient>>(serviceProvider
21+
=> new ScopedService<IRulesEngineClient>(serviceProvider.GetRequiredService<IRulesEngineFactory>().Create()));
2222

2323
services.TryAddTransient(serviceProvider =>
2424
{
2525
if (serviceProvider.EnableIsolation())
26-
return serviceProvider.GetRequiredService<ScopedRulesEngine>().RulesEngineClient;
26+
return serviceProvider.GetRequiredService<ScopedService<IRulesEngineClient>>().Service;
2727

28-
return serviceProvider.GetRequiredService<SingletonRulesEngine>().RulesEngineClient;
28+
return serviceProvider.GetRequiredService<SingletonService<IRulesEngineClient>>().Service;
2929
});
3030
var rulesEngineOptionsBuilder = new RulesEngineOptionsBuilder(services, name);
3131
action.Invoke(rulesEngineOptionsBuilder);

src/BuildingBlocks/RulesEngine/Masa.BuildingBlocks.RulesEngine/Internal/ScopedRulesEngine.cs

-16
This file was deleted.

src/BuildingBlocks/RulesEngine/Masa.BuildingBlocks.RulesEngine/Internal/SingletonRulesEngine.cs

-16
This file was deleted.

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteClientBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
55

6-
public abstract class AutoCompleteClientBase : IAutoCompleteClient, IDisposable
6+
public abstract class AutoCompleteClientBase : IManualAutoCompleteClient
77
{
88
public abstract Task<bool> BuildAsync(CancellationToken cancellationToken = default);
99

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/DefaultAutoCompleteFactory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
77

8-
public class DefaultAutoCompleteFactory : MasaFactoryBase<IAutoCompleteClient, MasaRelationOptions<IAutoCompleteClient>>, IAutoCompleteFactory
8+
public class DefaultAutoCompleteFactory : MasaFactoryBase<IManualAutoCompleteClient, MasaRelationOptions<IManualAutoCompleteClient>>, IAutoCompleteFactory
99
{
1010
protected override string DefaultServiceNotFoundMessage => "No default AutoComplete found";
1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] AutoComplete, it was not found";
12-
protected override MasaFactoryOptions<MasaRelationOptions<IAutoCompleteClient>> FactoryOptions => _options.CurrentValue;
12+
protected override MasaFactoryOptions<MasaRelationOptions<IManualAutoCompleteClient>> FactoryOptions => _options.CurrentValue;
1313

1414
private readonly IOptionsMonitor<AutoCompleteFactoryOptions> _options;
1515

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Extensions/AutoCompleteOptionsBuilderExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class AutoCompleteOptionsBuilderExtensions
99
{
1010
public static void UseCustomAutoComplete(
1111
this AutoCompleteOptionsBuilder autoCompleteOptionsBuilder,
12-
Func<IServiceProvider, IAutoCompleteClient> implementationFactory)
12+
Func<IServiceProvider, IManualAutoCompleteClient> implementationFactory)
1313
{
1414
MasaArgumentException.ThrowIfNull(implementationFactory);
1515

@@ -19,7 +19,7 @@ public static void UseCustomAutoComplete(
1919
throw new ArgumentException(
2020
$"The {nameof(IAutoCompleteClient)} name already exists, please change the name, the repeat name is [{autoCompleteOptionsBuilder.Name}]");
2121

22-
factoryOptions.Options.Add(new MasaRelationOptions<IAutoCompleteClient>(autoCompleteOptionsBuilder.Name, implementationFactory));
22+
factoryOptions.Options.Add(new MasaRelationOptions<IManualAutoCompleteClient>(autoCompleteOptionsBuilder.Name, implementationFactory));
2323
});
2424
}
2525
}

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Extensions/ServiceCollectionExtensions.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ public static IServiceCollection AddAutoCompleteBySpecifyDocument<TDocument>(
4444
MasaArgumentException.ThrowIfNull(configure);
4545

4646
services.TryAddTransient<IAutoCompleteFactory, DefaultAutoCompleteFactory>();
47-
services.TryAddSingleton<SingletonAutoCompleteClient>(serviceProvider
48-
=> new SingletonAutoCompleteClient(serviceProvider.GetRequiredService<IAutoCompleteFactory>().Create()));
49-
services.TryAddScoped<ScopedAutoCompleteClient>(serviceProvider
50-
=> new ScopedAutoCompleteClient(serviceProvider.GetRequiredService<IAutoCompleteFactory>().Create()));
47+
services.TryAddSingleton<SingletonService<IAutoCompleteClient>>(serviceProvider
48+
=> new SingletonService<IAutoCompleteClient>(serviceProvider.GetRequiredService<IAutoCompleteFactory>().Create()));
49+
services.TryAddScoped<ScopedService<IAutoCompleteClient>>(serviceProvider
50+
=> new ScopedService<IAutoCompleteClient>(serviceProvider.GetRequiredService<IAutoCompleteFactory>().Create()));
5151

52-
services.TryAddTransient(serviceProvider =>
52+
services.TryAddTransient<IManualAutoCompleteClient>(serviceProvider =>
5353
{
5454
if (serviceProvider.EnableIsolation())
55-
return serviceProvider.GetRequiredService<ScopedAutoCompleteClient>().AutoCompleteClient;
55+
return serviceProvider.GetRequiredService<ScopedService<IManualAutoCompleteClient>>().Service;
5656

57-
return serviceProvider.GetRequiredService<SingletonAutoCompleteClient>().AutoCompleteClient;
57+
return serviceProvider.GetRequiredService<SingletonService<IManualAutoCompleteClient>>().Service;
5858
});
59+
services.TryAddTransient<IAutoCompleteClient>(serviceProvider => serviceProvider.GetRequiredService<IManualAutoCompleteClient>());
5960

6061
MasaApp.TrySetServiceCollection(services);
6162

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
55

6-
public interface IAutoCompleteFactory : IMasaFactory<IAutoCompleteClient>
6+
public interface IAutoCompleteFactory : IMasaFactory<IManualAutoCompleteClient>
77
{
88
[Obsolete("Use Create() instead")]
99
IAutoCompleteClient CreateClient();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
5+
6+
public interface IManualAutoCompleteClient : IAutoCompleteClient, IDisposable
7+
{
8+
9+
}

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Internal/ScopedAutoCompleteClient.cs

-16
This file was deleted.

0 commit comments

Comments
 (0)