AutoMapper define mapping level

大兔子大兔子 提交于 2019-12-08 14:59:43

问题


public class Foo
{
    public string Baz { get; set; }
    public List<Bar> Bars { get; set; }
}

When I map the class above, is there any way to define how deep I want automapper to map objects? Some pseudo code of what I'm after:

var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 0 });
// result = { Baz: "" }

var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 1 });
// result = { Baz: "", Bars: [{ Blah: "" }] }

 var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 2 });
// result = { Baz: "", Bars: [{ Blah: "", Buzz: [{ Baz: "" }] }] }

// etc...

I'm currently using automapper 3.3 due to a nuget dependency.


回答1:


You can do it by providing a value at runtime as was answered in question: How to ignore a property based on a runtime condition?.

Configuration:

Mapper.CreateMap<Foo, FooDTO>().ForMember(e => e.Bars,
    o => o.Condition(c => !c.Options.Items.ContainsKey("IgnoreBars")));

Usage:

Mapper.Map<FooDTO>(foo, opts => { opts.Items["IgnoreBars"] = true; });

Same configuration approach you can apply for all your nested objects that you call levels.

If you want to achieve same behavior for your DB Entities you can use ExplicitExpansion approach as described in this answer: Is it possible to tell automapper to ignore mapping at runtime?.

Configuration:

Mapper.CreateMap<Foo, FooDTO>()
    .ForMember(e => e.Bars, o => o.ExplicitExpansion());

Usage:

dbContext.Foos.Project().To<FooDTO>(membersToExpand: d => d.Bars);



回答2:


You can define map specific MaxDepth like:

Mapper.CreateMap<Source, Destination>().MaxDepth(1);

More info: https://github.com/AutoMapper/AutoMapper/wiki



来源:https://stackoverflow.com/questions/44353062/automapper-define-mapping-level

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!