不忽略Automapper中未使用的属性的后果是什么?

本文关键字:属性 后果 是什么 未使用 Automapper | 更新日期: 2023-09-27 18:12:46

假设我有一个像这样的域对象:

public class Product
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Description {get;set;}
   public int DisplayOrder {get;set;}
   //Lots of other properties
}

对于我的视图,我想使用2个不同的视图模型,它们使用产品类的不同属性。

public class ProductViewModel1
{
   public int Id {get;set;}
   public string Name {get;set;}
   //A mix of some of the other properties
}
public class ProductViewModel2
{
   public int Id {get;set;}
   public string Description {get;set;}
   //A different mix of the other properties
}
Automapper

:

Mapper.CreateMap<Product, ProductViewModel1>();
Mapper.CreateMap<Product, ProductViewModel2>();

问题(s): 是否有必要将所有被忽略的属性添加到CreateMap?如果在较大的对象上不这样做,会有很大的开销吗?谢谢。

不忽略Automapper中未使用的属性的后果是什么?

这不是必需的,但是当您对映射进行单元测试(或在运行时断言以确保它们是准确的)时,需要使用ignore才能成功。

Mapper.AssertConfigurationIsValid();

你可以在这里阅读更多关于验证AutoMapper配置是否正确的信息:

http://automapper.codeplex.com/wikipage?title=Configuration%20Validation