mirror of https://github.com/abpframework/abp.git
2 changed files with 109 additions and 6 deletions
@ -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<TSource, TDestination>(this TSource source, TDestination destination) |
|||
/// <summary>
|
|||
/// Copies extra properties from the <paramref name="source"/> object
|
|||
/// to the <paramref name="destination"/> object.
|
|||
///
|
|||
/// Checks property definitions (over the <see cref="ObjectExtensionManager"/>)
|
|||
/// based on the <paramref name="definitionCheck"/> preference.
|
|||
/// </summary>
|
|||
/// <typeparam name="TSource"></typeparam>
|
|||
/// <typeparam name="TDestination"></typeparam>
|
|||
/// <param name="source">The source object</param>
|
|||
/// <param name="destination">The destination object</param>
|
|||
/// <param name="definitionCheck">
|
|||
/// Controls which properties to map.
|
|||
/// </param>
|
|||
public static void MapExtraPropertiesTo<TSource, TDestination>( |
|||
this TSource source, |
|||
TDestination destination, |
|||
MappingPropertyDefinitionCheck definitionCheck = MappingPropertyDefinitionCheck.Both) |
|||
where TSource : IHasExtraProperties |
|||
where TDestination : IHasExtraProperties |
|||
{ |
|||
var extensionPropertyInfos = ObjectExtensionManager.Instance.AddOrUpdate<TSource>().GetProperties(); |
|||
var sourceObjectExtension = ObjectExtensionManager.Instance.GetOrNull<TSource>(); |
|||
if (definitionCheck.HasFlag(MappingPropertyDefinitionCheck.Source) && |
|||
sourceObjectExtension == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var destinationObjectExtension = ObjectExtensionManager.Instance.GetOrNull<TSource>(); |
|||
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!"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,28 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
[Flags] |
|||
public enum MappingPropertyDefinitionCheck : byte |
|||
{ |
|||
/// <summary>
|
|||
/// No check. Copy all extra properties from the source to the destination.
|
|||
/// </summary>
|
|||
None = 0, |
|||
|
|||
/// <summary>
|
|||
/// Copy the extra properties defined for the source class.
|
|||
/// </summary>
|
|||
Source = 1, |
|||
|
|||
/// <summary>
|
|||
/// Copy the extra properties defined for the destination class.
|
|||
/// </summary>
|
|||
Destination = 2, |
|||
|
|||
/// <summary>
|
|||
/// Copy extra properties defined for both of the source and destination classes.
|
|||
/// </summary>
|
|||
Both = Source | Destination |
|||
} |
|||
} |
|||
Loading…
Reference in new issue