Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.Net : How to use AutoMapper

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    AutoMapper can be defined as an object to object mapping i.e mapping similar properties in one object of one type to another object of different type. It is a library which is being used to get rid of extra code that is used to map one object into another.

    A program demonstrating mapping one object into another without using AutoMapper:

    using System;
    namespace ConsoleApplication1
    {
     public class Program
        {
            public static void Main()
            {
                var program = new Program();
                Employee employee = program.GetEmployeeFromDB();  
                EmployeeViewItem employeeViewItem = new EmployeeViewItem() //Mapping one object into another
                {
                    FirstName = employee.FirstName,
                    LastName = employee.LastName,
                    Designation = employee.Designation
                };
    
                Console.WriteLine("Employee.FirstName={0} Employee.LastName={1} Employee.Designation={2}", employee.FirstName, employee.LastName, employee.Designation);
                Console.WriteLine("EmployeeViewItem.FirstName={0} EmployeeViewItem.LastName={1} EmployeeViewItem.Designation={2}", employeeViewItem.FirstName, employeeViewItem.LastName, employeeViewItem.Designation);
                Console.ReadKey();
            }
    
            private Employee GetEmployeeFromDB()
            {
                return new Employee()
                {
                    FirstName = "Deepak",
                    LastName = "Rana",
                    Designation = "Manager"
                };
            }
        }
    
        public class Employee
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Designation { get; set; }
        }
    
        public class EmployeeViewItem
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Designation { get; set; }
        }
    }

    Output:

    Employee.FirstName=Deepak Employee.LastName=Rana  Employee.Designation=Manager
    EmployeeViewItem.FirstName=Deepak EmployeeViewItem.LastName=Rana EmployeeViewItem.Designation=Manager


    In above code employee object of type Employee is copied into object employeeViewItem of type EmployeeViewItem. Since there is less number of properties to be mapped, code is short but when there will be more columns to be mapped a lot of extra code will be there. AutoMapper solve this problem

    A program demonstrating mapping one object into another using AutoMapper:

    using System;
    using AutoMapper;
    
    namespace ConsoleApplication1
    {
     public class Program
        {
            public static void Main()
            {
                Mapper.CreateMap<Employee, EmployeeViewItem>();
                var program = new Program();
                Employee employee = program.GetEmployeeFromDB();
                EmployeeViewItem employeeViewItem = Mapper.Map<Employee, EmployeeViewItem>(employee);
                  
                Console.WriteLine("Employee.FirstName={0} Employee.LastName={1}  Employee.Designation={2}", employee.FirstName, employee.LastName, employee.Designation);
                Console.WriteLine("EmployeeViewItem.FirstName={0} EmployeeViewItem.LastName={1} EmployeeViewItem.Designation={2}", employeeViewItem.FirstName, employeeViewItem.LastName, employeeViewItem.Designation);
                Console.ReadKey();
            }
    
            private Employee GetEmployeeFromDB()
            {
                return new Employee()
                {
                    FirstName = "Deepak",
                    LastName = "Rana",
                    Designation = "Manager"
                };
            }
        }
    
        public class Employee
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Designation { get; set; }
        }
    
        public class EmployeeViewItem
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Designation { get; set; }
        }
      }
    }

    Output:

    Employee.FirstName=Deepak Employee.LastName=Rana  Employee.Designation=Manager
    EmployeeViewItem.FirstName=Deepak EmployeeViewItem.LastName=Rana EmployeeViewItem.Designation=Manager

    In above code a default mapping is created between Employee and EmployeeViewItem by calling Mapper.CreateMap<Employee, EmployeeViewItem>() and finally call Mapper.Map<Employee, EmployeeViewItem>(employee) to get mapped object of EmployeeViewItem. Therefore, Automapper can be used to get rid of extra code that is used to map one object into another.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: