Saturday, May 26, 2018

Part 53 - How to use Automapper ForMember Method in ASP.NET MVC


In this video you will be able to know about how to use Automapper .ForMember() Method in  Asp.net MVC. Its very important to have customized property mapping. 

#AutoMapper Initialization (AutomapperWebProfile.cs)
 Create a folder Infrastructure in the root folder of the project and create a class AutomapperWebProfile and copy below code into this.

using MVCTutorial.Domain;
using MVCTutorial.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCTutorial.Infrastructure
{
    public class AutomapperWebProfile : AutoMapper.Profile
    {
        public AutomapperWebProfile()
        {

            CreateMap<EmployeeDomainModelWeb, EmployeeViewModel>()
                
                .ForMember(dest=>dest.ExtraValue,opt=>opt.MapFrom(src=>src.ExtraValue.Encrypt()))
                .ForMember(dest => dest.CurrentDate, opt => opt.MapFrom(src => src.CurrentDate.ToString("MM/dd/yyy hh:mm")));
            CreateMap<EmployeeDomainModel, EmployeeViewModel>();
            
            //Reverese mapping

            CreateMap<EmployeeViewModel, EmployeeDomainModelWeb>();     
            CreateMap<EmployeeDomainModel, EmployeeViewModel>();
        }

        public static void Run()
        {
            AutoMapper.Mapper.Initialize(a =>
            {
                a.AddProfile<AutomapperWebProfile>();


            });
        }
       

    }

    public static class ExtensionMethod
    {

        public static string Encrypt(this Int32 num)
        {

            return "Technotips:" + num;
        }
    }
}

#EmployeeDomainModel.cs 
Create below class into Model folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCTutorial.Models
{
    public class EmployeeDomainModelWeb
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public int ExtraValue { get; set; }
        public DateTime CurrentDate { get; set; }
    }
}

# EmployeeViewModel.cs 

Create below class into Model folder

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVCTutorial.Models
{
    public class EmployeeViewModel
    {
        public int EmployeeId { get; set; }

        [Required(ErrorMessage = "Enter Name")]
        public string Name { get; set; }

        public string ExtraValue { get; set; }

        public string CurrentDate { get; set; }

       
    }
}


# Controller Code (TestController.cs)
Create a Test controller and in your Index method write below code. Here, I have added few record in EmployeeDomainModel and using AutoMapper, I copied data into EmployeeViewModel directly. In this way you can use Automapper. 

        public ActionResult Index()
        {
             List<EmployeeDomainModel> empDomainList = new List<EmployeeDomainModel>();

            empDomainList.Add(new EmployeeDomainModel { EmployeeId = 1, Name = "Ashish", ExtraValue = 2, CurrentDate = DateTime.Now });
            empDomainList.Add(new EmployeeDomainModel { EmployeeId = 2, Name = "Ajay", ExtraValue = 3, CurrentDate = DateTime.Now });

            List<EmployeeViewModel> empVMList = new List<EmployeeViewModel>();

            AutoMapper.Mapper.Map(empDomainList, empVMList);


            return View();
        }



#Global.asax file
Add below code into Application_Start() Method. to Initialize Automapper and to create map between source and destination folder. 

  AutomapperWebProfile.Run();

Thanks  . Keep Learning and Sharing


All Code Factory


No comments: