From c198dd8f9e8a9b04133fd58f9f3d9dc8accbfd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 6 Nov 2020 21:21:03 +0300 Subject: [PATCH] Change Dictionary to ExtraPropertyDictionary for IHasExtraProperties --- .../Volo/Abp/Auditing/AuditLogActionInfo.cs | 6 +++--- .../Volo/Abp/Auditing/AuditLogInfo.cs | 6 +++--- .../Volo/Abp/Auditing/EntityChangeInfo.cs | 4 ++-- .../Volo/Abp/Domain/Entities/AggregateRoot.cs | 11 +++++------ .../Modeling/AbpEntityTypeBuilderExtensions.cs | 4 ++-- ....cs => ExtraPropertyDictionaryValueComparer.cs} | 8 ++++---- .../ExtraPropertiesValueConverter.cs | 10 ++++++---- .../Volo/Abp/Data/IHasExtraProperties.cs | 14 +++++++++++++- .../Volo/Abp/ObjectExtending/ExtensibleObject.cs | 4 ++-- 9 files changed, 40 insertions(+), 27 deletions(-) rename framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/{AbpDictionaryValueComparer.cs => ExtraPropertyDictionaryValueComparer.cs} (58%) diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogActionInfo.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogActionInfo.cs index d1b3bcfff9..6813d372b0 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogActionInfo.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogActionInfo.cs @@ -17,11 +17,11 @@ namespace Volo.Abp.Auditing public int ExecutionDuration { get; set; } - public Dictionary ExtraProperties { get; } + public ExtraPropertyDictionary ExtraProperties { get; } public AuditLogActionInfo() { - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs index f780db924a..501e12d66c 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs @@ -47,7 +47,7 @@ namespace Volo.Abp.Auditing public List Exceptions { get; } - public Dictionary ExtraProperties { get; } + public ExtraPropertyDictionary ExtraProperties { get; } public List EntityChanges { get; } @@ -57,7 +57,7 @@ namespace Volo.Abp.Auditing { Actions = new List(); Exceptions = new List(); - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); EntityChanges = new List(); Comments = new List(); } @@ -107,4 +107,4 @@ namespace Volo.Abp.Auditing return sb.ToString(); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/EntityChangeInfo.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/EntityChangeInfo.cs index 5bf45f1375..7842d04f0e 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/EntityChangeInfo.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/EntityChangeInfo.cs @@ -25,13 +25,13 @@ namespace Volo.Abp.Auditing public List PropertyChanges { get; set; } - public Dictionary ExtraProperties { get; } + public ExtraPropertyDictionary ExtraProperties { get; } public virtual object EntityEntry { get; set; } //TODO: Try to remove since it breaks serializability public EntityChangeInfo() { - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); } public virtual void Merge(EntityChangeInfo changeInfo) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/AggregateRoot.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/AggregateRoot.cs index 09af979e93..6d848b2dca 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/AggregateRoot.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/AggregateRoot.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using Volo.Abp.Auditing; using Volo.Abp.Data; @@ -13,7 +12,7 @@ namespace Volo.Abp.Domain.Entities IHasExtraProperties, IHasConcurrencyStamp { - public virtual Dictionary ExtraProperties { get; protected set; } + public virtual ExtraPropertyDictionary ExtraProperties { get; protected set; } [DisableAuditing] public virtual string ConcurrencyStamp { get; set; } @@ -21,7 +20,7 @@ namespace Volo.Abp.Domain.Entities protected AggregateRoot() { ConcurrencyStamp = Guid.NewGuid().ToString("N"); - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); this.SetDefaultsForExtraProperties(); } @@ -39,7 +38,7 @@ namespace Volo.Abp.Domain.Entities IHasExtraProperties, IHasConcurrencyStamp { - public virtual Dictionary ExtraProperties { get; protected set; } + public virtual ExtraPropertyDictionary ExtraProperties { get; protected set; } [DisableAuditing] public virtual string ConcurrencyStamp { get; set; } @@ -47,7 +46,7 @@ namespace Volo.Abp.Domain.Entities protected AggregateRoot() { ConcurrencyStamp = Guid.NewGuid().ToString("N"); - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); this.SetDefaultsForExtraProperties(); } @@ -55,7 +54,7 @@ namespace Volo.Abp.Domain.Entities : base(id) { ConcurrencyStamp = Guid.NewGuid().ToString("N"); - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); this.SetDefaultsForExtraProperties(); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs index 44962eca4c..a40c4d6254 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs @@ -60,10 +60,10 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling return; } - b.Property>(nameof(IHasExtraProperties.ExtraProperties)) + b.Property(nameof(IHasExtraProperties.ExtraProperties)) .HasColumnName(nameof(IHasExtraProperties.ExtraProperties)) .HasConversion(new ExtraPropertiesValueConverter(b.Metadata.ClrType)) - .Metadata.SetValueComparer(new AbpDictionaryValueComparer()); + .Metadata.SetValueComparer(new ExtraPropertyDictionaryValueComparer()); b.TryConfigureObjectExtensions(); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/AbpDictionaryValueComparer.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/ExtraPropertyDictionaryValueComparer.cs similarity index 58% rename from framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/AbpDictionaryValueComparer.cs rename to framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/ExtraPropertyDictionaryValueComparer.cs index c6959b0801..01c2be346e 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/AbpDictionaryValueComparer.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueComparers/ExtraPropertyDictionaryValueComparer.cs @@ -1,17 +1,17 @@ using System; -using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore.ChangeTracking; +using Volo.Abp.Data; namespace Volo.Abp.EntityFrameworkCore.ValueComparers { - public class AbpDictionaryValueComparer : ValueComparer> + public class ExtraPropertyDictionaryValueComparer : ValueComparer { - public AbpDictionaryValueComparer() + public ExtraPropertyDictionaryValueComparer() : base( (d1, d2) => d1.SequenceEqual(d2), d => d.Aggregate(0, (k, v) => HashCode.Combine(k, v.GetHashCode())), - d => d.ToDictionary(k => k.Key, v => v.Value)) + d => new ExtraPropertyDictionary(d)) { } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs index 661da95a19..0da81e6528 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs @@ -2,12 +2,13 @@ using System.Collections.Generic; using System.Text.Json; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Volo.Abp.Data; using Volo.Abp.Json.SystemTextJson.JsonConverters; using Volo.Abp.ObjectExtending; namespace Volo.Abp.EntityFrameworkCore.ValueConverters { - public class ExtraPropertiesValueConverter : ValueConverter, string> + public class ExtraPropertiesValueConverter : ValueConverter { public ExtraPropertiesValueConverter(Type entityType) : base( @@ -17,7 +18,7 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters } - private static string SerializeObject(Dictionary extraProperties, Type entityType) + private static string SerializeObject(ExtraPropertyDictionary extraProperties, Type entityType) { var copyDictionary = new Dictionary(extraProperties); @@ -39,11 +40,12 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters return JsonSerializer.Serialize(copyDictionary); } - private static Dictionary DeserializeObject(string extraPropertiesAsJson, Type entityType) + private static ExtraPropertyDictionary DeserializeObject(string extraPropertiesAsJson, Type entityType) { var deserializeOptions = new JsonSerializerOptions(); deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter()); - var dictionary = JsonSerializer.Deserialize>(extraPropertiesAsJson, deserializeOptions) ?? new Dictionary(); + var dictionary = JsonSerializer.Deserialize(extraPropertiesAsJson, deserializeOptions) ?? + new ExtraPropertyDictionary(); if (entityType != null) { var objectExtension = ObjectExtensionManager.Instance.GetOrNull(entityType); diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/IHasExtraProperties.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/IHasExtraProperties.cs index a637351e66..61a9f267f0 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/IHasExtraProperties.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/IHasExtraProperties.cs @@ -6,6 +6,18 @@ namespace Volo.Abp.Data public interface IHasExtraProperties { - Dictionary ExtraProperties { get; } + ExtraPropertyDictionary ExtraProperties { get; } + } + + public class ExtraPropertyDictionary : Dictionary + { + public ExtraPropertyDictionary() + { + } + + public ExtraPropertyDictionary(IDictionary dictionary) + : base(dictionary) + { + } } } diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs index d83efac53f..d068015581 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs @@ -9,7 +9,7 @@ namespace Volo.Abp.ObjectExtending [Serializable] public class ExtensibleObject : IHasExtraProperties, IValidatableObject { - public Dictionary ExtraProperties { get; protected set; } + public ExtraPropertyDictionary ExtraProperties { get; protected set; } public ExtensibleObject() : this(true) @@ -19,7 +19,7 @@ namespace Volo.Abp.ObjectExtending public ExtensibleObject(bool setDefaultsForExtraProperties) { - ExtraProperties = new Dictionary(); + ExtraProperties = new ExtraPropertyDictionary(); if (setDefaultsForExtraProperties) {