using System; using System.Collections.Generic; using Volo.Abp.Data; namespace Volo.Abp.ObjectExtending { public static class HasExtraPropertiesObjectExtendingExtensions { /// /// Copies extra properties from the object /// to the object. /// /// Checks property definitions (over the ) /// based on the preference. /// /// Source class type /// Destination class type /// 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 { MapExtraPropertiesTo( source.ExtraProperties, destination.ExtraProperties, definitionCheck ); } /// /// Copies extra properties from the object /// to the object. /// /// Checks property definitions (over the ) /// based on the preference. /// /// Source class type (for definition check) /// Destination class type (for definition check) /// The source dictionary object /// The destination dictionary object /// /// Controls which properties to map. /// public static void MapExtraPropertiesTo( Dictionary sourceDictionary, Dictionary destinationDictionary, MappingPropertyDefinitionCheck definitionCheck = MappingPropertyDefinitionCheck.Both) where TSource : IHasExtraProperties where TDestination : IHasExtraProperties { 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; } if (definitionCheck == MappingPropertyDefinitionCheck.None) { foreach (var keyValue in sourceDictionary) { destinationDictionary[keyValue.Key] = keyValue.Value; } } else if (definitionCheck == MappingPropertyDefinitionCheck.Source) { foreach (var property in sourceObjectExtension.GetProperties()) { if (!sourceDictionary.ContainsKey(property.Name)) { continue; } destinationDictionary[property.Name] = sourceDictionary[property.Name]; } } else if (definitionCheck == MappingPropertyDefinitionCheck.Destination) { foreach (var keyValue in sourceDictionary) { if (!destinationObjectExtension.HasProperty(keyValue.Key)) { continue; } destinationDictionary[keyValue.Key] = keyValue.Value; } } else if (definitionCheck == MappingPropertyDefinitionCheck.Both) { foreach (var property in sourceObjectExtension.GetProperties()) { if (!sourceDictionary.ContainsKey(property.Name)) { continue; } if (!destinationObjectExtension.HasProperty(property.Name)) { continue; } destinationDictionary[property.Name] = sourceDictionary[property.Name]; } } else { throw new NotImplementedException(definitionCheck + " was not implemented!"); } } } }