Is is possible with Automapper to setup a convention so that maps do not have to be created by hand for situations where the entity you are mapping to just has say "ViewModel" appended.
As an example I would rather not have to setup the following map:
Mapper.CreateMap<Error, ErrorViewModel>();
I understand if projection is required that I would need to create a custom map, but having a convention to create maps would be nice.
You would need to use Mapper.DynamicMap<TDest>(source)
to map.
As you can see in the example below, it automatically maps the matching properties from source to destination.
using AutoMapper;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var source = new Foo {Value = "Abc"};
var destination = Mapper.DynamicMap<FooViewModel>(source);
Debug.Assert(source.Value == destination.Value);
}
}
public class Foo
{
public string Value { get; set; }
}
public class FooViewModel
{
public string Value { get; set; }
}
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