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
There has been a major technological revolution in recent years, and now almost all of our sectors are driven by computers and technology.
A website is one of the most significant assets a business can have in this digital era.
Suppose you are a Java developer and want to expand your skills on learning more programming languages. The reason can be anything to take a new job, get a promotion, or do side projects.
Whether you are an absolute beginner or an experienced programmer, learning Python is one the best decisions you could make in this age of Artificial Intelligence. And the best part? It’s fairly easy to learn the language and create amazing things with it.
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.
Comments
comments powered by Disqus