How to Implement Automapper 6.2.2

Asked

Viewed 771 times

2

I’m following a tutorial in which classes are configured as follows:

A class called Automapperconfig:

public class AutoMapperConfig
{

    public static void RegisterMappings()
    {

        Mapper.Initialize(x =>
        {
            x.AddProfile<DomainToViewModelMappingProfile>();
            x.AddProfile<ViewModelToDomainMappingProfile>();
        });

    }
}

Another class called Domaintoviewmodelmappingprofile, which maps from Dominio to Viewmodel:

public class DomainToViewModelMappingProfile : Profile
{

    public override string ProfileName
    {
        get { return "ViewModelToDomainMappings"; }
    }

    protected override void Configure()
    {
        CreateMap<ClienteViewModel, Cliente>();
        CreateMap<ProdutoViewModel, Produto>();
    }

}

And a third one called Viewmodeltodomainmappingprofile that maps from Viewmodel to the Domain:

public class ViewModelToDomainMappingProfile : Profile
{

    public override string ProfileName
    {
        get { return "DomainToViewModelMappings"; }
    }

    protected override void Configure()
    {
        CreateMap<ClienteViewModel, Cliente>();
        CreateMap<ProdutoViewModel, Produto>();
    }
}

What happens is that in:

protected override void Configure()

You are in error, I believe that because the version of the Automapper I am using is the latest, and the tutorial is the 3.2.1, someone would know how to solve this?

  • What’s the mistake ?

  • Viewmodeltodomainmappingprofile.Configure: no suitable method found to override

1 answer

3


In version 6.2.2 mapping is done via constructor, your mappings would look like this: DomainToViewModelMappingProfile:

public class DomainToViewModelMappingProfile : Profile
{
    public DomainToViewModelMappingProfile()
    {
        CreateMap<ClienteViewModel, Cliente>();
        CreateMap<ProdutoViewModel, Produto>();
    }
}

ViewModelToDomainMappingProfile:

public class ViewModelToDomainMappingProfile : Profile
{
    public ViewModelToDomainMappingProfile()
    {
        CreateMap<ClienteViewModel, Cliente>();
        CreateMap<ProdutoViewModel, Produto>();
    }
}

And the configuration class like this:

public class AutoMapperConfig
{

    public static void RegisterMappings()
    {

        Mapper.Initialize(x =>
        {
            x.AddProfile(new DomainToViewModelMappingProfile());
            x.AddProfile(new ViewModelToDomainMappingProfile());
        });

    }
}
  • Thanks for the help Barbetta,I managed to solve the same problem, only in version 7.1 of the Automapper.

Browser other questions tagged

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