简体   繁体   English

Automapper - 同时展平列表

[英]Automapper - Flatten with list at the same time

I have a view model that I've received from the client which looks something like this 我有一个我从客户端收到的视图模型,看起来像这样

public class OrderViewModel
{
     public string Name{get;set;}
     public string ContactDetails {get;set;}
     public List<FunkyThing_ViewModel> {get;set;}
}

public class FunkyThing_ViewModel
{
    public string ThingName{get;set;}
    public string Colour{get;set;}
    public string Size{get;set;}
}

I wish to map this to a list of domain models where each which looks something more like this: 我希望将其映射到域模型列表,其中每个模型看起来更像这样:

public class Order
{
   public string Name{get;set;}
   public string ContactDetails {get;set;}
   public string ThingName{get;set;}
   public string Colour{get;set;}
   public string Size{get;set;}
}

So I'm wanting do end up with something that looks like this: 所以我想要最终看到这样的东西:

List<Order> orders = new Orders();
Mapper.CreateMap<OrderViewModel, List<Order>>();
//Something in here to ensure each funky thing creates an additional order....

Mapper.Map(viewModel, orders);
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using NUnit.Framework;
using SharpTestsEx;

namespace StackOverflowExample.Automapper
{
    public class OrderViewModel
    {
        public string Name { get; set; }
        public string ContactDetails { get; set; }
        public List<FunkyThingViewModel> FunkyThingViewModels { get; set; }
    }

    public class FunkyThingViewModel
    {
        public string ThingName { get; set; }
        public string Colour { get; set; }
        public string Size { get; set; }
    }

    public class Order
    {
        public string Name { get; set; }
        public string ContactDetails { get; set; }
        public string ThingName { get; set; }
        public string Colour { get; set; }
        public string Size { get; set; }
    }

    [TestFixture]
    public class FlattenWithListTests
    {
        [Test]
        public void FlattenListTest()
        {
            //arrange
            var source = new OrderViewModel
                {
                    Name = "name",
                    ContactDetails = "contact",
                    FunkyThingViewModels = new List<FunkyThingViewModel>
                        {
                            new FunkyThingViewModel {Colour = "red"},
                            new FunkyThingViewModel {Colour = "blue"}
                        }
                };

            Mapper.CreateMap<FunkyThingViewModel, Order>();
            Mapper.CreateMap<OrderViewModel, Order>();
            Mapper.CreateMap<OrderViewModel, List<Order>>()
                  .ConvertUsing(om => om.FunkyThingViewModels.Select(
                      ftvm =>
                          {
                              var order = Mapper.Map<Order>(om);
                              Mapper.Map(ftvm, order);
                              return order;
                          }).ToList());

            //act
            var mapped = Mapper.Map<List<Order>>(source);

            //assert
            mapped[0].Satisfy(m =>
                              m.Name == source.Name &&
                              m.ContactDetails == source.ContactDetails &&
                              m.Colour == "red");
            mapped[1].Satisfy(m =>
                              m.Name == source.Name &&
                              m.ContactDetails == source.ContactDetails &&
                              m.Colour == "blue");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM