Click here to Skip to main content
12,821,315 members (34,355 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 27 Feb 2010

Generic Mapper

, 28 Feb 2010 CPOL
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.


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)
        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();

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


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


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.

You may also be interested in...

Comments and Discussions

GeneralGeneric Type Mapping Pin
Dave Sexton18-Dec-10 14:03
memberDave Sexton18-Dec-10 14:03 
GeneralMy vote of 5 Pin
steliodibello8-Dec-10 12:41
membersteliodibello8-Dec-10 12:41 
GeneralMy vote of 1 Pin
Yang Yu1-Mar-10 4:43
memberYang Yu1-Mar-10 4:43 
GeneralMy vote of 1 Pin
Yang Yu1-Mar-10 4:42
memberYang Yu1-Mar-10 4:42 
GeneralRe: My vote of 1 Pin
Massimiliano Peluso "PeluSoft Limited"1-Mar-10 9:43
memberMassimiliano Peluso "PeluSoft Limited"1-Mar-10 9:43 
GeneralRe: My vote of 1 Pin
Yang Yu1-Mar-10 10:22
memberYang Yu1-Mar-10 10:22 
GeneralRe: My vote of 1 [modified] Pin
Massimiliano Peluso "PeluSoft Limited"4-Mar-10 0:01
memberMassimiliano Peluso "PeluSoft Limited"4-Mar-10 0:01 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 1 Mar 2010
Article Copyright 2010 by Massimiliano Peluso "WeDev Limited"
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid