Click here to Skip to main content
Click here to Skip to main content
Go to top

Generic Mapper

, 28 Feb 2010
Rate this:
Please Sign up or sign in to vote.
This article show an easy and reusable solution to create a generic mapper between two entity objects.

Introduction

There are a lot of cases where I need to define some sort of a map that I would later want to modify. On my last project, I was working in domain driven development, and I had the necessity to have a mapper class to map my entity to the POCO objects that were exposed through WCF, so I thought about making this simple solution that can make any class mappable to another type even if you just need to map some object's properties to another.

Using the code

First of all, we need to implement a couple of classes that we are going to use in this article:

public class FirstType : GenericMapper<secondtype>
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
}

public class SecondType
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

Now, suppose we want to be able to map FirstType to the second type, and we want to define the mapping using a "friendly" syntax to make the code readable and easy to understand. The solution is the mapper shown below:

Public delegate void MappingRules<d>(D To);
public abstract class GenericMapper<d> where D : new()
{
    private D toObj;

    public List<MappingRules<D>> MappingRule = 
           new List<MappingRules<D>>();

    public void SetMapping(params MappingRules<d>[] mappingList)
    {
         MappingRule = mappingList.ToList();
    }

    public D GetMappedObject()
    {
        toObj = (D)Activator.CreateInstance(typeof(D)); 
        foreach (var action in MappingRule)
        {
            action.Invoke(toObj);
        }
        return toObj;
    }
}

I make the mapper class abstract so that it can be used just as the base type. I also defined a generic delegate that will be used to store all the mapping rules. The core of the mapper is the GetMappedObject() that returns the object we want to be mapped with all the properties already initialized.

It is easier to understand this with an example. In this case, I want to map FirstType to SecondType, so I have to make the FirstType be able to be mapped to SecondType. I can do this easily by making FirstType extended from the mapper class. The mapper class is generic so we have to specify the type we want to be returned, in this case, SecondType.

public class FirstType : GenericMapper<secondtype>
{
    public string FirstName { get; set; }
    public string Surname { get; set; }

    public FirstType()
    {
        this.SetMapping(x => x.Surname = this.Surname,
                        x => x.Name = this.FirstName);
    }
}

Now all we need is to define a constructor where we specify the mapping between the object using a Lambda Expression: that makes the mapping easy to understand and very readable.

Now, we just need to call GetMappedObjecton of FirstType to have SecondType object already initialized, as we have defined in the constructor.

static void Main(string[] args)
{
    FirstType f = new FirstType();
    f.FirstName = "Peluso";
    f.Surname = "Massimiliano";
    // result is gonna be a secondType object already
    // initialized as we defined into the FirstType contructor.
    SecondType result = f.GetMappedObject();
    Console.WriteLine(result.Name);
    Console.WriteLine(result.Surname);
}

As shown in this article, this mapper is very flexible, and it can be used wherever we need a mapper between entity objects.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Massimiliano Peluso "WeDev Limited"
Architect PeluSoft Limited
United Kingdom United Kingdom
I have been fascinated by software development since I was 10 years old. I'm proud of learned the first programming language with an Olivetti PC 128S based on Microsoft BASIC. I'm a Software Architect/Senior Developer with a strong background of all the developing Microsoft's technologies.

Comments and Discussions

 
GeneralGeneric Type Mapping PinmemberDave Sexton18-Dec-10 13:03 
GeneralMy vote of 5 Pinmembersteliodibello8-Dec-10 11:41 
GeneralMy vote of 1 PinmemberYang Yu1-Mar-10 3:43 
GeneralMy vote of 1 PinmemberYang Yu1-Mar-10 3:42 
GeneralRe: My vote of 1 PinmemberMassimiliano Peluso "PeluSoft Limited"1-Mar-10 8:43 
GeneralRe: My vote of 1 PinmemberYang Yu1-Mar-10 9:22 
GeneralRe: My vote of 1 [modified] PinmemberMassimiliano Peluso "PeluSoft Limited"3-Mar-10 23:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 1 Mar 2010
Article Copyright 2010 by Massimiliano Peluso "WeDev Limited"
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid