0

Imagine classes like this:

class Source
{
    public string Name { get; set; }
    public string Code { get; set; }
}

class Destination
{
    public Destination(string name, int code)
    {}
}

I want to configure a mapping from Source to Destination. I want AutoMapper to automatically match Source.Name to Destination's name constructor parameter since they have the same name (excluding naming conventions) and type. However, the second constructor parameter will need to be custom-mapped.

The best I could find so far is to just manually do all of the mappping:

Mapper.CreateMap<Source, Destination>()
    .ConstructUsing(source => new Destination(source.Name, /*Custom map for "code" property*/));

However, by doing this, I lose the maintainability improvements that convention-based mapping offers.

Sam
  • 40,644
  • 36
  • 176
  • 219

1 Answers1

0

This doesn't exactly answer the question, but I ended up concluding that it was easier to just not use AutoMapper for scenarios like this. It's trivial to write a method that receives one object and creates an instance of another without using AutoMapper:

Destination Map(Source source)
{
    return new Destination(source.Name, source.Code);
}
Sam
  • 40,644
  • 36
  • 176
  • 219