Using Automapper (Part 1)

Automapper is a convention-based, object-to-object mapping utility which is open-source and hosted on codeplex. In a two-part series I’m going to show how Automapper can be used not only to map objects to each other but also as an Object-Relational Mapper for mapping results pulled from a database into objects.

Part 2 is about using Automapper as an object-relational mapper (ORM).

This tool has a number of useful and time saving functions. It’s normal function proves helpful when implementing best practices around XML serialization/deserialization as well as WCF data contracts in which you want to utilize objects in your application that aren’t necessarily marked up with XML attributes, are able to be versioned in the case of WCF or in which you want to change the structure of the data model to better fit your needs for use within your application. In these scenarios you typically have two identical data model objects with properties of the same name and type, except one of the objects is marked up with XML attributes or WCF data contract attributes. Rather than create a helper method to map between these objects Automapper will figure out which properties line up based on convention (same name and type). Automapper requires a single line of code to tell it, statically, which objects should map to each other and then you simply give Automapper your source object and it returns to you a new instance of your mapped object. Simple example is provided below.

[DataContract]
public class SourceObject
{
    [DataMember]
    public int MyIntegerProperty { get; set; }

    [DataMember]
    public string MyStringProperty { get; set }
}

public class DestinationObject
{
    public int MyIntegerProperty { get; set; }

    public string MyStringProperty { get; set; }
}

public class Test
{
    public static void Main(string[] args)
    {
        Automapper.Mapper.CreateMap<SourceObject, DestinationObject>();

        SourceObject source = new SourceObject
            {
                MyIntegerProperty = 123, 
                MyStringProperty = "asdf"
            };

        // destination object contains int property with value 123 
        // and string property with value "adsf".
        DestinationObject destination =
            Automapper.Mapper.Map<DestinationObject>(source);
    }
}

For situations in which you have properties that do not match by convention, as in they are spelled differently or in the case where you wish to change the data structure, Automapper provides a way to specify which properties should be matched to other properties.

public class NewDestinationObject
{
    public int SomeIntegerProperty { get; set; }

    public string AStringProperty { get; set; }
}

public static void Main(string[] args)
{
    Automapper.Mapper.CreateMap<SourceObject, NewDestinationObject>()
        .ForMember(m => m.SomeIntegerProperty, opt => opt.MapFrom(p => p.MyIntegerProperty))
        .ForMember(m => m.AStringProperty, opt => opt.MapFrom(p => p.MyStringProperty));

        SourceObject source = new SourceObject
            {
                MyIntegerProperty = 123, 
                MyStringProperty = "asdf"
            };

        DestinationObject destination =
            Automapper.Mapper.Map<DestinationObject>(source);
}

In the above example, Automapper allows you to use Lambda expressions to specify the properties to map to and the properties they should be mapped from. Given the use of Lambda’s, you can create any number of complicated mapping schemas.

Advertisements

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