是否可以告知automapper属性可以保留为空?
问题描述:
我想要决定是否要将Automapper作为一项技术用于我的公司。在我深入研究之前,我有一个问题,我想确定它是否适用于automapper。是否可以告知automapper属性可以保留为空?
说我在我的目的地类有一个属性,我不想用automapper填充。有没有办法告诉automapper忽略该属性,并且在我致电Mapper.AssertConfigurationIsValid()
时不会失败?
因此,举例来说:
class DTOMyObject
{
public int Test {get; set;}
public int Test2 {get; set;}
public int Test3 {get; set;}
public int Test4 {get; set;}
public int Test5 {get; set;}
}
class ViewMyObject
{
public int Test {get; set;}
public int Test2 {get; set;}
public int Test3 {get; set;}
public int Test4 {get; set;}
public int Test5 {get; set;}
public int MyCustomUnMappedProperty{get; set;}
}
映射这些(与ViewMyObject
作为目标)后,我希望能够调用Mapper.AssertConfigurationIsValid()
并将它没有失败,如果(且仅当)MyCustomUnMappedProperty
是唯一一个是未映射的。
有没有办法做到这一点?如果是这样,你能告诉我一个例子吗?
答
Mapper.CreateMap<Src, Dest>()
.ForMember(d => d.MyCustomUnmappedProperty, o => o.Ignore());