Automapper:自动映射集合属性的DTO对象(Automapper: Auto map col

2019-07-04 00:15发布

我有一个域对象

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

单DTO类:

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

单DTO类,它是DTO对象的列表:

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

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

我想域对象的列表映射到列表的DTO对象,使得ProductListDto对象的“产品”属性会自动映射与产品型号对象的列表:

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

该代码行(*)的作品不错,但我想知道是否有另一种方式为自动(隐含的)映射比代码行(*)其他DTO对象,即“产品”的属性?

这意味着我不用写代码一样的代码行(*)的左侧。

Answer 1:

您将需要创建一个映射它。 像这样的东西应该工作:

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

然后在你的代码,你可以这样做:

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

这里有几个单元测试,以显示它是如何工作:

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: Auto map collection property for a dto object
标签: AutoMapper