diff --git a/Directory.Build.props b/Directory.Build.props index 919959f..a25b35a 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ Jimmy Bogard latest - 3.1.0-preview02 + 3.1.0-preview03 true $(NoWarn);1701;1702;1591 diff --git a/src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs b/src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs index ce04425..26a335c 100644 --- a/src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs +++ b/src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs @@ -279,20 +279,22 @@ public static Dictionary AddTypeMapping(this Dictionary return typeMappings; } - private static void AddIncludedTypeMaps(this Dictionary typeMappings, IConfigurationProvider configurationProvider, Type source, Type dest) + private static void AddIncludedTypeMaps(this Dictionary typeMappings, IConfigurationProvider configurationProvider, Type source/*model*/, Type dest/*data*/)//model to date { - AddTypeMaps(configurationProvider.ResolveTypeMap(source, dest)); + //Stay with the existing design of using configured data to model maps to retrieve the type mappings. + //This is needed for property map custom expressions. + AddTypeMaps(configurationProvider.ResolveTypeMap(sourceType: dest/*data*/, destinationType: source/*model*/)); void AddTypeMaps(TypeMap typeMap) { if (typeMap == null) return; - foreach (var includedBase in typeMap.IncludedBaseTypes) - typeMappings.AddTypeMapping(configurationProvider, includedBase.SourceType, includedBase.DestinationType); + foreach (TypePair baseTypePair in typeMap.IncludedBaseTypes) + typeMappings.AddTypeMapping(configurationProvider, baseTypePair.DestinationType/*model*/, baseTypePair.SourceType/*data*/); - foreach (var includedDerived in typeMap.IncludedDerivedTypes) - typeMappings.AddTypeMapping(configurationProvider, includedDerived.SourceType, includedDerived.DestinationType); + foreach (TypePair derivedTypePair in typeMap.IncludedDerivedTypes) + typeMappings.AddTypeMapping(configurationProvider, derivedTypePair.DestinationType/*model*/, derivedTypePair.SourceType/*data*/); } } diff --git a/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.cs b/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.cs index 97d5478..e26c978 100644 --- a/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.cs +++ b/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.cs @@ -13,27 +13,6 @@ public class ExpressionMappingPropertyFromDerviedType : AutoMapperSpecBase private List _source; private IQueryable entityQuery; - public class BaseDTO - { - public Guid Id { get; set; } - } - - public class BaseEntity - { - public Guid Id { get; set; } - } - - public class DTO : BaseDTO - { - public string Name { get; set; } - public string Description { get; set; } - } - - public class Entity : BaseEntity - { - public string Name { get; set; } - } - protected override MapperConfiguration Configuration { get @@ -41,15 +20,7 @@ protected override MapperConfiguration Configuration var config = new MapperConfiguration(cfg => { cfg.AddExpressionMapping(); - - cfg.CreateMap(); - cfg.CreateMap(); - - cfg.CreateMap() - .ForMember(dest => dest.Description, opts => opts.MapFrom(src => string.Concat(src.Id.ToString(), " - ", src.Name))) - .IncludeBase(); - cfg.CreateMap() - .IncludeBase(); + cfg.AddProfile(typeof(DerivedTypeProfile)); }); return config; } @@ -88,5 +59,38 @@ public void Should_support_propertypath_expressions_with_properties_from_sub_typ // Assert entityQuery.ToList().Count().ShouldBe(1); } + + public class DerivedTypeProfile : Profile + { + public DerivedTypeProfile() + { + CreateMap(); + + CreateMap() + .ForMember(dest => dest.Description, opts => opts.MapFrom(src => string.Concat(src.Id.ToString(), " - ", src.Name))) + .IncludeBase(); + } + } + + public class BaseDTO + { + public Guid Id { get; set; } + } + + public class BaseEntity + { + public Guid Id { get; set; } + } + + public class DTO : BaseDTO + { + public string Name { get; set; } + public string Description { get; set; } + } + + public class Entity : BaseEntity + { + public string Name { get; set; } + } } } diff --git a/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.ForPath.Tests.cs b/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.ForPath.Tests.cs index 25ee1bc..98b186c 100644 --- a/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.ForPath.Tests.cs +++ b/tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.ForPath.Tests.cs @@ -22,8 +22,8 @@ public void Works_for_inherited_properties() Expression> selection = s => s.Nested.NestedTitle2 == "nested test"; //Act - Expression> selectionMapped = mapper.Map>>(selection); - List items = DataObjects.Where(selectionMapped).ToList(); + Expression> selectionMapped = mapper.Map>>(selection); + List items = DataObjects.Where(selectionMapped).ToList(); //Assert Assert.True(items.Count == 1); @@ -36,8 +36,8 @@ public void Works_for_inherited_properties_on_base_types() Expression> selection = s => ((DerivedModel)s).Nested.NestedTitle2 == "nested test"; //Act - Expression> selectionMapped = mapper.MapExpression>>(selection); - List items = DataObjects.Where(selectionMapped).ToList(); + Expression> selectionMapped = mapper.MapExpression>>(selection); + List items = DataObjects.Where(selectionMapped).ToList(); //Assert Assert.True(items.Count == 1); @@ -122,11 +122,11 @@ public void Throws_exception_when_mapped_string_is_a_child_of_the_parameter() private void SetupQueryableCollection() { - DataObjects = new DerivedDataModel[] + DataObjects = new DerivedData[] { - new DerivedDataModel() { OtherID = 2, Title2 = "nested test", ID = 1, Title = "test", DescendantField = "descendant field" }, - new DerivedDataModel() { OtherID = 3, Title2 = "nested", ID = 4, Title = "title", DescendantField = "some text" } - }.AsQueryable(); + new DerivedData() { OtherID = 2, Title2 = "nested test", ID = 1, Title = "test", DescendantField = "descendant field" }, + new DerivedData() { OtherID = 3, Title2 = "nested", ID = 4, Title = "title", DescendantField = "some text" } + }.AsQueryable(); Orders = new OrderDto[] { @@ -146,7 +146,7 @@ private void SetupQueryableCollection() } private static IQueryable Orders { get; set; } - private static IQueryable DataObjects { get; set; } + private static IQueryable DataObjects { get; set; } private void SetupAutoMapper() { @@ -181,7 +181,7 @@ public class DerivedModel : RootModel public string DescendantField { get; set; } } - public class DataModel + public class RootData { public int ID { get; set; } public string Title { get; set; } @@ -190,7 +190,7 @@ public class DataModel public string Title2 { get; set; } } - public class DerivedDataModel : DataModel + public class DerivedData : RootData { public string DescendantField { get; set; } } @@ -231,10 +231,10 @@ public class ForPathCustomerProfile : Profile { public ForPathCustomerProfile() { - CreateMap() - .Include(); + CreateMap() + .Include(); - CreateMap() + CreateMap() .ForPath(d => d.Nested.NestedTitle, opt => opt.MapFrom(src => src.Title)) .ForPath(d => d.Nested.NestedTitle2, opt => opt.MapFrom(src => src.Title2)) .ReverseMap();