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.
What is AutoMapper? AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.
When you call CreateMap, AutoMapper uses optimizers to build the code for getting/setting values on source/destination types. Currently, it uses a combination of Reflection. Emit and expression tree compilation. At Map time, it uses the optimized callbacks for interacting with types.
You can define map specific MaxDepth
like:
Mapper.CreateMap<Source, Destination>().MaxDepth(1);
More info: https://github.com/AutoMapper/AutoMapper/wiki
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With