Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Custom Mapper

0.00/5 (No votes)
5 Jun 2014 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here