Skip to content

Commit ca8f79c

Browse files
committed
chore: Refactoring of Serialization and Identity
1 parent 4ec7a97 commit ca8f79c

File tree

44 files changed

+366
-516
lines changed

Some content is hidden

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

44 files changed

+366
-516
lines changed

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/DefaultDeserializerFactory.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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.Data;
57

6-
public class DefaultDeserializerFactory : MasaFactoryBase<IDeserializer, DeserializerRelationOptions>,
8+
public class DefaultDeserializerFactory : MasaFactoryBase<IDeserializer, MasaRelationOptions<IDeserializer>>,
79
IDeserializerFactory
810
{
911
protected override string DefaultServiceNotFoundMessage => "Default deserializer not found, you need to add it, like services.AddJson()";
1012

1113
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] deserializer, it was not found";
12-
protected override MasaFactoryOptions<DeserializerRelationOptions> FactoryOptions => _options.CurrentValue;
14+
protected override MasaFactoryOptions<MasaRelationOptions<IDeserializer>> FactoryOptions => _options.CurrentValue;
1315

1416
private readonly IOptionsMonitor<DeserializerFactoryOptions> _options;
1517

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/DefaultSerializerFactory.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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.Data;
57

6-
public class DefaultSerializerFactory : MasaFactoryBase<ISerializer, SerializerRelationOptions>,
8+
public class DefaultSerializerFactory : MasaFactoryBase<ISerializer, MasaRelationOptions<ISerializer>>,
79
ISerializerFactory
810
{
911
protected override string DefaultServiceNotFoundMessage => "Default serializer not found, you need to add it, like services.AddJson()";
1012

1113
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] serializer, it was not found";
12-
protected override MasaFactoryOptions<SerializerRelationOptions> FactoryOptions => _options.CurrentValue;
14+
protected override MasaFactoryOptions<MasaRelationOptions<ISerializer>> FactoryOptions => _options.CurrentValue;
1315

1416
private readonly IOptionsMonitor<SerializerFactoryOptions> _options;
1517

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/Extensions/ServiceCollectionExtensions.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
// ReSharper disable once CheckNamespace
5+
56
namespace Microsoft.Extensions.DependencyInjection;
67

78
public static partial class ServiceCollectionExtensions
89
{
9-
public static void TryAddSerializationCore(this IServiceCollection services)
10+
public static IServiceCollection AddSerialization(
11+
this IServiceCollection services,
12+
Action<SerializationBuilder> configure)
13+
=> services.AddSerialization(Options.Options.DefaultName, configure);
14+
15+
public static IServiceCollection AddSerialization(
16+
this IServiceCollection services,
17+
string name,
18+
Action<SerializationBuilder> configure)
19+
{
20+
MasaApp.TrySetServiceCollection(services);
21+
var builder = new SerializationBuilder(name, services);
22+
configure.Invoke(builder);
23+
return services.AddSerializationCore();
24+
}
25+
26+
private static IServiceCollection AddSerializationCore(this IServiceCollection services)
1027
{
1128
services.TryAddSingleton<IDeserializerFactory, DefaultDeserializerFactory>();
1229
services.TryAddSingleton<ISerializerFactory, DefaultSerializerFactory>();
1330
services.TryAddSingleton(serviceProvider => serviceProvider.GetRequiredService<ISerializerFactory>().Create());
1431
services.TryAddSingleton(serviceProvider => serviceProvider.GetRequiredService<IDeserializerFactory>().Create());
1532
services.AddServiceFactory();
33+
return services;
1634
}
1735
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Contrib.Data.Serialization.Json")]
5+
[assembly: InternalsVisibleTo("Masa.Contrib.Data.Serialization.Yaml")]
6+
7+
// ReSharper disable once CheckNamespace
8+
9+
namespace Masa.BuildingBlocks.Data;
10+
11+
internal static class SerializationBuilderExtensions
12+
{
13+
public static void UseSerialization(
14+
this SerializationBuilder serializationBuilder,
15+
Func<IServiceProvider, ISerializer> serializerFunc,
16+
Func<IServiceProvider, IDeserializer> deserializerFunc)
17+
{
18+
serializationBuilder.Services.Configure<SerializerFactoryOptions>(
19+
options => options.TryAdd(serializationBuilder.Name, serializerFunc));
20+
21+
serializationBuilder.Services.Configure<DeserializerFactoryOptions>(
22+
options => options.TryAdd(serializationBuilder.Name, deserializerFunc));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
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.Data;
57

6-
public class DeserializerFactoryOptions : MasaFactoryOptions<DeserializerRelationOptions>
8+
public class DeserializerFactoryOptions : MasaFactoryOptions<MasaRelationOptions<IDeserializer>>
79
{
8-
public DeserializerFactoryOptions MappingDeserializer(string name, Func<IServiceProvider, IDeserializer> func)
10+
public void TryAdd(string name, Func<IServiceProvider, IDeserializer> func)
911
{
10-
var builder = Options.FirstOrDefault(b => b.Name == name.ToLower());
11-
if (builder != null) builder.Func = func;
12-
else Options.Add(new DeserializerRelationOptions(name.ToLower(), func));
13-
return this;
14-
}
12+
if (Options.Any(opt => opt.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
13+
throw new ArgumentException(
14+
$"The deserializer name already exists, please change the name, the repeat name is [{name}]");
1515

16-
public Func<IServiceProvider, IDeserializer>? GetDeserializer()
17-
=> GetDeserializer(Microsoft.Extensions.Options.Options.DefaultName);
18-
19-
public Func<IServiceProvider, IDeserializer>? GetDeserializer(string name)
20-
{
21-
var deserializer = Options.FirstOrDefault(b => b.Name == name.ToLower());
22-
return deserializer?.Func;
16+
Options.Add(new MasaRelationOptions<IDeserializer>(name, func));
2317
}
2418
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/Options/DeserializerRelationOptions.cs

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
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.Data;
57

6-
public class SerializerFactoryOptions : MasaFactoryOptions<SerializerRelationOptions>
8+
public class SerializerFactoryOptions : MasaFactoryOptions<MasaRelationOptions<ISerializer>>
79
{
8-
public SerializerFactoryOptions MappingSerializer(string name, Func<IServiceProvider, ISerializer> func)
10+
public void TryAdd(string name, Func<IServiceProvider, ISerializer> func)
911
{
10-
var builder = Options.FirstOrDefault(b => b.Name == name.ToLower());
11-
if (builder != null) builder.Func = func;
12-
else Options.Add(new SerializerRelationOptions(name.ToLower(), func));
13-
return this;
14-
}
12+
if (Options.Any(opt => opt.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
13+
throw new ArgumentException(
14+
$"The serializer name already exists, please change the name, the repeat name is [{name}]");
1515

16-
public Func<IServiceProvider, ISerializer>? GetSerializer()
17-
=> GetSerializer(Microsoft.Extensions.Options.Options.DefaultName);
18-
19-
public Func<IServiceProvider, ISerializer>? GetSerializer(string name)
20-
{
21-
var serializer = Options.FirstOrDefault(b => b.Name == name.ToLower());
22-
return serializer?.Func;
16+
Options.Add(new MasaRelationOptions<ISerializer>(name, func));
2317
}
2418
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/Options/SerializerRelationOptions.cs

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.Data;
5+
6+
public class SerializationBuilder
7+
{
8+
public string Name { get; }
9+
10+
public IServiceCollection Services { get; }
11+
12+
public SerializationBuilder(string name, IServiceCollection services)
13+
{
14+
Name = name;
15+
Services = services;
16+
}
17+
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/DefaultTypeConvertFactory.cs

-22
This file was deleted.

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/DefaultTypeConvertProvider.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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-
namespace Masa.Contrib.Data;
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.Data;
57

68
public class DefaultTypeConvertProvider : ITypeConvertProvider
79
{

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/Extensions/ServiceCollectionExtensions.cs

-38
This file was deleted.

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/Interfaces/ITypeConvertFactory.cs

-8
This file was deleted.

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/Options/TypeConvertFactoryOptions.cs

-44
This file was deleted.

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/Options/TypeConvertRelationOptions.cs

-13
This file was deleted.

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/_Imports.cs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
global using Masa.BuildingBlocks.Data;
5-
global using Masa.BuildingBlocks.Data.TypeConverts;
6-
global using Masa.Contrib.Data;
75
global using Microsoft.Extensions.DependencyInjection;
86
global using Microsoft.Extensions.DependencyInjection.Extensions;
97
global using Microsoft.Extensions.Options;

src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/BaseUserContext.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class BaseUserContext : IUserContext
1212
public string? UserId => _userContext.UserId;
1313
public string? UserName => _userContext.UserName;
1414

15-
public BaseUserContext(IServiceProvider serviceProvider)
15+
public BaseUserContext(IUserContext userContext, ITypeConvertProvider typeConvertProvider)
1616
{
17-
_userContext = serviceProvider.GetRequiredService<IUserContext>();
18-
TypeConvertProvider = serviceProvider.GetRequiredService<ITypeConvertProvider>();
17+
_userContext = userContext;
18+
TypeConvertProvider = typeConvertProvider;
1919
}
2020

2121
public TUserId? GetUserId<TUserId>() => _userContext.GetUserId<TUserId>();

src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/DefaultIsolatedUserContext.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class DefaultIsolatedUserContext : BaseUserContext, IIsolatedUserContext
99

1010
public string? Environment => GetUser<IsolatedIdentityUser>()?.Environment;
1111

12-
public DefaultIsolatedUserContext(IServiceProvider serviceProvider) : base(serviceProvider)
12+
public DefaultIsolatedUserContext(IUserContext userContext, ITypeConvertProvider typeConvertProvider)
13+
: base(userContext, typeConvertProvider)
1314
{
1415
}
1516

0 commit comments

Comments
 (0)