AutoMapper and DTOs
We found it very useful, especially in our scenario that includes passing data transfer objects (DTOs) between Web services.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.
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";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.
user.LastName = "Johnson"; user.Email = "[email protected]"; UserDTO userDTO = UserDTO.Create(user);
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.CreateMapPay attention to DTO member names – we choose the names so that AutoMapper;s convention-based matching algorithm can match the property names between objects.(); UserDTO userDTO = Mapper.Map (user);
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.
Related Posts
Most of the businesses in this modern world consider that UX design is a must-have investment for them.
.NET is a platform that gives you a platform to develop many applications that to free of cost.
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.
MVC is designed so that programmers, designers and testers can work independently.
So many people want to improve their programming and coding skills but not everyone finds the proper resources to learn and practice it.
ASP.NET MVC is an open source web application framework that implements the Model-View-Controller (MVC) architectural pattern.
Comments
comments powered by Disqus