Wednesday, 13 December 2017

Basic of AutoMapper in C#

AutoMapper is a dll which is used to mapped one object to another.
  • No longer need to manually map objects.
  • Good to use for ViewModel/CreateModel, Request/Response entity.
  • Least used features of AutoMapper is projection. AutoMapper, when used with an Object Relational Mapper (ORM) such as Entity Framework,

How to Use?

Just Install it through Nuget Package manager and use it.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using AutoMapper;
using AutomapperDemo.Models;
 
namespace AutomapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User()
            {
 
                ID = 100,
                FirstName = "Raj",
                LastName = "Kumar",
                Address = "Delhi",
                Company = "AAPC",
                Dept = "IT",
                OfficialEmail = "blog.raj111@gmail.com",
                FatherName = "R.J.T",
                Gender = "Male",
                Maritialstatus = "Married",
                Mobile = "8888888888"
            };
 
            Mapper.Initialize(config =>
            {
 
                config.CreateMap<UserUserPersonalInfo>();//All properties are same
 
                config.CreateMap<UserUserOfficialInfo>()
                    .ForMember(d => d.UserID, m => m.MapFrom(s => s.ID));//Map different property 
 
                config.CreateMap<UserUserViewModel>()
                    .ForMember(d => d.Name, m => m.MapFrom(s => s.FirstName + " " + s.LastName))//Modify property
                    .ForMember(d => d.ID, m => m.Ignore());//Ignore Default mapping
 
            });
 
 
            var personalInfo = Mapper.Map<UserUserPersonalInfo>(user);
            var officialInfo = Mapper.Map<UserUserOfficialInfo>(user);
            var userViewModel = Mapper.Map<UserUserViewModel>(user);
 
            Read();
 
        }
    }
 
 
 
}
 
 
 
 
namespace AutomapperDemo.Models
{
    class User
    {
        public int ID { getset; }
        public string FirstName { getset; }
        public string LastName { getset; }
        public string FatherName { getset; }
        public string Gender { getset; }
        public string Maritialstatus { getset; }
        public string Mobile { getset; }
        public string OfficialEmail { getset; }
        public string Address { getset; }
        public string Company { getset; }
        public string Dept { getset; }
 
    }
 
    class UserOfficialInfo
    {
        public int UserID { getset; }
        public string OfficialEmail { getset; }
        public string Address { getset; }
        public string Company { getset; }
        public string Dept { getset; }
    }
 
 
    class UserPersonalInfo
    {
        public string FirstName { getset; }
        public string LastName { getset; }
        public string FatherName { getset; }
        public string Gender { getset; }
        public string Maritialstatus { getset; }
        public string Mobile { getset; }
    }
 
 
    class UserViewModel
    {
        public int ID { getset; }
        public string Name { getset; }
        public string FatherName { getset; }
        public string Gender { getset; }
 
    }
}



Reference:

http://automapper.org/
https://www.codeproject.com/Articles/61629/AutoMapper
https://www.codeproject.com/Articles/814869/AutoMapper-tips-and-tricks