Introduction
We all know that by using AutoMapper, we can reduce the pain of assigning values to one object from another object manually everywhere in our project. Automapper helps in auto assigning the values from one object to another object.
Here are three standard things to follow while defining the profiles:
- Never use .ForAllOtherMembers(e=>e.Ignore());
If we use this, it will override all the members that are not mentioned in the mapping profiles. There is a risk of losing any property while getting mapped from the domain object to another object. If we don't use this in our mapping configuration, then any missed properties in the model will be known during the run time of the solution (an Invalid Configuration error will be thrown) - If the property name is identical in both the objects, then there is no need to mention it explicitly in the mapper configurations.
- If the property name is different in both the objects, then you need to mention this property mapping in the configuration.
- If you don’t require any property to be not mapped, then mention the property as ignore.
Example
Here, I have two domain models, Customer and Country.
- using System;
- namespace AutMapper.Models {
- public class Customer {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string CountryId {
- get;
- set;
- }
- public DateTime JoiningDate {
- get;
- set;
- }
- public bool PrimeUser {
- get;
- set;
- }
- public Country Country {
- get;
- set;
- }
- }
- }
- namespace AutMapper.Models {
- public class Country {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- }
- }
I have my dto model for customer which I will be using this model as return type of my API.
- using System;
- namespace AutMapper.Models {
- public class CustomerDto {
- public int Id {
- get;
- set;
- }
- public string CustomerName {
- get;
- set;
- }
- public string CountryId {
- get;
- set;
- }
- public DateTime JoiningDate {
- get;
- set;
- }
- public bool PrimeUser {
- get;
- set;
- }
- }
- }
Now I need to map the data from customer model to CustomerDto model. Lets do the mapping by using the above standards.
- using AutoMapper;
- namespace AutMapper.Models {
- public class CustomerMappingProfiles: Profile {
- public CustomerMappingProfiles() {
- CreateMap < Customer, CustomerDto > ()
-
- .ForMember(e => e.CustomerName, src => src.MapFrom(e => e.Name))
-
- .ForMember(e => e.Country, src => src.MapFrom(e => e.Country.Name))
-
- .ForMember(e => e.Id, e => e.Ignore());
- }
- }
- }
CustomerName
Here, the property name is different in both the models, hence we need to mention the configuration explicitly.
Id
I don’t want the Id property in my model to be exposed. Hence, I'm using e.Ignore() to restrict the data from the domain object to my dto object.
Country
I want to get the country name from the countries domain model by using CountryId from the customer domain model. Hence, I am mapping the property using the domain model of the country.
Note
Always call the mapper in the Iquerable state rather than a list because the related tables data won’t be mapped in the list state.
Now for other properties I haven’t mapped, they will be automatically mapped as the property name is identical in both the models. Let’s suppose if you use the ForAllOtherMembers(e=>e.Ignore()); then the properties which are not mentioned above will not be mapped. For a model with 5 to 10 properties, it is easy to find, but in real scenarios we may have a model with above 20 properties, hence following these standards will help us overcome these challenges.