Browse Source

Map ExtraProperties of an entity to regular properties of a type-safe dto

pull/7666/head
ismail.yilmaz 5 years ago
parent
commit
dfd5634085
  1. 14
      framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs
  2. 16
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs
  3. 25
      framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperExtensibleDtoExtensions_Tests.cs
  4. 6
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs
  5. 4
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs
  6. 11
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPersonWithRegularPropertiesDto.cs

14
framework/src/Volo.Abp.AutoMapper/AutoMapper/AbpAutoMapperExtensibleDtoExtensions.cs

@ -10,7 +10,8 @@ namespace AutoMapper
public static IMappingExpression<TSource, TDestination> MapExtraProperties<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> 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<TSource, TDestination> IgnoreExtraProperties<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> mappingExpression)
where TDestination : IHasExtraProperties

16
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]);
}
}
}
}

25
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<string>("Name").ShouldBe("John");
personDto.Name.ShouldBe("John");
//Defined in both classes
personDto.GetProperty<int>("Age").ShouldBe(42);
personDto.Age.ShouldBe(42);
//Should not clear existing values
personDto.GetProperty<bool>("IsActive").ShouldBe(true);
personDto.IsActive.ShouldBe(true);
}
}
}

6
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs

@ -11,6 +11,12 @@ namespace Volo.Abp.AutoMapper.SampleClasses
CreateMap<ExtensibleTestPerson, ExtensibleTestPersonDto>()
.MapExtraProperties(ignoredProperties: new[] { "CityName" });
CreateMap<ExtensibleTestPerson, ExtensibleTestPersonWithRegularPropertiesDto>()
.ForMember(x => x.Name, y => y.Ignore())
.ForMember(x => x.Age, y => y.Ignore())
.ForMember(x => x.IsActive, y => y.Ignore())
.MapExtraProperties(mapToRegularProperties: true);
}
}
}

4
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs

@ -23,7 +23,9 @@ namespace Volo.Abp.ObjectExtending
.AddOrUpdateProperty<ExtensibleTestPerson, string>("CityName")
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>("Name")
.AddOrUpdateProperty<ExtensibleTestPersonDto, int>("ChildCount")
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>("CityName");
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>("CityName")
.AddOrUpdateProperty<ExtensibleTestPersonWithRegularPropertiesDto, string>("Name")
.AddOrUpdateProperty<ExtensibleTestPersonWithRegularPropertiesDto, int>("Age");
});
}
}

11
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; }
}
}
Loading…
Cancel
Save