0

我正在尝试创建一个简单的电子商务应用程序。产品必须有品牌和至少一个类别。我一直在使用(相当多)Sharp Lite 示例 MyStore 和 Cats - 所以如果你熟悉这些,你应该希望能理解流程。我正在尝试在 ProductController 中创建 Create 操作。

1) 在 ProductController 中,我注入了我的产品存储库和我的产品服务。

2) 在创建操作中,我调用 productService.CreateProductViewModel()

3) 在 CreateProductViewModel 中,我创建了一个新的 ProductFormDto,它具有我的 ViewModel 需要的所有属性(名称、产品代码等) - 以及 Category 和 Brands 的 IEnumerables。

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

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

}

我认为这将是一个相当复杂的视图。

我的问题是,使用 Sharp Lite 是否需要使用 automapper 来映射我的 ViewModel,如下所示:

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

我将它作为 ServiceLayerMappings.cs 放置在任务(服务)层中

如果是这样,我将把我见过的额外映射代码放在哪里,在大多数示例中,它放在控制器中?

我将如何/在何处/何时首次调用此 ServiceLayerMappings?

ServiceLayerMappings 是否会将我所有 ViewModel 的所有映射都放在我的 Tasks 层根目录的单个文件中?

任何形式的指导都将不胜感激,因为此时我有点摸不着头脑!

4

1 回答 1

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;
    }
于 2012-08-09T10:51:56.770 回答