Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C# 5.0
Tip/Trick

C# Custom Mapper

Rate me:
Please Sign up or sign in to vote.
4.74/5 (10 votes)
5 Jun 2014CPOL 39.8K   15   5
This is a custom mapper which can be used to map two objects instead of automapper

Introduction

This is a custom C# mapper which can be used to map two objects. As an example, you can use this to map DomainModel object to DTO object. You have to do some couple of extra works, but you'll get more benefits rather than AutoMapper because you can utilize same mapping loop to modify properties via callback function.

Background

I used this custom mapper to overcome .NET auto mapper performance issue.

Using the Code

As an example, you have User model class and UserDto class, like this:

C#
public class User
{
   public string Name {get;set;}
   public string Address {get;set;}
}

public class UserDto
{
   public string UserName {get;set;}
   public string UserAddress {get;set;}
}

This is my MapperBase class.

C#
public abstract class MapperBase<TFirst, TSecond>
{
        public abstract TFirst Map(TSecond element);
        public abstract TSecond Map(TFirst element);

        public List<TFirst> Map(List<TSecond> elements, Action<TFirst> callback = null)
        {
            var objectCollection = new List<TFirst>();
            if (elements != null)
            {
                foreach (TSecond element in elements)
                {
                    TFirst newObject = Map(element);
                    if (newObject != null)
                    {
                        if (callback != null)
                            callback(newObject);
                        objectCollection.Add(newObject);
                    }
                }
            }
            return objectCollection;
        }

        public List<TSecond> Map(List<TFirst> elements, Action<TSecond> callback = null)
        {
            var objectCollection = new List<TSecond>();

            if (elements != null)
            {
                foreach (TFirst element in elements)
                {
                    TSecond newObject = Map(element);
                    if (newObject != null)
                    {
                        if (callback != null)
                            callback(newObject);
                        objectCollection.Add(newObject); 
                    }
                }
            }
            return objectCollection;
        }
  }

Here, I am going to map User to UserDto, so you have to implement UserMapper class as below:

C#
public sealed class UserMapper:MapperBase<User, UserDto>
{
       public override User Map(UserDto element)
        {
            return new User
                       {
                           Name = element.UserName,
                           Address = element.Address
                       };
        }

        public override UserDto Map(User element)
        {
            return new UserDto
                       {
                           UserName = element.Name,
                           UserAddress = element.Address
                       };
        }
}

MapperBase<User, UserDto> userMapper = new UserMapper();

The below example code illustrates how we can use mapper callback function usages.

C#
List<User> users = new List<User>();
List<UserDto> userDtos = userMapper.Map(users, delegate(UserDto u)
{
        //Update UserDto object here

});

As mentioned above, you can use the same loop to modify the target object. I've used anonymous method here and if you want, you can have a separate method and can pass it as action.

Now, you can use userMapper object to map any User or UserDto object(s) to each other.

Points of Interest

If you have any ideas to enhance this, please share them.

History

  • 6th June, 2014: Initial version

License

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


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalcomment Pin
Dmytro Danko11-Jan-24 20:06
Dmytro Danko11-Jan-24 20:06 
PraiseNice Mapper Pin
Júnior Pacheco18-Feb-19 5:48
professionalJúnior Pacheco18-Feb-19 5:48 
SuggestionPerformance Benchmarks Pin
Member 78219436-Jun-14 4:18
Member 78219436-Jun-14 4:18 
GeneralRe: Performance Benchmarks Pin
mrchief_200019-Sep-14 6:51
mrchief_200019-Sep-14 6:51 
QuestionGreat! Pin
Volynsky Alex5-Jun-14 22:36
professionalVolynsky Alex5-Jun-14 22:36 

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.