AutoMapper In C#

I hope you’re doing great well in your life. I’ll give you a brief rundown of AutoMapper in C#.

What precisely is an AutoMapper?
Data is collided from object to object using AutoMapper.

Scenario when we used to require AutoMapper?
The communication from the services or data subcaste is always a worry for the reality layers in a factual design. A second class called a ViewModel or Model Class is required to display the data in operation. One aspect of the design could or might not correspond to reality. We’ll need an AutoMapper to map the subcaste model to a model or ViewModel.

How does it work?

  • First, we need to add  NuGet package AutoMapper 
  • Create a class  Student
    using System;
    
    namespace AutoMappper.Models
    {
        public class Student
        {
            public Int64 StudentId { get; set; }
            public string StudentFirstName { get; set; }
            public string StudentLastName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public DateTime? DateOfJoining { get; set; }
        }
    }
  • Create another class called User
    using System;
    
    namespace AutoMappper.Models
    {
        public class User
        {
            public Int64 Userid { get; set; }
            public string UserFirstName { get; set; }
            public string UserLastName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public DateTime? DateOfJoining { get; set; }
        }
    }
  • Now, we map the Student class to a User class
    using AutoMapper;
    using AutoMappper.Models;
    using System;
    using System.Web.Mvc;
    
    namespace AutoMappper.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                Student studentObj = new Student
                {
                    StudentId = 1,
                    StudentFirstName = "Piyush",
                    StudentLastName = "Vavaliya",
                    Address = "Thecodehubs",
                    City = "Surat",
                    State = "Gujarat",
                    Zip = "	395008",
                    DateOfJoining = DateTime.Now,
                };
    
                //Creates the map if properties are same both class
                IMapper mapper = new MapperConfiguration(map => {
                    map.CreateMap<Student, User>();
                }).CreateMapper();
    
                //Creates the map if some of properties are not same both class
                mapper = new MapperConfiguration(map => {
                    map.CreateMap<Student, User>()
                   .ForMember(o => o.Userid, b => b.MapFrom(z => z.StudentId))
                   .ForMember(o => o.UserFirstName, b => b.MapFrom(z => z.StudentFirstName))
                   .ForMember(o => o.UserLastName, b => b.MapFrom(z => z.StudentLastName));
    
                }).CreateMapper();
    
                User userObj = mapper.Map<Student, User>(studentObj);
    
                return View();
            }
        }
    }
  • Output

Submit a Comment

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

Subscribe

Select Categories