使用自动映射器从两个项目进行映射 [英] Mapping from two items with automapper

查看:77
本文介绍了使用自动映射器从两个项目进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据层中,我有一个存储库,可以返回类似以下项的列表:

In my data layer I have a repository that can return a list of items like this:

new List<Item> {
    new Item { Title = "Something", DetailId = 500 },
    new Item { Title = "Whatever", DetailId = 501 },
}

存储库还有另一种方法,当给定项目的详细信息ID时,可以返回这些项目的详细信息:

The repository has another method that can return details for these items when given the items detail ID:

// repository.GetDetail(500) would return something like:
new ItemDetail {
    Id = 500,
    Size = "Large"        
}

现在,在我的服务层中,我想将上面的列表映射为这样的内容:

Now, in my service layer I would like to map a the above list into something like this:

new List<ServiceItem> {
    new ServiceItem { Title = "Something", Size = "Large" },
    new ServiceItem { Title = "Whatever", Size = "Medium" },
}

请注意,我需要列表中的对象(在这种情况下为Title)和详细对象的属性.有没有什么好方法可以通过AutoMapper映射它?

Note that I need properties both from the objects in the list (Title in this case) and from the detailed objects. Is there any good way to map this with AutoMapper?

我曾经考虑过要根据我的存储库实例创建一个配置文件,然后让AutoMapper进行详细请求,但是让AutoMapper提取新数据有点混乱吗?

I have thought about making a profile that depends on an instance of my repository, and then have AutoMapper do the detail requests, but it sort of feels messy to have AutoMapper fetch new data?

是否有一种干净的方法将两个对象映射为一个?

Is there a clean way to map from two objects into one?

推荐答案

一种可能性是将Item和ItemDetail对象包装在Tuple中,然后根据该Tuple定义Map.映射代码如下所示.

One possibility would be to wrap your Item and ItemDetail objects in a Tuple and define your Map based off that Tuple. The mapping code would look something like this.

Mapper.CreateMap<Tuple<Item, ItemDetail>, ServiceItem>()
    .ForMember(d => d.Title, opt => opt.MapFrom(s => s.Item1.Title))
    .ForMember(d => d.Size, opt => opt.MapFrom(s => s.Item2.Size));

您必须自己加入正确的Item和ItemDetail.如果希望Automapper也将Item与正确的ItemDetail匹配,则需要更改映射,以便在Size解析期间,Automapper进行查找并返回匹配的Size.代码看起来像这样.

You would have to join the correct Item and ItemDetail yourself. If you wanted Automapper to also match the Item to the correct ItemDetail, you would need to change the mapping so that during the resolution of Size, Automapper performs a lookup and returns the Size of the match. The code would look something like this.

Mapper.CreateMap<Item, ServiceItem>()
    .ForMember(d => d.Size, opt => opt.MapFrom(s => itemDetails.Where(x => s.DetailId == x.Id).First().Size));

此映射使用Automapper的自动映射功能将Item.Title映射到ServiceItem.Title.该映射使用一个名为itemDetails的IEnumerable来查找匹配项,但是应该可以将其替换为数据库调用.现在,由于我不认为Autmapper设计要进行这种类型的查找,因此对于大量项目而言性能可能不那么好.那将是要测试的东西.

This map uses the automapping functionality of Automapper to map Item.Title to ServiceItem.Title. The map is using an IEnumerable named itemDetails to find the match, but it should be possible to replace it with a database call. Now since doing this type of lookup is something that I don't think Autmapper was designed to do, performance may not be that great for a large number of items. That would be something to test.

这篇关于使用自动映射器从两个项目进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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