From c457e1f3590147c20d3da4b25529c3eff6672fe8 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 24 Jun 2022 09:51:43 +0800 Subject: [PATCH 1/2] Introdce the `IApplicationConfigurationContributor`. Resolve #13099 --- .../AbpApplicationConfigurationOptions.cs | 13 +++++++++++++ ...pplicationConfigurationContributorContext.cs | 17 +++++++++++++++++ .../ApplicationConfigurationDto.cs | 3 ++- .../IApplicationConfigurationContributor.cs | 8 ++++++++ .../AbpApplicationConfigurationAppService.cs | 17 ++++++++++++++++- .../Mvc/AbpAspNetCoreMvcTestModule.cs | 6 ++++++ .../ApplicationConfigurationBuilder_Tests.cs | 2 ++ .../TestApplicationConfigurationContributor.cs | 13 +++++++++++++ 8 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationOptions.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationContributorContext.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/IApplicationConfigurationContributor.cs create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/TestApplicationConfigurationContributor.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationOptions.cs new file mode 100644 index 0000000000..e4e59bc197 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationOptions.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; + +public class AbpApplicationConfigurationOptions +{ + public List Contributors { get; } + + public AbpApplicationConfigurationOptions() + { + Contributors = new List(); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationContributorContext.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationContributorContext.cs new file mode 100644 index 0000000000..3831cc1618 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationContributorContext.cs @@ -0,0 +1,17 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; + +public class ApplicationConfigurationContributorContext : IServiceProviderAccessor +{ + public IServiceProvider ServiceProvider { get; } + + public ApplicationConfigurationDto ApplicationConfiguration { get; } + + public ApplicationConfigurationContributorContext(IServiceProvider serviceProvider, ApplicationConfigurationDto applicationConfiguration) + { + ServiceProvider = serviceProvider; + ApplicationConfiguration = applicationConfiguration; + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs index eb29c5c7cb..4a7a21959f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs @@ -1,11 +1,12 @@ using System; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending; using Volo.Abp.AspNetCore.Mvc.MultiTenancy; +using Volo.Abp.ObjectExtending; namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; [Serializable] -public class ApplicationConfigurationDto +public class ApplicationConfigurationDto : ExtensibleObject { public ApplicationLocalizationConfigurationDto Localization { get; set; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/IApplicationConfigurationContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/IApplicationConfigurationContributor.cs new file mode 100644 index 0000000000..7612ae6fec --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/IApplicationConfigurationContributor.cs @@ -0,0 +1,8 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; + +public interface IApplicationConfigurationContributor +{ + Task ContributeAsync(ApplicationConfigurationContributorContext context); +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs index b80a288e81..abf3fe603c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs @@ -41,6 +41,7 @@ public class AbpApplicationConfigurationAppService : ApplicationService, IAbpApp private readonly ITimezoneProvider _timezoneProvider; private readonly AbpClockOptions _abpClockOptions; private readonly ICachedObjectExtensionsDtoService _cachedObjectExtensionsDtoService; + private readonly AbpApplicationConfigurationOptions _options; public AbpApplicationConfigurationAppService( IOptions localizationOptions, @@ -58,7 +59,8 @@ public class AbpApplicationConfigurationAppService : ApplicationService, IAbpApp ILanguageProvider languageProvider, ITimezoneProvider timezoneProvider, IOptions abpClockOptions, - ICachedObjectExtensionsDtoService cachedObjectExtensionsDtoService) + ICachedObjectExtensionsDtoService cachedObjectExtensionsDtoService, + IOptions options) { _serviceProvider = serviceProvider; _abpAuthorizationPolicyProvider = abpAuthorizationPolicyProvider; @@ -74,6 +76,7 @@ public class AbpApplicationConfigurationAppService : ApplicationService, IAbpApp _timezoneProvider = timezoneProvider; _abpClockOptions = abpClockOptions.Value; _cachedObjectExtensionsDtoService = cachedObjectExtensionsDtoService; + _options = options.Value; _localizationOptions = localizationOptions.Value; _multiTenancyOptions = multiTenancyOptions.Value; } @@ -99,6 +102,18 @@ public class AbpApplicationConfigurationAppService : ApplicationService, IAbpApp ObjectExtensions = _cachedObjectExtensionsDtoService.Get() }; + if (_options.Contributors.Any()) + { + using (var scope = _serviceProvider.CreateScope()) + { + var context = new ApplicationConfigurationContributorContext(scope.ServiceProvider, result); + foreach (var contributor in _options.Contributors) + { + await contributor.ContributeAsync(context); + } + } + } + Logger.LogDebug("Executed AbpApplicationConfigurationAppService.GetAsync()."); return result; diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index 2e8d8c4689..e303a0fb5d 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.Authorization; using Volo.Abp.AspNetCore.Mvc.GlobalFeatures; using Volo.Abp.AspNetCore.Mvc.Localization; @@ -127,6 +128,11 @@ public class AbpAspNetCoreMvcTestModule : AbpModule options.Maps.Add("SerialNumber", () => ClaimTypes.SerialNumber); options.Maps.Add("DateOfBirth", () => ClaimTypes.DateOfBirth); }); + + Configure(options => + { + options.Contributors.Add(new TestApplicationConfigurationContributor()); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationBuilder_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationBuilder_Tests.cs index c932e09488..5d3cb09749 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationBuilder_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationBuilder_Tests.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Shouldly; +using Volo.Abp.Data; using Xunit; namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; @@ -15,5 +16,6 @@ public class ApplicationConfigurationBuilder_Tests : AspNetCoreMvcTestBase config.Auth.ShouldNotBeNull(); config.Localization.ShouldNotBeNull(); + config.GetProperty("TestKey").ShouldBe("TestValue"); } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/TestApplicationConfigurationContributor.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/TestApplicationConfigurationContributor.cs new file mode 100644 index 0000000000..4ae197d2a0 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/TestApplicationConfigurationContributor.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Volo.Abp.Data; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; + +public class TestApplicationConfigurationContributor : IApplicationConfigurationContributor +{ + public Task ContributeAsync(ApplicationConfigurationContributorContext context) + { + context.ApplicationConfiguration.SetProperty("TestKey", "TestValue"); + return Task.CompletedTask; + } +} From 9a9e8d36df7849c30d398d55ee2c75db1722da13 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 24 Jun 2022 09:56:06 +0800 Subject: [PATCH 2/2] Use `IHasExtraProperties` to replace `ExtensibleObject`. --- .../ApplicationConfigurationDto.cs | 6 ++++-- .../AbpApplicationConfigurationAppService.cs | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs index 4a7a21959f..61b5ec18db 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationDto.cs @@ -1,12 +1,12 @@ using System; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending; using Volo.Abp.AspNetCore.Mvc.MultiTenancy; -using Volo.Abp.ObjectExtending; +using Volo.Abp.Data; namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; [Serializable] -public class ApplicationConfigurationDto : ExtensibleObject +public class ApplicationConfigurationDto : IHasExtraProperties { public ApplicationLocalizationConfigurationDto Localization { get; set; } @@ -29,4 +29,6 @@ public class ApplicationConfigurationDto : ExtensibleObject public ClockDto Clock { get; set; } public ObjectExtensionsDto ObjectExtensions { get; set; } + + public ExtraPropertyDictionary ExtraProperties { get; set; } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs index abf3fe603c..528a04583d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs @@ -13,6 +13,7 @@ using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending; using Volo.Abp.AspNetCore.Mvc.MultiTenancy; using Volo.Abp.Authorization; using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Data; using Volo.Abp.Features; using Volo.Abp.GlobalFeatures; using Volo.Abp.Localization; @@ -99,7 +100,8 @@ public class AbpApplicationConfigurationAppService : ApplicationService, IAbpApp CurrentTenant = GetCurrentTenant(), Timing = await GetTimingConfigAsync(), Clock = GetClockConfig(), - ObjectExtensions = _cachedObjectExtensionsDtoService.Get() + ObjectExtensions = _cachedObjectExtensionsDtoService.Get(), + ExtraProperties = new ExtraPropertyDictionary() }; if (_options.Contributors.Any())