Flattening object with AutoMapper

I blogged about basic concept of AutoMapper before. AutoMapper supports flattening complex object model into DTO or other simple object model. For example, domain model usually has complex object model with many associations between them, but view model has flat object model. We can map domain object model to view object model with AutoMapper’s flattening. If you follow the naming convention of your destination object model, then you don’t need to provide any configuration code to AutoMapper. AutoMapper work with convention based and map your object model from complex to flat/simple one.

The convention is, if you has same name property then it will map that automatically. If your source object has some association with other object, then destination object should have that class name and property name in Pascal case name. If your object has a method with Get prefix, then your destination object should have a property with the name excluding Get. If you follow this convention then AutoMapper will automatically map your objects. If you do not follow this convention then you need to configure AutoMapper with fluent API.

For example, I have a source object model Student. This has an association with Faculty object.

class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Faculty Faculty { get; set; }
}

class Faculty
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }

    public string GetFullName()
    {
        return FirstName + " " + LastName;
    }
}

Now I have a destination object with flat structure. I want to map from source object model to destination object model with AutoMapper.

class StuDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FacultyFullName { get; set; }
    public string FacultyPhoneNumber { get; set; }
}

I am following the convention of AutoMapper, so I don’t need to provide further configuration. Just I need to specify my source and destination object to AutoMapper.

Mapper.CreateMap<Student, StuDto>();

Now I have data in my source object model to map in to destination object model with AutoMapper.

var student = new Student
{
    Id = 1,
    Name = "stu1",
    Faculty = new Faculty
    {
        Id = 1,
        FirstName = "jon",
        LastName = "doe",
        PhoneNumber = "9830654123"
    }
};
var stuDto = Mapper.Map(student);

AutoMapper successfully map our source model to destination model.

Console.WriteLine(stuDto.Id);                 // Output: 1
Console.WriteLine(stuDto.Name);               // Output: stu1
Console.WriteLine(stuDto.FacultyFullName);    // Output: jon doe
Console.WriteLine(stuDto.FacultyPhoneNumber); // Output: 9830654123

You can see that, same name properties are mapped automatically. Faculty full name and phone number is mapped also because of the convention of associate class name and then property name. So inside Student there is a Faculty object and that has a GetFullName() method, it is mapped to FacultyFullName property. In same way PhoneNumber property inside Faculty object is mapped to FacultyPhoneNumber destination property.

Source (Student) Destination (StuDto)
Id Id
Name Name
Faculty –> GetFullName() FacultyFullName
Faculty –> PhoneNumber FacultyPhoneNumber

Summary

AutoMapper can gear towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s