locked
How to Initilize two Automapper profile in asp.net mvc? RRS feed

  • Question

  • User140010582 posted

    I have to AutoMapper Profile like:

    1.

        public class ViewModelToDomainMappingProfile:Profile
        {
            public override string ProfileName => "ViewModelToDomainMappingProfile";
            public ViewModelToDomainMappingProfile()
            {
                CreateMap<EmployeeTableVm, EmployeeTable>();
    
            }
    
        }
    

    2.

        public class DomainToViewModelMappingProfile:Profile
        {
            public override string ProfileName => "DomainToViewModelMappingProfile";
            public DomainToViewModelMappingProfile()
            {
                CreateMap<EmployeeTable, EmployeeTableVm>();
            }
    
        }

    I want to initialize two profile in one like

            public static void Configure()
            {
                Mapper.Initialize(x =>
                {
                    x.AddProfile<ViewModelToDomainMappingProfile>();
                    x.AddProfile<DomainToViewModelMappingProfile>();
                });
            }

    The Mapper does not have definition for Initialize it says how can I do this in MVC 

    also I have inject dependency as

        public class EmployeeController : Controller
        {
            private readonly IEmployeeService _employeeService;
            private readonly IMapper _mapper;
            public EmployeeController(IEmployeeService employeeService,IMapper mapper)
            {
                _employeeService = employeeService;
                _mapper = mapper;
            }
            public ActionResult Index()
            {
                var employee = _employeeService.GetAllEmployees();
                var modelResources = _mapper.Map<IEnumerable<EmployeeTable>, List<EmployeeTableVm>>(employee);
                return View(modelResources);
            }
        } 

    also I don't have any idea where and how to register dependency for IMapper.

    please help.
     

    Tuesday, June 23, 2020 4:56 PM

All replies