automapper - Automapper:dto对象的自动 map 收集属性

标签 automapper

我有一个领域对象

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对象的“Products”属性自动与ProductModel对象的列表进行映射:

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对象的“Products”属性而不是代码行(*)?

这意味着我不必像代码行(*)的左侧那样编写代码。

最佳答案

您将需要为其创建一个映射。这样的事情应该起作用:

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 - Automapper:dto对象的自动 map 收集属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479208/

相关文章:

c# - Automapper 映射 Arraylist

c# - 将附加对象传递给 CreateMap

c# - 如何使用 AutoMapper 忽略目标成员?

c# - 找到未映射的成员(即使忽略该属性)

c# - 我可以使用 AutoMapper 在两个不可变字段之间进行映射吗?

c# - 使用 AutoMapper 获取异常

c# - 如何使用 automapper 映射具有多个表的数据集

c# - Linq 使用自动映射器将 null 设置为一个值

c# - C# 中的自动映射器错误。...的最佳重载匹配有一些无效参数

asp.net-mvc-3 - 使用Automapper/EF代码优先进行数据库更新