Linq to SQL データベース モデルで生成された部分的な Cart クラスを拡張しています (ここで正しい言葉かどうかはわかりません)。
ビジネス ロジックは、顧客ごとに 1 つのカートしか存在できないということです。顧客がカートを持っていない場合は、カートを作成する必要があります。顧客がカートを持っている場合は、カートを返却する必要があります。
これが私がやっていることです:
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;
}
}
コンストラクターからメソッドを呼び出すのは正しくないようです。ビジネス ロジックを正しい方法で実施しているか?