From ff55b9f83fd00cb764454d8291cd59881e3041f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 28 Mar 2020 21:16:26 +0300 Subject: [PATCH] Added MappingPropertyDefinitionCheck. --- ...xtraPropertiesObjectExtendingExtensions.cs | 87 +++++++++++++++++-- .../MappingPropertyDefinitionCheck.cs | 28 ++++++ 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/MappingPropertyDefinitionCheck.cs diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions.cs index 3f415172e2..4226bb30f7 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions.cs @@ -1,22 +1,97 @@ -using Volo.Abp.Data; +using System; +using Volo.Abp.Data; namespace Volo.Abp.ObjectExtending { public static class HasExtraPropertiesObjectExtendingExtensions { - public static void MapExtraPropertiesTo(this TSource source, TDestination destination) + /// + /// Copies extra properties from the object + /// to the object. + /// + /// Checks property definitions (over the ) + /// based on the preference. + /// + /// + /// + /// The source object + /// The destination object + /// + /// Controls which properties to map. + /// + public static void MapExtraPropertiesTo( + this TSource source, + TDestination destination, + MappingPropertyDefinitionCheck definitionCheck = MappingPropertyDefinitionCheck.Both) where TSource : IHasExtraProperties where TDestination : IHasExtraProperties { - var extensionPropertyInfos = ObjectExtensionManager.Instance.AddOrUpdate().GetProperties(); + var sourceObjectExtension = ObjectExtensionManager.Instance.GetOrNull(); + if (definitionCheck.HasFlag(MappingPropertyDefinitionCheck.Source) && + sourceObjectExtension == null) + { + return; + } + + var destinationObjectExtension = ObjectExtensionManager.Instance.GetOrNull(); + if (definitionCheck.HasFlag(MappingPropertyDefinitionCheck.Destination) && + destinationObjectExtension == null) + { + return; + } - foreach (var extensionPropertyInfo in extensionPropertyInfos) + if (definitionCheck == MappingPropertyDefinitionCheck.None) + { + foreach (var keyValue in source.ExtraProperties) + { + destination.ExtraProperties[keyValue.Key] = keyValue.Value; + } + } + else if (definitionCheck == MappingPropertyDefinitionCheck.Source) { - if (source.ExtraProperties.ContainsKey(extensionPropertyInfo.Name)) + foreach (var property in sourceObjectExtension.GetProperties()) { - destination.ExtraProperties[extensionPropertyInfo.Name] = source.ExtraProperties[extensionPropertyInfo.Name]; + if (!source.ExtraProperties.ContainsKey(property.Name)) + { + continue; + } + + destination.ExtraProperties[property.Name] = source.ExtraProperties[property.Name]; } } + else if (definitionCheck == MappingPropertyDefinitionCheck.Destination) + { + foreach (var keyValue in source.ExtraProperties) + { + if (!destinationObjectExtension.HasProperty(keyValue.Key)) + { + continue; + } + + destination.ExtraProperties[keyValue.Key] = keyValue.Value; + } + } + else if(definitionCheck == MappingPropertyDefinitionCheck.Both) + { + foreach (var property in sourceObjectExtension.GetProperties()) + { + if (!source.ExtraProperties.ContainsKey(property.Name)) + { + continue; + } + + if (!destinationObjectExtension.HasProperty(property.Name)) + { + continue; + } + + destination.ExtraProperties[property.Name] = source.ExtraProperties[property.Name]; + } + } + else + { + throw new NotImplementedException(definitionCheck + " was not implemented!"); + } } } } diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/MappingPropertyDefinitionCheck.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/MappingPropertyDefinitionCheck.cs new file mode 100644 index 0000000000..f1bd9c3c66 --- /dev/null +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/MappingPropertyDefinitionCheck.cs @@ -0,0 +1,28 @@ +using System; + +namespace Volo.Abp.ObjectExtending +{ + [Flags] + public enum MappingPropertyDefinitionCheck : byte + { + /// + /// No check. Copy all extra properties from the source to the destination. + /// + None = 0, + + /// + /// Copy the extra properties defined for the source class. + /// + Source = 1, + + /// + /// Copy the extra properties defined for the destination class. + /// + Destination = 2, + + /// + /// Copy extra properties defined for both of the source and destination classes. + /// + Both = Source | Destination + } +} \ No newline at end of file