4

我正在使用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. 有没有不同的方法来解决这个问题?

4

2 回答 2

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

我在这里找到了迁移过程

于 2016-09-28T12:52:11.363 回答
1

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

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

于 2021-06-07T12:47:02.437 回答