Asraf Uddin Ahmed
Asraf Uddin Ahmed

Reputation: 202

Configure Automapper Profile Class with Parameter Constructor and Ninject

I am using Automapper (v5.1.1.0) and Ninject (v3.2.0.0). My Profile class is:

public class ApplicationUserResponseProfile : Profile
{
    public ApplicationUserResponseProfile(HttpRequestMessage httpRequestMessage) 
    {
        UrlHelper urlHelper = new UrlHelper(httpRequestMessage);
        CreateMap<ApplicationUser, ApplicationUserResponseModel>()
            .ForMember(dest => dest.Url, opt => opt.MapFrom(src => urlHelper.Link("GetUserById", new { id = src.Id })));
    }

    public ApplicationUserResponseModel Create(ApplicationUser applicationUser)
    {
        return Mapper.Map<ApplicationUserResponseModel>(applicationUser);
    }
}

And AutoMapperWebConfiguration is:

Mapper.Initialize(cfg =>
        {
            cfg.AddProfile<ApplicationUserResponseProfile>(); // unable to configure
        });

I have also tried to bind it into Ninject kernel:

var config = new MapperConfiguration(
            c =>
            {
                c.AddProfile(typeof(ApplicationUserResponseProfile));
            });
var mapper = config.CreateMapper();
kernel.Bind<IMapper>().ToConstant(mapper);

And different way:

Mapper.Initialize(cfg =>
        {
            cfg.ConstructServicesUsing((type) => kernel.Get(type));
            cfg.AddProfile(typeof(ApplicationUserResponseProfile));
        });

But got error in both ways -

No parameterless constructor defined for this object

Please help me. I am unable to configure AutoMapper profile class (which have parameter) with Ninject. Is there any different way to solve this problem?

Upvotes: 4

Views: 7915

Answers (2)

Leandro Carvalho
Leandro Carvalho

Reputation: 350

You can create a Resolver like this:

public class YourResolver : IValueResolver<ApplicationUser, ApplicationUserResponseModel, string>
{
    HttpRequestMessage HttpRequestMessage { get; }
  
    public YourResolver(HttpRequestMessage httpRequestMessage)
    {
      HttpRequestMessage = httpRequestMessage;
    }
  
    public string Resolve(ApplicationUser source, ApplicationUserResponseModel destination, string destMember, ResolutionContext context)
    {
       //do something with your HttpRequestMessage and return a value;
    }
}

And then in the Profile:

public class ApplicationUserResponseProfile : Profile
{
    public ApplicationUserResponseProfile()
    {
        CreateMap<ApplicationUser, ApplicationUserResponseModel>()
                 .ForMember(dest => dest.Url, opt => opt.MapFrom<YourResolver>());
    }
}

Source: https://docs.automapper.org/en/stable/Dependency-injection.html

Upvotes: 1

Asraf Uddin Ahmed
Asraf Uddin Ahmed

Reputation: 202

I have solved this problem in different way. I have migrated automapper from static instead of Profile approach.

public class ApplicationUserResponseFactory
{
    private MapperConfiguration _mapperConfiguration;
    public ApplicationUserResponseFactory(HttpRequestMessage httpRequestMessage) 
    {
        UrlHelper urlHelper = new UrlHelper(httpRequestMessage);
        _mapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<ApplicationUser, ApplicationUserResponseModel>()
                .ForMember(dest => dest.Url, opt => opt.MapFrom(src => UrlHelper.Link("GetUserById", new { id = src.Id })));
        });

    }

    public ApplicationUserResponseModel Create(ApplicationUser applicationUser)
    {
        return _mapperConfiguration.CreateMapper().Map<ApplicationUserResponseModel>(applicationUser);
    }
}

I have found the migration procedure here

Upvotes: 1

Related Questions