Automapper: Auto map collection property for a dto object

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 {     public List Products;      public ProductListDto()     {         Products = new List();     } } 

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();  /* dto = (ProductListDto) Mapper.Map, List>((List)model);  this code line causes error. It is commented out. */   dto.Products = Mapper.Map, List>((List)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();              Mapper.CreateMap, ProductListDto>()                 .ForMember(dest => dest.Products,                            opt => opt.MapFrom(                                src => Mapper.Map,                                                  List>(src)));         }     } } 

Then in your code you can do:

dto = Mapper.Map, ProductListDto>((List)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());             Mapper.AssertConfigurationIsValid();         }          [Test]         public void AutoMapper_DriverMapping_IsValid()         {             Mapper.Initialize(m => m.AddProfile());             Mapper.AssertConfigurationIsValid();              var products = new List                 {                     new ProductModel                         {                             Id = 1,                             Name = "StackOverflow Rocks",                             SerialNumber = "1234"                         },                     new ProductModel                         {                             Id = 2,                             Name = "I Also Rock",                             SerialNumber = "4321"                         }                 };              var productsDto =                     Mapper.Map, ProductListDto>(products);              Assert.That(productsDto, Is.Not.Null);             Assert.That(productsDto.Products, Is.Not.Null);             Assert.That(productsDto.Products.Count, Is.EqualTo(2));         }     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!