Browse Source

Added MappingPropertyDefinitionCheck.

pull/3401/head
Halil İbrahim Kalkan 7 years ago
parent
commit
ff55b9f83f
  1. 87
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions.cs
  2. 28
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/MappingPropertyDefinitionCheck.cs

87
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<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!");
}
}
}
}

28
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
{
/// <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…
Cancel
Save