Skip to content

AutoMapper BoilderPlate

AutoMapper

It is a simple Object – Object mapper, through which we can transform an input object of one type into an output object of another type.

In layman’s terms when you want to convert your DTO’s (Data Transfer Objects) to Business Model or vice versa auto-mapper could be used.

Example:

//return type of our API
    public class GetUserDTO
    {
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

//our DB table type
    public class Users
    {
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

Now lets say we would want to convert Users to GetUserDTO in our service and return that to the client.

public GetUserDTO GetUIUser()
{
    GetUserDTO user = new GetUserDTO();
    //We have to store our DB user in users data type and then convert it to getUsersDto manually
    //as GetUserDTO user = GetUser(); doesnt work
    Users Dbuser = GetUser();
    user.UserID = Dbuser.UserID;
    user.FirstName = Dbuser.FirstName;
    user.LastName = Dbuser.LastName;
    return user;
}

As you could see it required lot of lines of code to do it. Lets look at auto mapper to get the same thing done.

Boiler Plate

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAutoMapper(typeof(Startup));
    }
}

public class AutoMapperProfile: Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Users, GetusersDTO>();
    }
}

public class UserService
{
    private readonly IMapper _mapper;
    public UserService(IMapper mapper)
    {
        _mapper = mapper;
    }
    public Users GetUser()
    {
        return new Users()
        {
            UserID = 1,
            FirstName = "Tejaswi",
            LastName = "Pandava",
            Age = 27
        };
    }
    public GetUserDTO GetUIUser()
    {
        GetUserDTO user = _mapper.Map<GetUserDTO>(GetUser());
        //GetUserDTO user = _mapper.Map<GetUserDTO>(usersobject);
        return user;
    }
}

It that’s simple. Now Lets say we have a complex object (Character) like below which has foreign key for CharacterSkill which intern is connected to Skill.

public class Character
{
    public int Id { get; set; }
    public List<CharacterSkill> CharacterSkills { get; set; }
}

public class CharacterSkill
{
    public Character  Character { get; set; }
    public int CharacterId { get; set; }
    public Skill Skill { get; set; }
    public int SkillId { get; set; }
}

public class Skill
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<CharacterSkill> CharacterSkills { get; set; }
}

Now you want to return a DTO which consolidates Character and Skill in that case we could do like below. Now we could access skills from character using LINQ

CreateMap<Character, GetCharacterDto>()
.ForMember(dto => dto.Skills, c => c.MapFrom(c => c.CharacterSkills.Select(cs => cs.Skill)));

in short the above statements tries to say that we have a skill table whose relationship can be found in character skills property.

Published inUncategorised

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *