简体   繁体   English

自动映射器将ID列表映射到多对多场景中的对象

[英]Automapper mapping a list of ids to objects in many-to-many scenario

I'm looking for the best way to handle this with Automapper 我正在寻找使用Automapper处理此问题的最佳方法

My simplified domain model (all IDs are auto generated by the DB): 我的简化域模型(所有ID由数据库自动生成):

public class Product
{
    public long Id { get; set; }
    public List<OrderProduct> OrderProducts { get; set; }
}

public class Order
{
    public long Id { get; set; }
    public List<OrderProduct> OrderProducts { get; set; }
}

public class OrderProduct
{
    public long Id { get; set; }
    public long ProductId { get; set; }
    public long OrderId { get; set; }
    public Product Product { get; set; }
    public Order Order { get; set; }
}

My DTO object: 我的DTO对象:

public class CreateOrderDTO
    {
        public long Id { get; set; }
        public List<long> ProductIds { get; set; }
    }

Now, I need to map from CreateOrderDTO to Order object. 现在,我需要从CreateOrderDTO映射到Order对象。 What is the best way to achieve this using automapper? 使用自动映射器实现此目标的最佳方法是什么? I can map it using plain C# - however would really like to know the proper way to do this using Automapper. 我可以使用普通的C#映射它-但是,我真的很想知道使用Automapper进行此操作的正确方法。

Assuming that you already installed the Automapper. 假设您已经安装了Automapper。

Here's one way of configuring your mapping. 这是配置映射的一种方法。

You create your mapping profile, you should inherit from the Profile object, then create your mapping in your profile's contructor: 创建映射概要文件后,应从Profile对象继承,然后在概要文件的构造函数中创建映射:

public class OrderProfile : Profile
    {
        public OrderProfile()
        {
            CreateMap<CreateOrderDTO, Order>().ForMember(c => c.OrderProducts, m => m.MapFrom(l => CreateOrderProducts(l.ProductIds)));
        }

        private static List<OrderProduct> CreateOrderProducts(IList<long> productIds)
        {
            IList<OrderProduct> orderProducts = new List<OrderProduct>();

            foreach (long id in productIds)
            {
                orderProducts.Add(new OrderProduct
                {
                    ProductId = id
                });
            }

            return orderProducts.ToList();
        }
    }

Note: I feel that you need to manually map your ProductIds List<long> to List<OrderProduct> and you should do that in your profile as well. 注意:我觉得您需要手动将ProductIds List<long>映射到List<OrderProduct>并且也应该在个人资料中进行此操作。

Next, you configure a AutoMapper: 接下来,配置一个AutoMapper:

 public static class MappingConfig
    {
        public static MapperConfiguration Configure()
        {
            MapperConfiguration config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile<OrderProfile>();
            });

            return config;
        }
    }

This is one way of doing the configuration (ie adding your profile), thru the constructor of the MapperConfiguration, you can also do this with static Mapper instance http://docs.automapper.org/en/stable/Configuration.html . 这是通过MapperConfiguration的构造函数进行配置(即添加配置文件)的一种方法,您也可以使用静态Mapper实例http://docs.automapper.org/en/stable/Configuration.html进行此配置。

Usage: 用法:

You can then create an instance of the mapper, preferably, you should only have one instance of mapper in your app, so it's always a good idea to register your mapper in your ioc container. 然后,您可以创建一个映射器实例,最好在您的应用程序中只包含一个映射器实例,因此在ioc容器中注册映射器始终是一个好主意。

static void Main(string[] args)
        {
            var mapper = MappingConfig.Configure().CreateMapper();

            CreateOrderDTO dto = new CreateOrderDTO
            {
                Id = 1,
                ProductIds = new List<long> { 23L }
            };

            // Here it appears that it's as if you didn't do any manual mapping.
        Order order = mapper.Map<CreateOrderDTO, Order>(dto);
            Order order = mapper.Map<CreateOrderDTO, Order>(dto);

            Console.WriteLine("Order Id: " + order.Id);
        Console.WriteLine("Product Id: " + order.OrderProducts.Select(o => o.ProductId).First());
        Console.ReadLine();
        }

在此处输入图片说明

If find this unhelpful, you can vote it down, and I'll remove it as I want the readers not to be confused of the right ways of doing things. 如果发现这无济于事,可以将其否决,并将其删除,因为我希望读者不要对正确的做事方式感到困惑。

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

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