2012-08-09 13 views
0

我正在嘗試創建一個簡單的電子商務應用程序。產品必須有品牌,並且至少有一個類別。我一直在使用Sharp Lite的例子MyStore和CaTs,所以如果你熟悉這些,你應該理解這個流程。我試圖在ProductController中創建Create操作。請問有人可以用Sharp Lite澄清Automapper的用法嗎? (Asp.Net MV3)

1)在ProductController中,我注入了我的產品存儲庫和我的產品服務。

2)在創建動作我打電話productService.CreateProductViewModel()

3)CreateProductViewModel我創建了一個新的ProductFormDto它具有所有屬性我的ViewModel需要(名稱,產品編號等) - 和也是類別和品牌的IE編號。

public CreateProductViewModel() 
    { 
     ProductFormDto = new ProductFormDto(); 
    } 

    public ProductFormDto ProductFormDto { get; set; } 
    public IEnumerable<ProductCategory> Categories { get; set; } 
    public IEnumerable<Brand> Brands { get; set; } 

} 

這將是一個相當複雜的觀點,我認爲。

我的問題是,採用了夏普精簡版,我需要用automapper到我的視圖模型映射像這樣:

public void Map() 
    { 
     Mapper.CreateMap<Product, ProductFormDto>(); 
    } 

我把這個在任務(服務)層ServiceLayerMappings.cs

如果那麼,我會在哪裏放置我見過的額外映射代碼,在大多數示例中,代碼都放置在Controller中?

我該如何/何時/何時第一次調用此ServiceLayerMappings?

ServiceLayerMappings是否將所有映射都映射到我的任務圖層根目錄下的所有ViewModels?

任何類型的指導將不勝感激,因爲我在這一點上撓了撓頭!

回答

0

啊我想我有。在MyStore的例子中有CudTasks文件,下面的方法是其中的一部分:

/// <summary> 
    /// Udpates an entitry from the DB with new information from the form. This example 
    /// manually copies the data but you could use a more sophisticated mechanism, like AutoMapper 
    /// </summary> 
    protected override void TransferFormValuesTo(ProductCategory toUpdate, ProductCategory fromForm) { 
     toUpdate.Name = fromForm.Name; 
     toUpdate.Parent = fromForm.Parent; 
    }