AutoMapper

In programing we often need to map one object to another. For example, suppose you are working with MVC pattern and you need to map your domain model with view model. This kind of mapping code are time consuming to write and not easy to test. So why not use a tool which take the heavy lifting for us.

AutoMapper in .NET is an object oriented mapping strategy. It support fluent API and convention over configuration approach. It is geared 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.

You simply need to add AutoMapper reference in your project with NuGet package manager.

PM> Install-Package AutoMapper

It saves our time to write mapping code for our applications. If you follow the convention in your destination class AutoMapper can automatically map your object to that. It needs almost zero configuration. You can configure it if your class do not follow convention with fluent API.

If your source class and destination classes has same name properties then AutoMapper can map it.

using System;
using AutoMapper;

class Source
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Destination
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class AutoMapperDemo
{
    static void Main()
    {
        Mapper.CreateMap<source, destination>();
        Mapper.AssertConfigurationIsValid();

        var src = new Source { Id = 1, Name = "bob" };
        var dest = Mapper.Map<destination>(src);

        Console.WriteLine(dest.Id);   // Output: 1
        Console.WriteLine(dest.Name); // Output: bob
    }
}

If your source and destination classes have different property names, then you need to configure it with fluent API.

using System;
using AutoMapper;

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

class Destination
{
    public int Id { get; set; }
    public string FullName { get; set; }
}

public class Class1
{
    static void Main(string[] args)
    {
        Mapper.CreateMap<source, destination>()
              .ForMember(dst => dst.FullName, 
                  opt => opt.MapFrom(source => 
                      source.FirstName + " " + source.LastName));

        Mapper.AssertConfigurationIsValid();

        var src = new Source 
        { 
            Id = 1, 
            FirstName = "jon", 
            LastName = "doe" 
        };
        var dest = Mapper.Map<destination>(src);

        Console.WriteLine(dest.Id);       // Output: 1
        Console.WriteLine(dest.FullName); // Output: jon doe
    }
}

Testing your mapping is also very easy. Simply call the AssertConfigurationIsValid() static method of Mapper class and it will throw exception in the case of invalid mapping.

AutoMapper is a very powerful tool for your object oriented mapping requirements. For full tutorial please take a look at AutoMapper GitHub Wiki Page. You can find full code on GitHub.

If you like this post, please follow me on twitter.

Advertisements

One Reply to “AutoMapper”

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 )

Google+ photo

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

Connecting to %s