假设我在购物车场景中,涉及以下步骤:
- 从数据库中获取购物车
- 计算税金
- 总计一切
Project.Core(拥有所有领域类)
public CartItem
{
public int Id { get; set; }
public string Desc { get; set; }
}
public CartDetails
{
public int Id { get; set; }
public List<CartItem> CartItems { get; set; }
public Decimal Tax { get; set; }
public Decimal Total { get; set; }
}
项目.DAL
public class CartDAL
{
public List<CartItem> GetCart(int cartId)
{
//execute sql here, get datareader or datatable
//loop thru the rows and mait into a List<CartItem>
return List<CartItem>
}
}
查看 Dapper 或 Massive,而不是像上面那样手动。
http://code.google.com/p/dapper-dot-net/
https://github.com/robconery/massive
项目.BLL
public class CartBLL
{
CartDAL cartDAL = new CartDAL();
public CartDetails GetCartDetails(int cartId)
{
var cartDetails = new CartDetails();
cartDetails.CartItems = cartDAL.GetCart(cartId);
cartDetails.Tax = cartDAL.GetCart(cartId);
cartDetails.Total = cartDAL.GetCart(cartId);
return cartDetails;
}
}
项目网站
public class CartController
{
CartBLL cartBLL = new CartBLL();
public ActionResult Index(int Id)
{
var cartDetails = cartBLL.GetCartDetails(Id);
// Make a cartViewModel
View(cartViewModel);
}
}
项目.WPF
//Nothing much changes in desktop comapared to Web, just call the BLL stuff and you are set
CartBLL cartBLL = new CartBLL();
var cartDetails = cartBLL.GetCartDetails(Id);
//Populate view
所以,基本上你必须在所有前端项目中重用你的 BLL