自动映射器配置文件在api控制器中不起作用

时间:2018-09-13 20:49:59

标签: c# api automapper autofac profile

我有两节课:

public class Parametro
{
    public int Id { get; set; }
    public string Nombre { get; set; }
    public int TipoDatoId { get; set; }
    public TipoDato TipoDato { get; set; }
}

public class ParametroCreateViewModel
{
    public string Nombre { get; set; }
    public int TipoDatoId { get; set; }
}

当我以这种方式将它设置为_mapper.Map(模型)时;我收到以下错误:

  

找到未映射的成员。在下面查看类型和成员。添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,请添加no-arg ctor,添加可选参数或映射所有构造函数参数   ParametroCreateViewModel-> Parametro(目标成员列表)ProyectoTest.ServiciosCliente.Application.Parametros.Commands.AddFParametro.ParametroCreateViewModel-> Calidda.ServiciosCliente.Domain.Parametros.Parametro(目标成员列表)未映射的属性:ID

但是当我以这种方式尝试时,如果可行: Mapper.Map (模型);

我的autofac如下:

builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile));
            builder.Register(c => new MapperConfiguration(cfg =>
            {
                foreach (var profile in c.Resolve<IEnumerable<Profile>>())
                {
                    cfg.AddProfile(profile);
                }
            })).AsSelf().SingleInstance();
            builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();

我的automapperconfig是:

public class AutoMapperConfig : Profile
{
    public static void Initialize()
    {
        Mapper.Initialize((config) =>
        {

config.CreateMap<ParametroCreateViewModel, Parametro>()
            .ForMember(dest => dest.Id, opt => opt.Ignore()).ReverseMap();
        }
     }
   }

并声明我的global.asax:

AutofacConfig.Register();
AutoMapperConfig.Initialize();
GlobalConfiguration.Configure(WebApiConfig.Register);

1 个答案:

答案 0 :(得分:1)

看起来这里是多个问题的组合。您应该使用概要文件类加载某些程序集,并将它们注册为Profile。在您的示例中,因为容器未包含Profile的任何注册,所以foreach循环未执行任何迭代。要创建映射,您应该使用配置文件本身而不是静态Mapper类。未调用静态方法Initialize,因此未创建映射。请考虑以下示例:

public class AutoMapperConfig : Profile
{
    public AutoMapperConfig()
    {
        // Create mapping within profile in constructor
        CreateMap<ParametroCreateViewModel, Parametro>()
            // ignore both unmapped properties
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.TipoDato, opt => opt.Ignore())
            .ReverseMap();
    }
}

[Test]
public void AutoMapperAutofacTest()
{
    // Arrange
    var builder = new ContainerBuilder();

    // load certain assembly
    builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AutoMapperConfig)))
            .AssignableTo<Profile>()
            .As<Profile>(); // register as profile
    builder.Register(c => new MapperConfiguration(cfg =>
    {
        foreach (var profile in c.Resolve<IEnumerable<Profile>>())
        {
            cfg.AddProfile(profile);
        }
    })).AsSelf().SingleInstance();
    builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
    var container = builder.Build();

    var mapper = container.Resolve<IMapper>();

    var param = new Parametro();
    var viewModel = new ParametroCreateViewModel
    {
        Nombre = "Nombre",
        TipoDatoId = 1
    };

    // Act
    mapper.Map(viewModel, param);

    //Assert
    Assert.AreEqual(param.TipoDatoId, 1);
    Assert.AreEqual(param.Nombre, "Nombre");
}

我已经用测试包装了示例,以使其可重现。希望对您有所帮助。