How to use AutoMapper 6.2.2 in Asp MVC5?

1

Hello, how to use AutoMapper 6.2.2 in Asp MVC5 to map my view models to the domain model.

Example: In MVC I use UserViewModel and User domain.

    
asked by anonymous 14.12.2017 / 12:22

1 answer

4

I'll show an example using the category class and the viewcategory of the ...

using System;
using System.Collections.Generic;
using AutoMapper;
using LojaVirtual.Aplicacao.Interfaces;
using LojaVirtual.Aplicacao.ViewModels;
using LojaVirtual.Aplicacao.AutoMapper;
using LojaVirtual.Dominio.Interfaces.Services;
using LojaVirtual.Dominio.Entidades;
using System.Linq.Expressions;

namespace LojaVirtual.Aplicacao.Services
{
    public class AppServiceCategorias : IAppServiceCategorias
    {
        private readonly IServiceCategorias _repositorio;
        private readonly IMapper _mapper;

        public AppServiceCategorias(IServiceCategorias repositorio)
        {
            _repositorio = repositorio;
            _mapper = AutoMapperConfig.Mapper;
        }

        public IEnumerable<CategoriasViewModel> GetAll()
        {
            return _mapper.Map<IEnumerable<CategoriasViewModel>>(_repositorio.GetAll());
        }

        public CategoriasViewModel GetById(int id)
        {
            return _mapper.Map<CategoriasViewModel>(_repositorio.GetById(id));
        }

        public void Register(CategoriasViewModel customerViewModel)
        {
            var categoria = _mapper.Map<Categorias>(customerViewModel);
            _repositorio.Add(categoria);
        }

        public void Update(CategoriasViewModel customerViewModel)
        {
            var categoria = _mapper.Map<Categorias>(customerViewModel);
            _repositorio.Update(categoria);
        }

        public void Remove(CategoriasViewModel customerViewModel)
        {
            var categoria = _mapper.Map<Categorias>(customerViewModel);
            _repositorio.Remove(categoria);
        }

        public void Remove(int id)
        {
            _repositorio.Remove(id);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}

This is a DDD standard with MVVM usage. in general you have to do the map out and back to the template ..

return _mapper.Map<CategoriasViewModel>(_repositorio.GetById(id));

See above, here I'm doing a mapping of a return from my model to my view model.

Already here;

var categoria = _mapper.Map<Categorias>(customerViewModel);
                _repositorio.Remove(categoria);

I'm mapping my view model to the template.

Your autmap needs to know what objects will be mapped when your application starts.

using AutoMapper;

namespace LojaVirtual.Aplicacao.AutoMapper
{
    public class AutoMapperConfig
    {
        public static IMapper Mapper { get; private set; }
        public static void RegisterMappings()
        {
            var _mapper = new MapperConfiguration((mapper) =>
            {
                mapper.AddProfile<DomainToViewModelMappingProfile>();
                mapper.AddProfile<ViewModelToDomainMappingProfile>();
            });

            Mapper = _mapper.CreateMapper();
        }
    }
}

The above class does this. see that I have two mappings one back and forth between view model and model.

using AutoMapper;
using LojaVirtual.Aplicacao.ViewModels;
using LojaVirtual.Dominio.Entidades;

namespace LojaVirtual.Aplicacao.AutoMapper
{
    public class DomainToViewModelMappingProfile : Profile
    {
        public DomainToViewModelMappingProfile()
        {
           CreateMap<Categorias, CategoriasViewModel>();
        }
    }
}

and

using AutoMapper;
using LojaVirtual.Aplicacao.ViewModels;
using LojaVirtual.Dominio.Entidades;

namespace LojaVirtual.Aplicacao.AutoMapper
{
    public class ViewModelToDomainMappingProfile : Profile
    {
        public ViewModelToDomainMappingProfile()
        {
            CreateMap<CategoriasViewModel, Categorias>();
        }
    }
}

As I mentioned in the initialization of your project you need to start your mapping class.

protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AutoMapperConfig.RegisterMappings();
        }

You can download the here project.

    
14.12.2017 / 12:51