AutoMapper 在验证期间抛出 "No default constructor"
AutoMapper throwing "No default constructor" during validation
我要映射 类,但它们没有默认构造函数,我也不希望它们有。这是因为我只映射 to/from 个已经存在的对象。
public class Order
{
public string OrderName { get; set; }
public Order(string name)
{
this.OrderName = name;
}
}
public class OrderProcessor
{
private IService service;
public string OrderName { get; set; }
public OrderProcessor(IService service)
{
this.service = service;
Mapper.Initialize(config => config.CreateMap<Order, OrderProcessor>());
}
public void Init()
{
var order = this.service.GetOrder();
// this works
Mapper.Map(order, this);
// this fails
Mapper.Configuration.AssertConfigurationIsValid();
}
}
AutoMapper.AutoMapperConfigurationException : Unmapped members were
found. Review the types and members below. Add a custom mapping
expression, ignore, add a custom resolver, or modify the
source/destination type For no matching constructor, add a no-arg
ctor, add optional arguments, or map all of the constructor parameters
Order -> OrderProcessor (Destination member list)
No available constructor.
at Test() in Tests.cs:line
当我不想创建新对象时,如何使配置断言通过以及为什么它会失败?
您需要使用 .ConstructUsing(),因为您的类型没有无参数构造函数...
类似的东西;
Mapper.Initialize(config =>
config.CreateMap<Order, OrderProcessor>()
.ConstructUsing(x => new OrderProcessor(new WhateverService()));
);
您可能显然需要使用您正在使用的任何 DI 框架来解析 WhateverService....
我最近从 Automapper 4.x 升级到 6.x 后也遇到了这个问题。
您需要通过调用 CreateMap<TSource, TDest>().DisableCtorValidation()
.
告诉 AutoMapper 您不打算让它构造目标类型
根据方法文档:
// Summary:
// Disable constructor validation. During mapping this map is used
// against an existing destination object and never constructed itself.
//
我要映射 类,但它们没有默认构造函数,我也不希望它们有。这是因为我只映射 to/from 个已经存在的对象。
public class Order
{
public string OrderName { get; set; }
public Order(string name)
{
this.OrderName = name;
}
}
public class OrderProcessor
{
private IService service;
public string OrderName { get; set; }
public OrderProcessor(IService service)
{
this.service = service;
Mapper.Initialize(config => config.CreateMap<Order, OrderProcessor>());
}
public void Init()
{
var order = this.service.GetOrder();
// this works
Mapper.Map(order, this);
// this fails
Mapper.Configuration.AssertConfigurationIsValid();
}
}
AutoMapper.AutoMapperConfigurationException : Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
Order -> OrderProcessor (Destination member list)
No available constructor.
at Test() in Tests.cs:line
当我不想创建新对象时,如何使配置断言通过以及为什么它会失败?
您需要使用 .ConstructUsing(),因为您的类型没有无参数构造函数...
类似的东西;
Mapper.Initialize(config =>
config.CreateMap<Order, OrderProcessor>()
.ConstructUsing(x => new OrderProcessor(new WhateverService()));
);
您可能显然需要使用您正在使用的任何 DI 框架来解析 WhateverService....
我最近从 Automapper 4.x 升级到 6.x 后也遇到了这个问题。
您需要通过调用 CreateMap<TSource, TDest>().DisableCtorValidation()
.
根据方法文档:
// Summary:
// Disable constructor validation. During mapping this map is used
// against an existing destination object and never constructed itself.
//