Wednesday, March 06, 2013

The joy of Auto-Mapper

Auto-Mapper solves a common problem - its mission is to make code that's used to map one object to another, obsolete. Its own website summarises the vision of the product adequately:

"AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?"

http://automapper.org/

I decided to throw together a really quick example.

First, I defined two identically built, but differently named, classes:

public class AltPlanA
    {
        public string Name { get; set; }
        public int FirstRowNumber { get; set; }
        public int LastRowNumber { get; set; }
        public string FirstRowLetter { get; set; }
        public string LastRowLetter { get; set; }
    }

    public class AltPlanB
    {
        public string Name { get; set; }
        public int FirstRowNumber { get; set; }
        public int LastRowNumber { get; set; }
        public string FirstRowLetter { get; set; }
        public string LastRowLetter { get; set; }
    }


Then, in a separate class, I wrote some mapping logic:

    public class AutoMapEx
    {
        public AutoMapEx()
        {

        }

        public AltPlanB DoMapping()
        {
            // Create the map
            Mapper.CreateMap();

            // Define source (this might, for example, come back from a DB)
            AltPlanA planA = new AltPlanA()
            {
                FirstRowLetter = "A",
                FirstRowNumber = 1,
                LastRowLetter = "Z",
                LastRowNumber = 10,
                Name = "My Plan"
            };

            // Copy to another object
            AltPlanB planB = Mapper.Map(planA);
            return planB;
        }
    }

I wanted to see the result of my mapping - to ensure it was working correctly - in a console window. To facilitate this, I decided to override the ToString() method in my AltPlanB class, like so:

  public override string ToString()
  {
   return "Name : " + Name + "\nFirstRowLetter: " + FirstRowLetter +
    "\nFirstRowNumber: " + FirstRowNumber.ToString()
    + "\nLastRowLetter: " + LastRowLetter
    + "\nLastRowNumber: " + LastRowNumber;
  }

My new AltPlanB class looked like this:

    public class AltPlanB
    {
        public string Name { get; set; }
        public int FirstRowNumber { get; set; }
        public int LastRowNumber { get; set; }
        public string FirstRowLetter { get; set; }
        public string LastRowLetter { get; set; }


        public override string ToString()
        {
            return "Name : " + Name + "\nFirstRowLetter: " + FirstRowLetter +
                "\nFirstRowNumber: " + FirstRowNumber.ToString()
                + "\nLastRowLetter: " + LastRowLetter
                + "\nLastRowNumber: " + LastRowNumber;
        }
    }

Then I wrote some simple calling code on the Console side:

        static void CopyObjectsUsingAutoMapper()
        {
            AutoMapEx mapper = new AutoMapEx();
            AltPlanB bPlan = mapper.DoMapping();
            Console.WriteLine(bPlan.ToString());            
        }

Then I called this from my Main method:

        static void Main(string[] args)
        {            
            CopyObjectsUsingAutoMapper();
            Console.ReadLine();
        }

Running the application produced the following output:


Going Further

Above is quite a straightforward example, but Auto-Mapper can go much further than that, for example objects do not necessarily have to be identical in order to map. By default, AM matches like for like (name and variable type) to do its mapping, but there are other possibilities too. See https://github.com/AutoMapper/AutoMapper/wiki to read up on what those possibilities are.

No comments:

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Z