How to use Automapper 6.2.2 in Asp MVC5?

Asked

Viewed 1,172 times

2

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

Example: in MVC I use Usuarioviewmodel and in the User domain.

1 answer

7


I will demonstrate an example using the category class and the viewcategoriamodel...

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 standard DDD with use of MVVM. in general you have to map out and back to the template..

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

Look at the above, here I am mapping a return of my model to my view model.

Already here;

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

I’m mapping my model view to the model.

Your autmap needs to know which objects will be mapped when your application is started.

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 class above does this. see that I have two one-way mappings 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 said at the start 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 project here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.