I’m trying to create a simple eCommerce application. Products must have a brand and also at least one category. I’ve been using (quite heavily) the Sharp Lite examples MyStore and CaTs – so if you’re familiar with these you should hopefully understand the flow. I’m trying to create the Create action in the ProductController.
1) In the ProductController I inject both my product repository and my product service.
2) In the Create action I call productService.CreateProductViewModel()
3) in the CreateProductViewModel I create a new ProductFormDto which has all the properties my ViewModel needs (Name, Product Code, etc.) – and also the IEnumerables of both Category and Brands.
public CreateProductViewModel()
{
ProductFormDto = new ProductFormDto();
}
public ProductFormDto ProductFormDto { get; set; }
public IEnumerable<ProductCategory> Categories { get; set; }
public IEnumerable<Brand> Brands { get; set; }
}
It’s going to be quite a complex View I think.
My question is, using Sharp Lite do I need to use automapper to map my ViewModel like so:
public void Map()
{
Mapper.CreateMap<Product, ProductFormDto>();
}
I placed this in the Tasks (services) layer as ServiceLayerMappings.cs
If so, where would I put the extra mapping code that I’ve seen, which in most examples is placed in the Controller?
How / where / when would I first call this ServiceLayerMappings?
Would the ServiceLayerMappings have all the maps to all of my ViewModels in a single file at the root of my Tasks layer?
Any sort of guidance would be greatly appreciated, as I’m scratching my head a little bit at this point!
Ah I think I have it. In the MyStore examples there are CudTasks files, and the following method is part of it: