Click here to Skip to main content
Click here to Skip to main content

C# Custom Mapper

, 5 Jun 2014
Rate this:
Please Sign up or sign in to vote.
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:

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.

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:

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.

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)

About the Author

Sameera Millavithanachchi
Software Developer (Senior)
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
SuggestionPerformance Benchmarks PinmemberMember 78219436-Jun-14 4:18 
QuestionGreat! PinpremiumVolynsky Alex5-Jun-14 22:36 

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
Web04 | 2.8.140721.1 | Last Updated 6 Jun 2014
Article Copyright 2014 by Sameera Millavithanachchi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid