How to implement Automapper 5.0.2

Asked

Viewed 4,187 times

6

I set up a project a while back, as follows, A Class called Automapperconfig as follows:

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

another class: DomainToViewModelMappingProfile mapping from Dominio to Viewmodel:

public class DomainToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "ViewModelToDomainMappings"; }
    }

    protected override void Configure()
    {
        Mapper.CreateMap<UsuarioViewModel, Usuario>();
    }
}

and one that maps from Viewmodel to the Domain:

public class ViewModelToDomainMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "DomainToViewModelMappings"; }
    }

    protected override void Configure()
    {
        Mapper.CreateMap<Usuario, UsuarioViewModel>();
    }
}

And in the Controller Where I call a method where I get all users saved in the database

 public ActionResult Index()
 {
   var UsuarioViewModel= Mapper.Map<IEnumerable<Usuario>, IEnumerable<UsuarioViewModel>>(_usuarioApp.ObterTodos());
   return View(UsuarioViewModel);
 }

And finally in the Global.asax made the call from AutoMapperConfig.RegisterMappings();

With that already worked perfectly the mapping for this context... But I saw that from version 4.2 Automapper this type of configuration is obsolete. How do I implement the framework in the new way ?

1 answer

8


Renan, at this point little thing has changed, but now you need to store your Mapper in a static variable.

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

To call the mapper, you will need to do so:

public ActionResult Index()
{
    var UsuarioViewModel = AutoMapperConfig.Mapper.Map<IEnumerable<Usuario>, IEnumerable<UsuarioViewModel>>(_usuarioApp.ObterTodos());
    return View(UsuarioViewModel);
}

The configuration call on Global.asax remains the same. But now you have control of how to organize and more freedom in the configuration, for example you can create several IMappers.

A suggestion, you do not need to have a Profile for each mapping, this helps as much as put all the mappings in the same file so try to group in some way, for example a Profile of Domain.

finally, I see that Usuario and UsuarioViewModel are identical, as I do not see any transformation or omission of the data, the best would be that the UsuarioController work directly with the Usuario without having to do the Mapping.

  • got the answer. Now speaking of the hint you gave about working directly with the User class, I did see in some implementations, where you have a Viewmodel class to validate with Dataannotations and do not need to reference them in the main class, thinks it’s best to do this directly in the User class ?

  • 3

    @Renancarlos, completely unnecessary, the Framework has something much more appropriate and efficient: How to: Add Metadata Classes and Entity Framework Validation - IValidatableObject

Browser other questions tagged

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