AutoMapper:最大深度图 [英] AutoMapper: Max graph depth
问题描述
我有以下的实体这是与名单连接:
I have the following entities which are connected with lists:
国家 - >地区 - >市 - >街道
Country -> Region -> Municipality -> Street
当我映射一个国家反对我的DTO,AutoMapper自动项目我的整个图形,下至街道一路,这是一个伟大的默认值。
在一个特定的用例,我想只映射根对象(国家)及其直接子(地区)。 。然后,这些地区应该有连接各市(或空)的空列表
When I map a Country object to my DTO, AutoMapper automatically projects my entire graph, all the way down to Streets, which is a great default. In a specific usecase I would like to map just the root object (Country) and its immediate children (Regions). These regions should then have en empty list of Municipalities (or null).
要实现这一目标的一个方法是创建一个映射是这样的:
One way to achieve this is to create a map like this:
Mapper.CreateMap<Data.Country, Dto.Country>();
Mapper.CreateMap<Data.Region, Dto.Region>()
.ForMember(dest => dest.Municipalities, opt => opt.Ignore())
这将意味着投影区域作为根对象时,它的市政名单将被忽略。针对此解决方法是为每一个可能的根对象创建单独的ConfigurationStore对象,但是这会导致很多不同的ConfigurationStores的。有没有办法告诉AutoMapper只下跌映射到对象图有一定深度?
This would mean that when projecting a Region as the root object, its list of Municipalities will be ignored. A workaround for this is to create separate ConfigurationStore objects for each possible root object, but that would result in a lot of different ConfigurationStores. Is there any way to tell AutoMapper to only map down to a certain depth in the object graph?
推荐答案
是的,你可以定义地图具体MAXDEPTH这样的:
Yes you can define map specific MaxDepth like:
Mapper.CreateMap<Source, Destination>().MaxDepth(1);
更多信息/ AutoMapper /维基相对=nofollow> https://github.com/AutoMapper/AutoMapper/wiki
More info: https://github.com/AutoMapper/AutoMapper/wiki
这篇关于AutoMapper:最大深度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!