A better way to use AutoMapper to flatten nested objects?

前端 未结 7 1622
执笔经年
执笔经年 2020-12-28 15:02

I have been flattening domain objects into DTOs as shown in the example below:

public class Root
{
    public string AParentProperty { get; set; }
    public         


        
7条回答
  •  孤城傲影
    2020-12-28 15:25

    I much prefer avoiding the older Static methods and do it like this.

    Place our mapping definitions into a Profile. We map the Root first, and then apply the mappings of the Nested afterwards. Note the use of the Context.

    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap()
                .AfterMap((src, dest, context) => context.Mapper.Map(src.TheNestedClass, dest));
            CreateMap();
        }
    }
    

    The advantage of defining both the mapping from Root to Flattened and Nested to Flatterned is that you retain full control over the mapping of the properties, such as if the destination property name is different or you want to apply a transformation etc.

    An XUnit test:

    [Fact]
    public void Mapping_root_to_flattened_should_include_nested_properties()
    {
        // ARRANGE
        var myRoot = new Root
        {
            AParentProperty = "my AParentProperty",
            TheNestedClass = new Nested
            {
                ANestedProperty = "my ANestedProperty"
            }
        };
    
        // Manually create the mapper using the Profile
        var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())).CreateMapper();
    
        // ACT
        var myFlattened = mapper.Map(myRoot);
    
        // ASSERT
        Assert.Equal(myRoot.AParentProperty, myFlattened.AParentProperty);
        Assert.Equal(myRoot.TheNestedClass.ANestedProperty, myFlattened.ANestedProperty);
    }
    

    By adding AutoMapper's serviceCollection.AddAutoMapper() from the AutoMapper.Extensions.Microsoft.DependencyInjection nuget package to your start up, the Profile will be picked up automatically, and you can simply inject IMapper into wherever you are applying the mapping.

提交回复
热议问题