使用Parameter Constructor和Ninject配置Automapper Profile Class

时间:2016-09-28 06:59:57

标签: c# asp.net-mvc ninject automapper-5

我正在使用Automapper(v5.1.1.0)和Ninject(v3.2.0.0)。我的个人资料类是:

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);
    }
}

AutoMapperWebConfiguration是:

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

我也尝试将它绑定到Ninject内核:

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

以不同的方式:

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

但两种方式都有错误 -

  

没有为此对象定义无参数构造函数

请帮帮我。我无法使用AutoMapper配置Ninject个人资料类(包含参数)。有没有不同的方法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

我以不同的方式解决了这个问题。我已从静态而非automapper方法迁移Profile

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);
    }
}

我找到了迁移程序here

答案 1 :(得分:0)

您可以像这样创建解析器:

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;
    }
}

然后在个人资料中:

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

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