Automapper ninject dependencies

I have a problem with Automapper on my site and I cannot find a solution. I created a class called AutoMapperProfile where I would like to place all my Maps

public class AutoMapperProfile: Profile
{
    private readonly IConfiguration _mapper;

    public AutoMapperProfile(IConfiguration mapper)
    {
        _mapper = mapper;
    }

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

        _mapper.CreateMap<SlideDTO, Slide>();
        _mapper.CreateMap<Slide, SlideDTO>();
    }
}

For DI purposes, I use Ninject, so I added the following bindings to NinjectWebCommon:

kernel.Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);
kernel.Bind<IConfigurationProvider>().ToMethod(x => Mapper.Engine.ConfigurationProvider);

The controller is as follows:

private readonly ISlideRepository slideRepository;
    private readonly IMappingEngine mappingEngine;

    public HomeController(
        ISlideRepository slideRepository,
        IMappingEngine mappingEngine)
    {
        this.slideRepository = slideRepository;
        this.mappingEngine = mappingEngine;
    }

    [HttpGet]
    public ActionResult Index()
    {
        var model = new IndexViewModel();
        var slide = slideRepository.GetSlide();
        model.Slide = mappingEngine.Map<SlideDTO, Slide>(slide);

        return View(model);
    }

When I switch from SlideDTO to Slide, I get the following error:

Missing type map configuration or unsupported mapping.

So I think I didn’t do the bindings correctly so that Automapper could see my maps, but I'm not sure how to fix it.

+4
source share
1 answer

You do not need to type IConfigurationin AutoMapperProfile, it already inherits the method CreateMapfrom Profile.

, AutoMapperProfile :

public class AutoMapperProfile : Profile
{
    protected override void Configure()
    {
        this.CreateMap<SlideDTO, Slide>();
        this.CreateMap<Slide, SlideDTO>();
    }
}

, AutoMapper , :

Mapper.Engine.ConfigurationProvider.AddProfile<AutoMapperProfile>();

, AddProfile IConfigurationProvider ( ConfigurationProvider Engine).

+1

Source: https://habr.com/ru/post/1618941/


All Articles