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?
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