Automapper: Auto map collection property for a dto

2019-02-10 16:50发布

问题:

I have a domain object

public class ProductModel
{
    public long Id {get;set;}
    public string Name {get;set;}
    public string SerialNumber {get;set;}
}

Single Dto class:

public class ProductDto
{
    public long Id {get;set;}
    public string Name {get;set;}
    public string SerialNumber {get;set;}
}

Single Dto class that is a list of Dto object:

public class ProductListDto : List<ProductDto>
{
    public List<ProductDto> Products;

    public ProductListDto()
    {
        Products = new List<ProductDto>();
    }
}

And I'd like to map a list of domain objects to list of Dto objects such that the "Products" property of ProductListDto object AUTOMATICALLY is mapped with a list of ProductModel objects:

ProductListDto dto = new ProductListDto();  

Mapper.CreateMap<ProductModel, ProductDto>();

/* dto = (ProductListDto) Mapper.Map<List<ProductModel>, List<ProductDto>>((List<ProductModel>)model);  this code line causes error. It is commented out. */ 

dto.Products = Mapper.Map<List<ProductModel>, List<ProductDto>>((List<ProductModel>)model);  // (*)  works OK but need to specify "Products" property

The code line (*) works OK, but I'd like to know if there is another way to AUTOMATICALLY (implicitly) map that "Products" property of dto object other than the code line (*)?

That means I do not have to write code like the left hand side of code line (*).

回答1:

You will need to create a mapping for it. Something like this should work:

namespace StackOverflow
{
    using System.Collections.Generic;

    using AutoMapper;

    public class MyProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "MyProfile";
            }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<ProductModel, ProductDto>();

            Mapper.CreateMap<List<ProductModel>, ProductListDto>()
                .ForMember(dest => dest.Products,
                           opt => opt.MapFrom(
                               src => Mapper.Map<List<ProductModel>,
                                                 List<ProductDto>>(src)));
        }
    }
}

Then in your code you can do:

dto = Mapper.Map<List<ProductModel>, ProductListDto>((List<ProductModel>)model);

Here are a couple of unit tests to show how it works:

namespace StackOverflow
{
    using System.Collections.Generic;

    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
        }

        [Test]
        public void AutoMapper_DriverMapping_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();

            var products = new List<ProductModel>
                {
                    new ProductModel
                        {
                            Id = 1,
                            Name = "StackOverflow Rocks",
                            SerialNumber = "1234"
                        },
                    new ProductModel
                        {
                            Id = 2,
                            Name = "I Also Rock",
                            SerialNumber = "4321"
                        }
                };

            var productsDto =
                    Mapper.Map<List<ProductModel>, ProductListDto>(products);

            Assert.That(productsDto, Is.Not.Null);
            Assert.That(productsDto.Products, Is.Not.Null);
            Assert.That(productsDto.Products.Count, Is.EqualTo(2));
        }
    }
}


标签: AutoMapper