我正在扩展(不确定此处是否正确)在 Linq to SQL 数据库模型中生成的部分 Cart 类。
业务逻辑是每个客户只能有一个购物车。如果客户没有购物车,则应该创建它;如果客户有购物车,则应退回。
这就是我正在做的事情:
public partial class Cart
{
//the rest of the Cart class is in the .dbml file created by L2S
public Cart(int userId)
{
Cart c = GetCurrentCart(userId);
this.CartId = c.CartId ;
this.UserId = c.UserId;
}
public Cart GetCurrentCart(int userId)
{
Cart currentCart = new Cart();
// if cart exists - get it from DB
//if not - create it, save in DB, and get if right out
//all of this is done with Linq to SQL
return currentCart;
}
}
从构造函数调用方法似乎并不正确。我是否以正确的方式执行业务逻辑?