Implement synchronization for object extensions.

pull/6342/head
Halil İbrahim Kalkan 5 years ago
parent 869bc26a02
commit cd4a7200f9

@ -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<ExtensionPropertyConfiguration>();
}
var moduleConfig = ObjectExtensionManager.Instance.Modules().GetOrDefault(moduleName);
if (moduleConfig == null)
{
return Array.Empty<ExtensionPropertyConfiguration>();
}
var objectConfig = moduleConfig.Entities.GetOrDefault(entityName);
if (objectConfig == null)
{
return Array.Empty<ExtensionPropertyConfiguration>();
}
var objectConfig = moduleConfig.Entities.GetOrDefault(entityName);
if (objectConfig == null)
{
return Array.Empty<ExtensionPropertyConfiguration>();
}
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;
}
);
}
}
}
}

@ -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<string, ObjectExtensionPropertyInfo> Properties { get; }
protected ConcurrentDictionary<string, ObjectExtensionPropertyInfo> Properties { get; }
[NotNull]
public Dictionary<object, object> Configuration { get; }
public ConcurrentDictionary<object, object> Configuration { get; }
[NotNull]
public List<Action<ObjectExtensionValidationContext>> Validators { get; }
@ -23,8 +24,8 @@ namespace Volo.Abp.ObjectExtending
public ObjectExtensionInfo([NotNull] Type type)
{
Type = Check.AssignableTo<IHasExtraProperties>(type, nameof(type));
Properties = new Dictionary<string, ObjectExtensionPropertyInfo>();
Configuration = new Dictionary<object, object>();
Properties = new ConcurrentDictionary<string, ObjectExtensionPropertyInfo>();
Configuration = new ConcurrentDictionary<object, object>();
Validators = new List<Action<ObjectExtensionValidationContext>>();
}
@ -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);
}
}
}
}

@ -14,11 +14,11 @@ namespace Volo.Abp.ObjectExtending
[NotNull]
public ConcurrentDictionary<object, object> Configuration { get; }
protected Dictionary<Type, ObjectExtensionInfo> ObjectsExtensions { get; }
protected ConcurrentDictionary<Type, ObjectExtensionInfo> ObjectsExtensions { get; }
protected internal ObjectExtensionManager()
{
ObjectsExtensions = new Dictionary<Type, ObjectExtensionInfo>();
ObjectsExtensions = new ConcurrentDictionary<Type, ObjectExtensionInfo>();
Configuration = new ConcurrentDictionary<object, object>();
}
@ -51,10 +51,10 @@ namespace Volo.Abp.ObjectExtending
[CanBeNull] Action<ObjectExtensionInfo> configureAction = null)
{
Check.AssignableTo<IHasExtraProperties>(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();
}
}
}
}

@ -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;

Loading…
Cancel
Save