【问题标题】:Automapper: Auto map collection property for a dto objectAutomapper:dto 对象的自动映射集合属性
【发布时间】:2012-11-08 20:59:29
【问题描述】:

我有一个域对象

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 对象的“产品”属性自动映射到 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 对象的“产品”属性?

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

【问题讨论】:

  • 你是在问你是否可以做类似dto = Mapper.Map(...)的事情?
  • 是的,Mightymuke。我尝试过,但在 dto = (ProductListDto) Mapper.Map, List>((List)model); 之类的代码中出现错误

标签: automapper


【解决方案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));
        }
    }
}

【讨论】:

  • 你的代码就像一个魅力!非常感谢,Mightymuke。
  • @user1219702 ..然而,由于某种原因,它不是公认的答案? :s
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 2019-04-06
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多