自动映射器将ID列表映射到多对多场景中的对象 [英] Automapper mapping a list of ids to objects in many-to-many scenario

查看:166
本文介绍了自动映射器将ID列表映射到多对多场景中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用Automapper处理此问题的最佳方法

I'm looking for the best way to handle this with Automapper

我的简化域模型(所有ID由数据库自动生成):

My simplified domain model (all IDs are auto generated by the DB):

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; }
}

我的DTO对象:

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

现在,我需要从CreateOrderDTO映射到Order对象.使用自动映射器实现此目标的最佳方法是什么?我可以使用普通的C#映射它-但是,我真的很想知道使用Automapper进行此操作的正确方法.

Now, I need to map from CreateOrderDTO to Order object. 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.

推荐答案

假定您已经安装了Automapper.

Assuming that you already installed the Automapper.

这是配置映射的一种方法.

Here's one way of configuring your mapping.

您创建映射配置文件,应该从Profile对象继承,然后在配置文件的构造器中创建映射:

You create your mapping profile, you should inherit from the Profile object, then create your mapping in your profile's contructor:

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();
        }
    }

注意:我认为您需要手动将ProductIds List<long>映射到List<OrderProduct>,并且您也应该在个人资料中进行此操作.

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.

接下来,您配置一个AutoMapper:

Next, you configure a AutoMapper:

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

            return config;
        }
    }

这是通过MapperConfiguration的构造函数进行配置(即添加配置文件)的一种方法,您也可以使用静态Mapper实例进行此操作

This is one way of doing the configuration (i.e. 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.

用法:

然后您可以创建一个映射器实例,最好是,您的应用程序中应该只包含一个映射器实例,因此将映射器注册到ioc容器中始终是一个好主意.

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.

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.

这篇关于自动映射器将ID列表映射到多对多场景中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆