如何从一个平面实体映射到多个DTO?

时间:2019-07-08 17:02:15

标签: c# asp.net-core automapper

我希望在C#.NET Core中使用AutoMapper将已经扁平化的实体映射到一组嵌套的DTO。 DTO也具有一对多的关系,扁平实体隐藏在结构中。例如:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
    public int Weight { get; set; }
}

public class ProductDto
{
    public string Name { get; set; }
    public IEnumerable<PriceDto> Prices { get; set; }
}

public class PriceDto
{
    public int Price { get; set; }
    public int Weight { get; set; }
}

我知道AutoMapper提供的ReverseMap()函数,但是考虑到我是从一个扁平化的实体开始开始,我不确定如何设置ProductDto和PriceDto之间的关系。 / p>

编辑: 这是我以“产品”形式接收的一组扁平化数据的示例。请注意,将需要推断出ProductDto和PriceDto之间的一对多关系。

+-----+-------+-------+--------+
| Id  | Name  | Price | Weight |
+-----+-------+-------+--------+
|   1 | "foo" |     8 |     12 |
|   2 | "foo" |    12 |     18 |
|   3 | "bar" |     3 |      1 |
|   4 | "bar" |     6 |      2 |
| ... |       |       |        |
+-----+-------+-------+--------+

2 个答案:

答案 0 :(得分:0)

我将定义两个域配置文件。 一个用于产品到ProductDto

CreateMap<Product, ProductDto>()
  .ForMember(x => x.Id , opts => opts.Ignore())
  .ForMember(x => x.Price , opts => opts.Ignore())
  .ForMember(x => x.Weight , opts => opts.Ignore())
  .ForMember(x => x.Name , opts => opts.MapFrom(y => y.Name));

还有一个产品到PriceDto

CreateMap<Product, ProductDto>()
  .ForMember(x => x.Id , opts => opts.Ignore())
  .ForMember(x => x.Name , opts => opts.Ignore())
  .ForMember(x => x.Price , opts => opts.MapFrom(y => y.Price ))
  .ForMember(x => x.Weight , opts => opts.MapFrom(y => y.Weight ));

然后一次从同一源到两个不同目标进行一次映射。

答案 1 :(得分:0)

您需要实现自己的Converter,如下所示:

  1. ProductsConverter

    public class ProductsConverter : ITypeConverter<List<Product>, List<ProductDto>>
    {
        public List<ProductDto> Convert(List<Product> source, List<ProductDto> destination, ResolutionContext context)
        {
            return source.GroupBy(p => p.Name)
                        .Select(r => new ProductDto
                        {
                            Name = r.Key,
                            Prices =  source.Where(pp => pp.Name == r.Key)
                                                        .Select(rr => new PriceDto
                                                        {
                                                            Price = rr.Price,
                                                            Weight = rr.Weight
                                                        })
                        }).ToList();
        }
    }
    
  2. ModelProfile

    public class ModelProfile: Profile
    {
        public ModelProfile()
        {
            CreateMap<List<Product>, List<ProductDto>>()
                .ConvertUsing<ProductsConverter>();
        }
    }
    
  3. 用例

    public IActionResult Index()
    {
        List<Product> products= new List<Product>() {
            new Product{ Id = 1, Name = "foo", Price = 8, Weight = 12},
            new Product{ Id = 2, Name = "foo", Price = 12, Weight = 18},
            new Product{ Id = 3, Name = "bar", Price = 3, Weight = 1},
            new Product{ Id = 4, Name = "bar", Price = 6, Weight = 2},
        };
        var result = _mapper.Map<List<ProductDto>>(products);
        return Ok(result);
    }