问题描述:

I am creating a Web API asp.net 5 using EF 6 without any repository, UOW, Facade in top of EF.

I do want to apply business rules in Service layer keeping async (at least the return of every API method) applying business logic

and make some conversions between Product to ProductVO

Product Service

public async Task<ProductVO> getProductVO(int id)

{

using (_context = new OnlineShopContext())

{

EntityMapper mapper = new EntityMapper();

Product product = await GetAsync(id);

Task<Supplier> supplier = _context.Suppliers.SingleOrDefaultAsync(s => s.ID == product.Supplier.ID);

Task<Category> category = _context.Categories.SingleOrDefaultAsync(c => c.ID == product.Category.ID);

return await mapper.map(product, await supplier, await category);

}

}

Product service needs to wait for product for finding supplier and category. (This is a very simple case, but think about calling a SP, or there is a locking

on supplier or category, handling exceptions?)

Entity mapper

public Task<ProductVO> map(Product product, Supplier supplier, Category category)

{

ProductVO productVo = new ProductVO(id: product.ID, productName: product.Name, supplierId: supplier.ID,

supplierName: supplier.Name, categoryId: category.ID, categoryName: category.Name,

thumbImageName: product.ThumbImageName, imageName: product.ImageName, price: product.Price

);

return new Task<ProductVo>productVo;

}

Think about a more complex case of evaluating a list of Products until matching a business rule without waiting for the whole list from the db to start

matching an specific requirement. Similar to my AsyncEx library with OrderByCompletion option.

Thank you

相关阅读:
Top