From cd4a7200f952999430fd17d47b69dc246df67311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 24 Nov 2020 20:53:25 +0300 Subject: [PATCH] Implement synchronization for object extensions. --- .../ModuleExtensionConfigurationHelper.cs | 179 ++++++++++-------- .../ObjectExtending/ObjectExtensionInfo.cs | 13 +- .../ObjectExtending/ObjectExtensionManager.cs | 12 +- .../ObjectExtensionPropertyInfo.cs | 1 - 4 files changed, 112 insertions(+), 93 deletions(-) diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs index 4f7d1396af..93beb2b073 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs @@ -1,23 +1,27 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using JetBrains.Annotations; namespace Volo.Abp.ObjectExtending.Modularity { public static class ModuleExtensionConfigurationHelper { + private static object SyncLock = new object(); + public static void ApplyEntityConfigurationToEntity( string moduleName, string entityName, Type entityType) { - foreach (var propertyConfig in GetPropertyConfigurations(moduleName, entityName)) + lock (SyncLock) { - if (propertyConfig.Entity.IsAvailable && - entityType != null) + foreach (var propertyConfig in GetPropertyConfigurations(moduleName, entityName)) { - ApplyPropertyConfigurationToTypes(propertyConfig, new[] { entityType }); + if (propertyConfig.Entity.IsAvailable && + entityType != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, new[] { entityType }); + } } } } @@ -29,29 +33,32 @@ namespace Volo.Abp.ObjectExtending.Modularity Type[] createApiTypes = null, Type[] updateApiTypes = null) { - foreach (var propertyConfig in GetPropertyConfigurations(moduleName, objectName)) + lock (SyncLock) { - if (!propertyConfig.IsAvailableToClients) + foreach (var propertyConfig in GetPropertyConfigurations(moduleName, objectName)) { - continue; - } + if (!propertyConfig.IsAvailableToClients) + { + continue; + } - if (propertyConfig.Api.OnGet.IsAvailable && - getApiTypes != null) - { - ApplyPropertyConfigurationToTypes(propertyConfig, getApiTypes); - } + if (propertyConfig.Api.OnGet.IsAvailable && + getApiTypes != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, getApiTypes); + } - if (propertyConfig.Api.OnCreate.IsAvailable && - createApiTypes != null) - { - ApplyPropertyConfigurationToTypes(propertyConfig, createApiTypes); - } + if (propertyConfig.Api.OnCreate.IsAvailable && + createApiTypes != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, createApiTypes); + } - if (propertyConfig.Api.OnUpdate.IsAvailable && - updateApiTypes != null) - { - ApplyPropertyConfigurationToTypes(propertyConfig, updateApiTypes); + if (propertyConfig.Api.OnUpdate.IsAvailable && + updateApiTypes != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, updateApiTypes); + } } } } @@ -62,23 +69,26 @@ namespace Volo.Abp.ObjectExtending.Modularity Type[] createFormTypes = null, Type[] editFormTypes = null) { - foreach (var propertyConfig in GetPropertyConfigurations(moduleName, entityName)) + lock (SyncLock) { - if (!propertyConfig.IsAvailableToClients) + foreach (var propertyConfig in GetPropertyConfigurations(moduleName, entityName)) { - continue; - } + if (!propertyConfig.IsAvailableToClients) + { + continue; + } - if (propertyConfig.UI.OnCreateForm.IsVisible && - createFormTypes != null) - { - ApplyPropertyConfigurationToTypes(propertyConfig, createFormTypes); - } + if (propertyConfig.UI.OnCreateForm.IsVisible && + createFormTypes != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, createFormTypes); + } - if (propertyConfig.UI.OnEditForm.IsVisible && - editFormTypes != null) - { - ApplyPropertyConfigurationToTypes(propertyConfig, editFormTypes); + if (propertyConfig.UI.OnEditForm.IsVisible && + editFormTypes != null) + { + ApplyPropertyConfigurationToTypes(propertyConfig, editFormTypes); + } } } } @@ -93,29 +103,32 @@ namespace Volo.Abp.ObjectExtending.Modularity Type[] createApiTypes = null, Type[] updateApiTypes = null) { - if (entityType != null) + lock (SyncLock) { - ApplyEntityConfigurationToEntity( + if (entityType != null) + { + ApplyEntityConfigurationToEntity( + moduleName, + entityName, + entityType + ); + } + + ApplyEntityConfigurationToApi( moduleName, entityName, - entityType + getApiTypes: getApiTypes, + createApiTypes: createApiTypes, + updateApiTypes: updateApiTypes ); - } - ApplyEntityConfigurationToApi( - moduleName, - entityName, - getApiTypes: getApiTypes, - createApiTypes: createApiTypes, - updateApiTypes: updateApiTypes - ); - - ApplyEntityConfigurationToUi( - moduleName, - entityName, - createFormTypes: createFormTypes, - editFormTypes: editFormTypes - ); + ApplyEntityConfigurationToUi( + moduleName, + entityName, + createFormTypes: createFormTypes, + editFormTypes: editFormTypes + ); + } } [NotNull] @@ -123,41 +136,47 @@ namespace Volo.Abp.ObjectExtending.Modularity string moduleName, string entityName) { - var moduleConfig = ObjectExtensionManager.Instance.Modules().GetOrDefault(moduleName); - if (moduleConfig == null) + lock (SyncLock) { - return Array.Empty(); - } + var moduleConfig = ObjectExtensionManager.Instance.Modules().GetOrDefault(moduleName); + if (moduleConfig == null) + { + return Array.Empty(); + } - var objectConfig = moduleConfig.Entities.GetOrDefault(entityName); - if (objectConfig == null) - { - return Array.Empty(); - } + var objectConfig = moduleConfig.Entities.GetOrDefault(entityName); + if (objectConfig == null) + { + return Array.Empty(); + } - return objectConfig.GetProperties(); + return objectConfig.GetProperties(); + } } public static void ApplyPropertyConfigurationToTypes( ExtensionPropertyConfiguration propertyConfig, Type[] types) { - ObjectExtensionManager.Instance - .AddOrUpdateProperty( - types, - propertyConfig.Type, - propertyConfig.Name, - property => - { - property.Attributes.Clear(); - property.Attributes.AddRange(propertyConfig.Attributes); - property.DisplayName = propertyConfig.DisplayName; - property.Validators.AddRange(propertyConfig.Validators); - property.DefaultValue = propertyConfig.DefaultValue; - property.DefaultValueFactory = propertyConfig.DefaultValueFactory; - property.Lookup = propertyConfig.UI.Lookup; - } - ); + lock (SyncLock) + { + ObjectExtensionManager.Instance + .AddOrUpdateProperty( + types, + propertyConfig.Type, + propertyConfig.Name, + property => + { + property.Attributes.Clear(); + property.Attributes.AddRange(propertyConfig.Attributes); + property.DisplayName = propertyConfig.DisplayName; + property.Validators.AddRange(propertyConfig.Validators); + property.DefaultValue = propertyConfig.DefaultValue; + property.DefaultValueFactory = propertyConfig.DefaultValueFactory; + property.Lookup = propertyConfig.UI.Lookup; + } + ); + } } } } diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs index 2159e06d5e..d54ee7104a 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using JetBrains.Annotations; @@ -12,10 +13,10 @@ namespace Volo.Abp.ObjectExtending public Type Type { get; } [NotNull] - protected Dictionary Properties { get; } + protected ConcurrentDictionary Properties { get; } [NotNull] - public Dictionary Configuration { get; } + public ConcurrentDictionary Configuration { get; } [NotNull] public List> Validators { get; } @@ -23,8 +24,8 @@ namespace Volo.Abp.ObjectExtending public ObjectExtensionInfo([NotNull] Type type) { Type = Check.AssignableTo(type, nameof(type)); - Properties = new Dictionary(); - Configuration = new Dictionary(); + Properties = new ConcurrentDictionary(); + Configuration = new ConcurrentDictionary(); Validators = new List>(); } @@ -56,7 +57,7 @@ namespace Volo.Abp.ObjectExtending var propertyInfo = Properties.GetOrAdd( propertyName, - () => new ObjectExtensionPropertyInfo(this, propertyType, propertyName) + _ => new ObjectExtensionPropertyInfo(this, propertyType, propertyName) ); configureAction?.Invoke(propertyInfo); @@ -79,4 +80,4 @@ namespace Volo.Abp.ObjectExtending return Properties.GetOrDefault(propertyName); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs index fcc6fddbd0..4cbc3a3fb8 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs @@ -14,11 +14,11 @@ namespace Volo.Abp.ObjectExtending [NotNull] public ConcurrentDictionary Configuration { get; } - protected Dictionary ObjectsExtensions { get; } - + protected ConcurrentDictionary ObjectsExtensions { get; } + protected internal ObjectExtensionManager() { - ObjectsExtensions = new Dictionary(); + ObjectsExtensions = new ConcurrentDictionary(); Configuration = new ConcurrentDictionary(); } @@ -51,10 +51,10 @@ namespace Volo.Abp.ObjectExtending [CanBeNull] Action configureAction = null) { Check.AssignableTo(type, nameof(type)); - + var extensionInfo = ObjectsExtensions.GetOrAdd( type, - () => new ObjectExtensionInfo(type) + _ => new ObjectExtensionInfo(type) ); configureAction?.Invoke(extensionInfo); @@ -83,4 +83,4 @@ namespace Volo.Abp.ObjectExtending return ObjectsExtensions.Values.ToImmutableList(); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs index b5da5e6eeb..fee53f717a 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using JetBrains.Annotations; using Volo.Abp.Localization; using Volo.Abp.ObjectExtending.Modularity;