For many cases, derived type mapping is supported without configuration.
For example, with the following classes:
public class Person {}
public class Customer : Person
{
public float Discount { get; set; }
}
public class PersonViewModel {}
public class CustomerViewModel : PersonViewModel
{
public double Discount { get; set; }
}
...Customer and CustomerViewModel are auto-discovered, and paired up based on the [Type] -> [TypeViewModel] naming convention.
Mapping a customer variable of type Person to a PersonViewModel automatically maps to a CustomerViewModel instead:
var person = new Customer { Discount = 0.1f } as Person;
var viewModel = Mapper.Map(person).ToANew<PersonViewModel>();
// viewModel is of type CustomerViewModel
// viewModel.Discount is 0.1
Other type pairs are also paired up automatically - for example, with the following classes:
public class Animal {}
public class Dog : Animal {}
public class Cat : Animal {}
public class AnimalDto {}
public class DogDto : AnimalDto {}
public class CatDto : AnimalDto {}
The following mappings are performed:
var sourceAnimal = new Dog() as Animal;
var resultAnimal = Mapper.Map(sourceDog).ToANew<Animal>();
// resultAnimal is of type Dog
var sourceAnimals = new Animal[] { new Cat(), new Dog() };
var resultAnimals = Mapper.Map(sourceAnimals).ToANew<AnimalDto[]>();
// resultAnimals[0] is of type CatDto
// resultAnimals[1] is of type DogDto
In the second example above, the Dog -> DogDto and Cat -> CatDto types are paired by convention based on the names of the original
Animal -> AnimalDto pairing.
You can configure type pairs which don't have a consistent naming convention, and in which assemblies to look for derived types.