2011-10-28 14 views
1

比方說,我有一個域對象是這樣的:什麼是不忽略Automapper中未使用的屬性的後果?

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?如果不在較大的對象上完成這項工作,是否會有大量開銷?謝謝。

回答

1

它不是必需的,但是當你單元測試你的映射(或者在運行時斷言以確保它們是準確的)時,忽略是它成功所必需的。

Mapper.AssertConfigurationIsValid(); 

你可以閱讀更多有關驗證AutoMapper配置正確的位置:

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

+0

感謝保羅。該鏈接很有用,因爲它引用了一個擴展方法,用於忽略單元測試中的所有未映射屬性。 – trevorc