From 790aa25165cb8b8cbe01a8af0e2a33efd2d36e02 Mon Sep 17 00:00:00 2001 From: damon Date: Tue, 12 Dec 2023 21:18:54 +0800 Subject: [PATCH] Automatic set validator factory. --- .../AutomapperModule.cs | 24 ++++++++++--------- .../Euonia.Mapping.Mapster/MapsterModule.cs | 24 ++++++++++--------- Source/Euonia.Validation/ValidationModule.cs | 10 ++++++++ project.props | 2 +- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/Source/Euonia.Mapping.Automapper/AutomapperModule.cs b/Source/Euonia.Mapping.Automapper/AutomapperModule.cs index b635f69..e1de911 100644 --- a/Source/Euonia.Mapping.Automapper/AutomapperModule.cs +++ b/Source/Euonia.Mapping.Automapper/AutomapperModule.cs @@ -8,20 +8,22 @@ namespace Nerosoft.Euonia.Mapping; /// public class AutomapperModule : ModuleContextBase { + private const string SERVICE_INJECTION_KEY = "automapper"; + /// public override void ConfigureServices(ServiceConfigurationContext context) - { - context.Services.AddAutomapper(); - context.Services.AddSingleton(); - } + { + context.Services.AddAutomapper(); + context.Services.AddKeyedSingleton(SERVICE_INJECTION_KEY); + } /// public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - var factory = context.ServiceProvider.GetService(); - if (factory != null) - { - TypeAdapterFactory.SetCurrent(factory); - } - } + { + var factory = context.ServiceProvider.GetKeyedService(SERVICE_INJECTION_KEY); + if (factory != null) + { + TypeAdapterFactory.SetCurrent(factory); + } + } } \ No newline at end of file diff --git a/Source/Euonia.Mapping.Mapster/MapsterModule.cs b/Source/Euonia.Mapping.Mapster/MapsterModule.cs index 497ee1c..4f9061a 100644 --- a/Source/Euonia.Mapping.Mapster/MapsterModule.cs +++ b/Source/Euonia.Mapping.Mapster/MapsterModule.cs @@ -8,20 +8,22 @@ namespace Nerosoft.Euonia.Mapping; /// public class MapsterModule : ModuleContextBase { + private const string SERVICE_INJECTION_KEY = "mapster"; + /// public override void ConfigureServices(ServiceConfigurationContext context) - { - context.Services.AddMapster(); - context.Services.AddSingleton(); - } + { + context.Services.AddMapster(); + context.Services.AddKeyedSingleton(SERVICE_INJECTION_KEY); + } /// public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - var factory = context.ServiceProvider.GetService(); - if (factory != null) - { - TypeAdapterFactory.SetCurrent(factory); - } - } + { + var factory = context.ServiceProvider.GetKeyedService(SERVICE_INJECTION_KEY); + if (factory != null) + { + TypeAdapterFactory.SetCurrent(factory); + } + } } \ No newline at end of file diff --git a/Source/Euonia.Validation/ValidationModule.cs b/Source/Euonia.Validation/ValidationModule.cs index f159e5c..5a228fd 100644 --- a/Source/Euonia.Validation/ValidationModule.cs +++ b/Source/Euonia.Validation/ValidationModule.cs @@ -16,4 +16,14 @@ public override void ConfigureServices(ServiceConfigurationContext context) context.Services.AddAutomaticRegistrar(new ValidationRegistrar()); context.Services.AddSingleton(); } + + /// + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var factory = context.ServiceProvider.GetService(); + if (factory != null) + { + ValidatorFactory.SetCurrent(factory); + } + } } \ No newline at end of file diff --git a/project.props b/project.props index d16b777..1a24835 100644 --- a/project.props +++ b/project.props @@ -1,6 +1,6 @@ - 8.1.8 + 8.1.9 damon Nerosoft Ltd. Euonia