From ba73d7687ceca80848e011d37d72de79b4a8ae6f Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:43:41 +0800 Subject: [PATCH] Instead of asynchronous IWebhookDefinitionManager interface --- .../IDynamicWebhookDefinitionStore.cs | 13 +++ .../Webhooks/IStaticWebhookDefinitionStore.cs | 13 +++ .../Abp/Webhooks/IWebhookDefinitionManager.cs | 12 +- .../NullDynamicWebhookDefinitionStore.cs | 34 ++++++ .../Webhooks/StaticWebhookDefinitionStore.cs | 101 +++++++++++++++++ .../Abp/Webhooks/WebhookDefinitionManager.cs | 106 +++++++----------- ...sManagementPermissionDefinitionProvider.cs | 3 +- .../WebhookSubscriptionAppService.cs | 2 +- ...ksManagementHttpApiHostModule.Configure.cs | 3 - 9 files changed, 208 insertions(+), 79 deletions(-) create mode 100644 aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs create mode 100644 aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs create mode 100644 aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs create mode 100644 aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs new file mode 100644 index 000000000..b50313e51 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IDynamicWebhookDefinitionStore.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.Webhooks; + +public interface IDynamicWebhookDefinitionStore +{ + Task GetOrNullAsync(string name); + + Task> GetWebhooksAsync(); + + Task> GetGroupsAsync(); +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs new file mode 100644 index 000000000..21f7b4ed3 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IStaticWebhookDefinitionStore.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.Webhooks; + +public interface IStaticWebhookDefinitionStore +{ + Task GetOrNullAsync(string name); + + Task> GetWebhooksAsync(); + + Task> GetGroupsAsync(); +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs index a284ab693..6732743c5 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/IWebhookDefinitionManager.cs @@ -1,4 +1,5 @@ -using System; +using JetBrains.Annotations; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -10,24 +11,25 @@ namespace LINGYUN.Abp.Webhooks /// Gets a webhook definition by name. /// Returns null if there is no webhook definition with given name. /// - WebhookDefinition GetOrNull(string name); + [NotNull] + Task GetOrNullAsync(string name); /// /// Gets a webhook definition by name. /// Throws exception if there is no webhook definition with given name. /// - WebhookDefinition Get(string name); + Task GetAsync(string name); /// /// Gets all webhook definitions. /// - IReadOnlyList GetAll(); + Task> GetWebhooksAsync(); /// /// Gets all webhook group definitions. /// /// - IReadOnlyList GetGroups(); + Task> GetGroupsAsync(); /// /// Checks if given webhook name is available for given tenant. diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs new file mode 100644 index 000000000..b9c95e991 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/NullDynamicWebhookDefinitionStore.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace LINGYUN.Abp.Webhooks; + +[Dependency(TryRegister = true)] +public class NullDynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ISingletonDependency +{ + private readonly static Task CachedWebhookResult = Task.FromResult((WebhookDefinition)null); + + private readonly static Task> CachedWebhooksResult = + Task.FromResult((IReadOnlyList)Array.Empty().ToImmutableList()); + + private readonly static Task> CachedGroupsResult = + Task.FromResult((IReadOnlyList)Array.Empty().ToImmutableList()); + + public Task GetOrNullAsync(string name) + { + return CachedWebhookResult; + } + + public Task> GetWebhooksAsync() + { + return CachedWebhooksResult; + } + + public Task> GetGroupsAsync() + { + return CachedGroupsResult; + } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs new file mode 100644 index 000000000..6ed2ba69a --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/StaticWebhookDefinitionStore.cs @@ -0,0 +1,101 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.DependencyInjection; + +namespace LINGYUN.Abp.Webhooks; + +public class StaticWebhookDefinitionStore : IStaticWebhookDefinitionStore, ISingletonDependency +{ + protected IDictionary WebhookGroupDefinitions => _lazyWebhookGroupDefinitions.Value; + private readonly Lazy> _lazyWebhookGroupDefinitions; + + protected IDictionary WebhookDefinitions => _lazyWebhookDefinitions.Value; + private readonly Lazy> _lazyWebhookDefinitions; + + protected AbpWebhooksOptions Options { get; } + + private readonly IServiceScopeFactory _serviceScopeFactory; + + public StaticWebhookDefinitionStore( + IServiceScopeFactory serviceScopeFactory, + IOptions options) + { + _serviceScopeFactory = serviceScopeFactory; + Options = options.Value; + + _lazyWebhookDefinitions = new Lazy>( + CreateWebhookDefinitions, + isThreadSafe: true + ); + + _lazyWebhookGroupDefinitions = new Lazy>( + CreateWebhookGroupDefinitions, + isThreadSafe: true + ); + } + + protected virtual Dictionary CreateWebhookDefinitions() + { + var Webhooks = new Dictionary(); + + foreach (var groupDefinition in WebhookGroupDefinitions.Values) + { + foreach (var Webhook in groupDefinition.Webhooks) + { + if (Webhooks.ContainsKey(Webhook.Name)) + { + throw new AbpException("Duplicate webhook name: " + Webhook.Name); + } + + Webhooks[Webhook.Name] = Webhook; + } + } + + return Webhooks; + } + + protected virtual Dictionary CreateWebhookGroupDefinitions() + { + var definitions = new Dictionary(); + + using (var scope = _serviceScopeFactory.CreateScope()) + { + var providers = Options + .DefinitionProviders + .Select(p => scope.ServiceProvider.GetRequiredService(p) as WebhookDefinitionProvider) + .ToList(); + + foreach (var provider in providers) + { + provider.Define(new WebhookDefinitionContext(definitions)); + } + } + + return definitions; + } + + public Task GetOrNullAsync(string name) + { + return Task.FromResult(WebhookDefinitions.GetOrDefault(name)); + } + + public virtual Task> GetWebhooksAsync() + { + return Task.FromResult>( + WebhookDefinitions.Values.ToImmutableList() + ); + } + + public Task> GetGroupsAsync() + { + return Task.FromResult>( + WebhookGroupDefinitions.Values.ToImmutableList() + ); + } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs index c0f96f20e..7db2082bd 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/WebhookDefinitionManager.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -14,54 +13,65 @@ namespace LINGYUN.Abp.Webhooks { internal class WebhookDefinitionManager : IWebhookDefinitionManager, ISingletonDependency { - protected IDictionary WebhookGroupDefinitions => _lazyWebhookGroupDefinitions.Value; - private readonly Lazy> _lazyWebhookGroupDefinitions; - - protected IDictionary WebhookDefinitions => _lazyWebhookDefinitions.Value; - private readonly Lazy> _lazyWebhookDefinitions; - private readonly IServiceProvider _serviceProvider; - private readonly AbpWebhooksOptions _options; + private readonly IStaticWebhookDefinitionStore _staticStore; + private readonly IDynamicWebhookDefinitionStore _dynamicStore; public WebhookDefinitionManager( IServiceProvider serviceProvider, - IOptions options) + IStaticWebhookDefinitionStore staticStore, + IDynamicWebhookDefinitionStore dynamicStore) { _serviceProvider = serviceProvider; - _options = options.Value; - - _lazyWebhookGroupDefinitions = new Lazy>(CreateWebhookGroupDefinitions); - _lazyWebhookDefinitions = new Lazy>(CreateWebhookDefinitions); + _staticStore = staticStore; + _dynamicStore = dynamicStore; } - public WebhookDefinition GetOrNull(string name) + public async virtual Task GetOrNullAsync(string name) { - if (!WebhookDefinitions.ContainsKey(name)) - { - return null; - } + Check.NotNull(name, nameof(name)); - return WebhookDefinitions[name]; + return await _staticStore.GetOrNullAsync(name) ?? + await _dynamicStore.GetOrNullAsync(name); } - public WebhookDefinition Get(string name) + public async virtual Task GetAsync(string name) { - if (!WebhookDefinitions.ContainsKey(name)) + var webhook = await GetOrNullAsync(name); + if (webhook == null) { - throw new KeyNotFoundException($"Webhook definitions does not contain a definition with the key \"{name}\"."); + throw new AbpException("Undefined webhook: " + name); } - return WebhookDefinitions[name]; + return webhook; } - public IReadOnlyList GetAll() + public async virtual Task> GetWebhooksAsync() { - return WebhookDefinitions.Values.ToImmutableList(); + var staticWebhooks = await _staticStore.GetWebhooksAsync(); + var staticWebhookNames = staticWebhooks + .Select(p => p.Name) + .ToImmutableHashSet(); + + var dynamicWebhooks = await _dynamicStore.GetWebhooksAsync(); + + return staticWebhooks + .Concat(dynamicWebhooks.Where(d => !staticWebhookNames.Contains(d.Name))) + .ToImmutableList(); } - public IReadOnlyList GetGroups() + public async virtual Task> GetGroupsAsync() { - return WebhookGroupDefinitions.Values.ToImmutableList(); + var staticGroups = await _staticStore.GetGroupsAsync(); + var staticGroupNames = staticGroups + .Select(p => p.Name) + .ToImmutableHashSet(); + + var dynamicGroups = await _dynamicStore.GetGroupsAsync(); + + return staticGroups + .Concat(dynamicGroups.Where(d => !staticGroupNames.Contains(d.Name))) + .ToImmutableList(); } public async Task IsAvailableAsync(Guid? tenantId, string name) @@ -71,7 +81,7 @@ namespace LINGYUN.Abp.Webhooks return true; } - var webhookDefinition = GetOrNull(name); + var webhookDefinition = await GetOrNullAsync(name); if (webhookDefinition == null) { @@ -95,45 +105,5 @@ namespace LINGYUN.Abp.Webhooks return true; } - - protected virtual Dictionary CreateWebhookDefinitions() - { - var definitions = new Dictionary(); - - foreach (var groupDefinition in WebhookGroupDefinitions.Values) - { - foreach (var webhook in groupDefinition.Webhooks) - { - if (definitions.ContainsKey(webhook.Name)) - { - throw new AbpException("Duplicate webhook name: " + webhook.Name); - } - - definitions[webhook.Name] = webhook; - } - } - - return definitions; - } - - protected virtual Dictionary CreateWebhookGroupDefinitions() - { - var definitions = new Dictionary(); - - using (var scope = _serviceProvider.CreateScope()) - { - var providers = _options - .DefinitionProviders - .Select(p => scope.ServiceProvider.GetRequiredService(p) as WebhookDefinitionProvider) - .ToList(); - - foreach (var provider in providers) - { - provider.Define(new WebhookDefinitionContext(definitions)); - } - } - - return definitions; - } } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs index ab14529b1..c2b835506 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs @@ -11,8 +11,7 @@ public class WebhooksManagementPermissionDefinitionProvider : PermissionDefiniti { var group = context.AddGroup( WebhooksManagementPermissions.GroupName, - L("Permission:WebhooksManagement"), - MultiTenancySides.Host); + L("Permission:WebhooksManagement")); var subscription = group.AddPermission( WebhooksManagementPermissions.WebhookSubscription.Default, diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs index 71bb55b99..2e151e871 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs @@ -109,7 +109,7 @@ public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, I public async virtual Task> GetAllAvailableWebhooksAsync() { - var groups = WebhookDefinitionManager.GetGroups(); + var groups = await WebhookDefinitionManager.GetGroupsAsync(); var definitions = new List(); foreach (var groupDefinition in groups) diff --git a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs index 6c12fe19b..148702021 100644 --- a/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs +++ b/aspnet-core/services/LY.MicroService.WebhooksManagement.HttpApi.Host/WebhooksManagementHttpApiHostModule.Configure.cs @@ -1,6 +1,5 @@ using DotNetCore.CAP; using LINGYUN.Abp.BackgroundTasks; -using LINGYUN.Abp.Dapr.Client.DynamicProxying; using LINGYUN.Abp.ExceptionHandling; using LINGYUN.Abp.ExceptionHandling.Emailing; using LINGYUN.Abp.Serilog.Enrichers.Application; @@ -302,8 +301,6 @@ public partial class WebhooksManagementHttpApiHostModule { options.Languages.Add(new LanguageInfo("en", "en", "English")); options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); - // 动态语言支持 - options.Resources.AddDynamic(); }); }