diff --git a/src/BuildingBlocks/Configuration/Masa.BuildingBlocks.Configuration/Options/ConfigurationRelationOptions.cs b/src/BuildingBlocks/Configuration/Masa.BuildingBlocks.Configuration/Options/ConfigurationRelationOptions.cs index d43243275..47294e499 100644 --- a/src/BuildingBlocks/Configuration/Masa.BuildingBlocks.Configuration/Options/ConfigurationRelationOptions.cs +++ b/src/BuildingBlocks/Configuration/Masa.BuildingBlocks.Configuration/Options/ConfigurationRelationOptions.cs @@ -15,4 +15,6 @@ public class ConfigurationRelationOptions /// Object type of mapping node relationship /// public Type ObjectType { get; set; } = default!; + + public string Name { get; set; } } diff --git a/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/MasaConfigurationBuilderExtensions.cs b/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/MasaConfigurationBuilderExtensions.cs index 0cd3cf9d8..6143a6d5b 100644 --- a/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/MasaConfigurationBuilderExtensions.cs +++ b/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/MasaConfigurationBuilderExtensions.cs @@ -30,7 +30,8 @@ internal static void AutoMapping(this MasaConfigurationBuilder builder, params A var option = (IMasaConfigurationOptions)Activator.CreateInstance(optionType, !constructorInfo.IsPublic)!; var sectionName = option.Section ?? optionType.Name; - if (builder.Relations.Any(relation => relation.SectionType == option.SectionType && relation.Section == sectionName && relation.ObjectType == optionType)) + var name = Options.DefaultName; + if (builder.Relations.Any(relation => relation.SectionType == option.SectionType && relation.Section == sectionName && relation.ObjectType == optionType && relation.Name == name)) { throw new ArgumentException( "The section has been loaded, no need to load repeatedly, check whether there are duplicate sections or inheritance between auto-mapping classes"); @@ -40,7 +41,8 @@ internal static void AutoMapping(this MasaConfigurationBuilder builder, params A SectionType = option.SectionType, ParentSection = option.ParentSection, Section = sectionName, - ObjectType = optionType + ObjectType = optionType, + Name = name }); }); } diff --git a/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/ServiceCollectionExtensions.cs b/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/ServiceCollectionExtensions.cs index 0d8e7aca2..fde80f2ac 100644 --- a/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/ServiceCollectionExtensions.cs +++ b/src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/ServiceCollectionExtensions.cs @@ -73,7 +73,7 @@ public static IConfigurationRoot CreateMasaConfiguration( sectionNames.AddRange(relation.Section!.Split(ConfigurationPath.KeyDelimiter)); } - services.ConfigureOption(configuration, sectionNames, relation.ObjectType); + services.ConfigureOption(configuration, sectionNames, relation.ObjectType, relation.Name); }); return configuration; @@ -83,7 +83,8 @@ private static void ConfigureOption( this IServiceCollection services, IConfiguration configuration, List sectionNames, - Type optionType) + Type optionType, + string name) { IConfigurationSection? configurationSection = null; foreach (var sectionName in sectionNames) @@ -97,9 +98,9 @@ private static void ConfigureOption( throw new MasaException($"Check if the mapping section is correct,section name is [{configurationSection!.Path}]"); var configurationChangeTokenSource = - Activator.CreateInstance(typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionType), string.Empty, + Activator.CreateInstance(typeof(ConfigurationChangeTokenSource<>).MakeGenericType(optionType), name, configurationSection)!; - services.TryAdd(new ServiceDescriptor(typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionType), + services.Add(new ServiceDescriptor(typeof(IOptionsChangeTokenSource<>).MakeGenericType(optionType), configurationChangeTokenSource)); Action configureBinder = _ => @@ -107,9 +108,9 @@ private static void ConfigureOption( }; var configureOptions = Activator.CreateInstance(typeof(NamedConfigureFromConfigurationOptions<>).MakeGenericType(optionType), - string.Empty, + name, configurationSection, configureBinder)!; - services.TryAdd(new ServiceDescriptor(typeof(IConfigureOptions<>).MakeGenericType(optionType), + services.Add(new ServiceDescriptor(typeof(IConfigureOptions<>).MakeGenericType(optionType), configureOptions)); } diff --git a/src/Contrib/Configuration/Masa.Contrib.Configuration/Options/MasaRelationOptions.cs b/src/Contrib/Configuration/Masa.Contrib.Configuration/Options/MasaRelationOptions.cs index 8b7c9aaec..d5471cbbb 100644 --- a/src/Contrib/Configuration/Masa.Contrib.Configuration/Options/MasaRelationOptions.cs +++ b/src/Contrib/Configuration/Masa.Contrib.Configuration/Options/MasaRelationOptions.cs @@ -12,9 +12,10 @@ public class MasaRelationOptions /// /// /// The default is null, which is consistent with the mapping class name, and string.Empty when no root node exists + /// /// - public MasaRelationOptions MappingLocal(string? section = null) where TModel : class - => Mapping(SectionTypes.Local, null!, section); + public MasaRelationOptions MappingLocal(string? section = null, string? name = null) where TModel : class + => Mapping(SectionTypes.Local, null!, section, name); /// /// Map Section relationship By ConfigurationApi @@ -22,9 +23,11 @@ public MasaRelationOptions MappingLocal(string? section = null) where TM /// /// The name of the parent section, if it is empty, it will be mounted under SectionType, otherwise it will be mounted to the specified section under SectionType /// The default is null, which is consistent with the mapping class name, and string.Empty when no root node exists + /// /// - public MasaRelationOptions MappingConfigurationApi(string parentSection, string? section = null) where TModel : class - => Mapping(SectionTypes.ConfigurationApi, parentSection, section); + public MasaRelationOptions MappingConfigurationApi(string parentSection, string? section = null, string? name = null) + where TModel : class + => Mapping(SectionTypes.ConfigurationApi, parentSection, section, name); /// /// Map Section relationship @@ -33,13 +36,16 @@ public MasaRelationOptions MappingConfigurationApi(string parentSection, /// /// parent section, local section is the name of the locally configured section, and ConfigurationApi is the name of the Appid where the configuration is located /// The default is null, which is consistent with the mapping class name + /// /// /// - public MasaRelationOptions Mapping(SectionTypes sectionType, string parentSection, string? section = null) where TModel : class + public MasaRelationOptions Mapping(SectionTypes sectionType, string parentSection, string? section = null, string? name = null) + where TModel : class { + name ??= Options.DefaultName; section ??= typeof(TModel).Name; - if (Relations.Any(relation => relation.SectionType == sectionType && relation.Section == section)) + if (Relations.Any(relation => relation.SectionType == sectionType && relation.Section == section && relation.Name == name)) throw new ArgumentOutOfRangeException(nameof(section), "The current section already has a configuration"); Relations.Add(new ConfigurationRelationOptions() @@ -47,7 +53,8 @@ public MasaRelationOptions Mapping(SectionTypes sectionType, string pare SectionType = sectionType, ParentSection = parentSection, Section = section, - ObjectType = typeof(TModel) + ObjectType = typeof(TModel), + Name = name }); return this; } diff --git a/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/ConfigurationTest.cs b/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/ConfigurationTest.cs index 16dd7147c..98a6d5e7e 100644 --- a/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/ConfigurationTest.cs +++ b/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/ConfigurationTest.cs @@ -308,4 +308,31 @@ public void TestMasaConfigurationByKey(string key, string value) var localConfiguration = builder.GetMasaConfiguration().Local; Assert.IsTrue(localConfiguration[key] == value); } + + [TestMethod] + public void TestMasaConfigurationByName() + { + var builder = WebApplication.CreateBuilder(); + builder.AddMasaConfiguration(configurationBuilder => + { + configurationBuilder.AddJsonFile("customAppConfig.json", true, true) + .AddJsonFile("rabbitMq.json", true, true); + configurationBuilder.UseMasaOptions(option => + { + option.MappingLocal(); + option.MappingLocal("RedisOptions2", "RedisOptions2"); + }); + }, typeof(ConfigurationTest).Assembly); + var serviceProvider = builder.Services.BuildServiceProvider(); + var options = serviceProvider.GetService>(); + Assert.IsNotNull(options); + Assert.AreEqual("localhost", options.Value.Ip); + Assert.AreEqual("", options.Value.Password); + Assert.AreEqual(6379, options.Value.Port); + + var options2 = options.Get("RedisOptions2"); + Assert.AreEqual("127.0.0.1", options2.Ip); + Assert.AreEqual("123456", options2.Password); + Assert.AreEqual(6378, options2.Port); + } } diff --git a/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/customAppConfig.json b/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/customAppConfig.json index 5fb6c8c30..ad56a3f16 100644 --- a/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/customAppConfig.json +++ b/src/Contrib/Configuration/Tests/Masa.Contrib.Configuration.Tests/customAppConfig.json @@ -3,5 +3,10 @@ "Ip": "localhost", "Password": "", "Port": 6379 + }, + "RedisOptions2": { + "Ip": "127.0.0.1", + "Password": "123456", + "Port": 6378 } } \ No newline at end of file