diff --git a/framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs b/framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs index d26ec97bf2..5d93217762 100644 --- a/framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs +++ b/framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs @@ -10,7 +10,8 @@ namespace AutoMapper public static IMappingExpression MapExtraProperties( this IMappingExpression mappingExpression, MappingPropertyDefinitionChecks? definitionChecks = null, - string[] ignoredProperties = null) + string[] ignoredProperties = null, + bool mapToRegularProperties = false) where TDestination : IHasExtraProperties where TSource : IHasExtraProperties { @@ -34,9 +35,16 @@ namespace AutoMapper return result; }) - ); + ) + .AfterMap((source, destination, context) => + { + if (mapToRegularProperties) + { + destination.SetExtraPropertiesToRegularProperties(); + } + }); } - + public static IMappingExpression IgnoreExtraProperties( this IMappingExpression mappingExpression) where TDestination : IHasExtraProperties diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs index 22b135c3d0..10086ecf21 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs @@ -2,8 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Globalization; -using Volo.Abp.DynamicProxy; -using Volo.Abp.Localization; +using System.Linq; using Volo.Abp.ObjectExtending; using Volo.Abp.Reflection; @@ -108,5 +107,18 @@ namespace Volo.Abp.Data ((IHasExtraProperties) source).SetDefaultsForExtraProperties(objectType); } + + public static void SetExtraPropertiesToRegularProperties(this IHasExtraProperties source) + { + var properties = source.GetType().GetProperties() + .Where(x => source.ExtraProperties.Keys.Contains(x.Name) + && x.GetSetMethod(true) != null) + .ToList(); + + foreach (var property in properties) + { + property.SetValue(source, source.ExtraProperties[property.Name]); + } + } } } diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperExtensibleDtoExtensions_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperExtensibleDtoExtensions_Tests.cs index 4ec19bb84e..c8dedffdc5 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperExtensibleDtoExtensions_Tests.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperExtensibleDtoExtensions_Tests.cs @@ -39,5 +39,30 @@ namespace AutoMapper personDto.HasProperty("Age").ShouldBeFalse(); //Not defined on the destination personDto.HasProperty("Sex").ShouldBeFalse(); //Not defined in both classes } + + [Fact] + public void MapExtraProperties_Also_Should_Map_To_RegularProperties() + { + var person = new ExtensibleTestPerson() + .SetProperty("Name", "John") + .SetProperty("Age", 42); + + var personDto = new ExtensibleTestPersonWithRegularPropertiesDto() + .SetProperty("IsActive", true); + + _objectMapper.Map(person, personDto); + + //Defined in both classes + personDto.GetProperty("Name").ShouldBe("John"); + personDto.Name.ShouldBe("John"); + + //Defined in both classes + personDto.GetProperty("Age").ShouldBe(42); + personDto.Age.ShouldBe(42); + + //Should not clear existing values + personDto.GetProperty("IsActive").ShouldBe(true); + personDto.IsActive.ShouldBe(true); + } } } diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs index 6c69d53e5f..1f7c4614e6 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs @@ -11,6 +11,12 @@ namespace Volo.Abp.AutoMapper.SampleClasses CreateMap() .MapExtraProperties(ignoredProperties: new[] { "CityName" }); + + CreateMap() + .ForMember(x => x.Name, y => y.Ignore()) + .ForMember(x => x.Age, y => y.Ignore()) + .ForMember(x => x.IsActive, y => y.Ignore()) + .MapExtraProperties(mapToRegularProperties: true); } } } diff --git a/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs b/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs index ba7091befa..f499cb024c 100644 --- a/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs +++ b/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs @@ -23,7 +23,9 @@ namespace Volo.Abp.ObjectExtending .AddOrUpdateProperty("CityName") .AddOrUpdateProperty("Name") .AddOrUpdateProperty("ChildCount") - .AddOrUpdateProperty("CityName"); + .AddOrUpdateProperty("CityName") + .AddOrUpdateProperty("Name") + .AddOrUpdateProperty("Age"); }); } } diff --git a/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPersonWithRegularPropertiesDto.cs b/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPersonWithRegularPropertiesDto.cs new file mode 100644 index 0000000000..ac24e61fdd --- /dev/null +++ b/framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPersonWithRegularPropertiesDto.cs @@ -0,0 +1,11 @@ +namespace Volo.Abp.ObjectExtending.TestObjects +{ + public class ExtensibleTestPersonWithRegularPropertiesDto : ExtensibleObject + { + public string Name { get; set; } + + public int Age { get; set; } + + public bool IsActive { get; set; } + } +} \ No newline at end of file