
Automagic with Automapper
March 13, 2014

Automapper is a library that helps map one object to another. Writing code to stuff an object into another can be really boring. It’s pretty common to see something like this in a copy constructor while working with entities and dtos:
public MyEntity(MyDTO other)
{
…
this.Property1 = other.Property1;
…
}
One of the nice things about Automapper is that it will match properties by name. This came in handy when writing the data access layer for a nosql database. Automapper made it easier to write generic crud functionality that mapped entities taken from the db to a dto.
Get example in our base class:
…
/// Gets a single entity as a DTO instance.
protected TDataObject Get(Guid id)
where TTableEntity : class, ITableEntity, new()
where TDataObject : class, new()
{
return Mapper.Map(GetEntity(id));
}
…
Stay up to date with our email updates!