AutoMapper and DTOs

AutoMapper and DTOs

If you are working on a solution that involves moving incompatible data between client and server, and You need to map objects between two different formats, we recommend using a small and nice object mapper utility called AutoMapper.

We found it very useful, especially in our scenario that includes passing data transfer objects (DTOs) between Web services.

Object to object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as we followed the established convention, almost zero configuration is needed to map two types.

Suppose that you have the following classes: User and UserDTO. UserDTO is a DTO we are using to send user data to the client.  Here is an example of our classes and a simple. manual approach to this task (without AutoMapper):

public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
 
    public class UserDTO
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
 
        public static UserDTO Create(User user)
        {
            var userDTO = new UserDTO();
            userDTO.FirstName = user.FirstName;
            userDTO.LastName = user.LastName;
            userDTO.Email = user.Email;
            return userDTO;
        }
    }

DTO creation and initialization is done in the UserDTO.Create() method. Here is the code that creates our user object and instantiates an user DTO from the UserDTO class.

User user = new User();
user.FirstName = "Michael";
user.LastName = "Johnson"; user.Email = "[email protected]"; UserDTO userDTO = UserDTO.Create(user);
Now let’s see how AutoMapper works. You will need to download the latest version and add its DLL  to the project references. All we need to to do is to define mappings and use appropriate names of class members so auto mapping can kick in. We need to modify the UserDTO class and remove its Create() method. Now our code looks like this.

public class UserDTO
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

User user = new User();
user.FirstName = "Michael";
user.LastName = "Johnson";
user.Email = "[email protected]";
 
Mapper.CreateMap();
UserDTO userDTO = Mapper.Map(user);
Pay attention to DTO member names – we choose the names so that AutoMapper;s convention-based matching algorithm can match the property names between objects.

Troubleshooting

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
If you recive this error (as we did), there is an easy fix. Unblock the dll (right click on the AutoMapper.dll > select properties > Unblock.), and then just recycle the site's app pool.

Posted by Ingenium Web

Ingenium Web

iNGENIUM Ltd. is an software development company from EU which delivers a full range of custom .NET, web and mobile solutions for different business to meet partner's demand.

The Power of Imagination Makes Us Infinite

Related Posts

Comments

comments powered by Disqus