Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,7 +10,8 @@ public static class AbpAutoMapperExtensibleDtoExtensions
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
{
Expand All @@ -34,9 +35,16 @@ public static class AbpAutoMapperExtensibleDtoExtensions

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
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -106,7 +105,20 @@ public static void SetDefaultsForExtraProperties(object source, Type objectType)
throw new ArgumentException($"Given {nameof(source)} object does not implement the {nameof(IHasExtraProperties)} interface!", nameof(source));
}

((IHasExtraProperties) source).SetDefaultsForExtraProperties(objectType);
((IHasExtraProperties)source).SetDefaultsForExtraProperties(objectType);
}

public static void SetExtraPropertiesToRegularProperties(this IHasExtraProperties source)
{
var properties = source.GetType().GetProperties()
.Where(x => source.ExtraProperties.ContainsKey(x.Name)
&& x.GetSetMethod(true) != null)
.ToList();

foreach (var property in properties)
{
property.SetValue(source, source.ExtraProperties[property.Name]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this property from source.ExtraProperties. No need to duplication. If we define a regular property, we want to use it instead of extra property.
Duplication is dangerous here. Because if we change one of them, the other one won't change.

}
}
}
}
Expand Up @@ -39,5 +39,30 @@ public void MapExtraPropertiesTo_Should_Only_Map_Defined_Properties_By_Default()
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);
}
}
}
Expand Up @@ -11,6 +11,12 @@ public MyMapProfile()

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);
}
}
}
Expand Up @@ -23,7 +23,9 @@ public override void PreConfigureServices(ServiceConfigurationContext context)
.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");
});
}
}
Expand Down
@@ -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; }
}
}