2012-02-03 156 views
3

我是業務邏輯組件,使客戶可以在網上訂購。到目前爲止,我簡單的商業邏輯是這樣的:訂單和訂單明細

public class Product 
    { 
     public int productID { get; } 
     public string name { get; set; } 
     //other properties here like address and such 
    } 


    public class Order 
    { 
    public int orderID { get; } 
    public Customer customer { get; set; } 

    public List<Product> OrderItems { get; set; } 
    //other properties go here 

    } 

Products List不支持包含多個批量的產品訂單。我如何在這裏添加支持?我如何從客戶端調用它?

回答

1

不要使用List,使用Dictionary<Product,int>,其中int參數是數量,或Dictionary<int,int>,其中第一int是產品ID和第二個是數量。

你總是可以覆蓋.Equals爲您Product類產品ID的方面來實現的,所以你還是使用int定義產品,但它可能使事情變得簡單一點的道路(或如果你需要改變它)。

+0

感謝您的答覆。 'Dictionary'總是需要一個密鑰,該密鑰必須是唯一的。這種情況下關鍵是什麼? – Victor 2012-02-03 20:46:35

+0

您應該使用ProductID作爲關鍵字(假設您只希望您的訂單中的每個產品都有一次)。它是一個整數,因此計算Dictionary內部使用的散列速度非常快,您也可以知道產品是否已經在您的訂單中。 – Jay 2012-02-03 20:50:15

+0

@Jay - 啊,所以它是'Dictionary '。是的,Dictionary在搜索時比列表快得多,但是當您實際下訂單時,它不會增加複雜性,因爲客戶現在需要知道他們選擇的每個項目的產品ID? – Victor 2012-02-03 21:04:44

0

我要補充的,裏面有包含一個鏈接回產品訂單項目第三個數據對象。是的原因是,你現在需要的數量,但後來我要猜你會想給大的折扣,你可能會調整價格每件下降:

public class OrderLineItem 
{ 
    Product p { get; set; } 
    int Quantity {get; set;} 
    Decimal PricePerItem {get; set;} 
} 
0

你可以把它像

東西
class OrderItem { 
    public Product Product .. 
    public int Qty .. 
} 

class Order { 
    public List<OrderItem> Items .. 
} 
3

另一種方法是添加了一個間接層與OrderItem類:

public class Product 
{ 
    public int productID { get; } 
    public string name { get; set; } 
} 

public class OrderItem 
{ 
    public Product product { get; set; } 
    public int quantity { get; set; } 
} 

public class Order 
{ 
    public int orderID { get; } 
    public Customer customer { get; set; } 

    public List<OrderItem> items { get; set; } 
} 

Order現指的OrderItems每個OrderItem具有關聯quantity列表。

0

你可以構建它,你將如何想象一個購物車的樣子。一條線就是某個產品的數量。像引用產品和數量的ProductLine對象。取決於您的邏輯具體是什麼,您可能對產品有其他屬性,例如製造商,SKU等。有時,您可能會從多個製造商那裏獲得可比較的產品,並且爲了訂單而不感興趣,但需要跟蹤這一點。

0

請澄清:

1)在課堂上令您的意思是否這樣寫:

public List<Product> OrderItems() { get; set; } 
//other properties go here 

2)你確定你是不是缺少一箇中間對象:

public class OrderItem 
{ 
    public int productID { get; } 
    public int quantity { get; set; } 
    // possibly other properties 
} 

在這種情況下,你會:

public List<OrderItem> OrderItems() { get; set; } 

3)您是否試圖確保每個OrderItem的數量爲1?換句話說,你不想讓人們訂購不止一種產品?或者你是否試圖確保有人不會將同一產品兩次添加到OrderItems中?

+0

感謝您的回覆。 1)是的,我的意思是,只是一個錯字抱歉2)不,我不確定,埃德的迴應增加了這一點。 3)不,我不是要確保允許多個數量,因此我的原始問題 – Victor 2012-02-03 21:05:10

+0

>>產品列表將不支持包含多個產品的訂單。我如何在這裏添加支持? << 由此我以爲你的意思是你想實施「不支持包含產品的訂單......」 在這種情況下,我同意愛德。字典是一個簡單的選項,可以讓多種產品同時確保任何產品只出現在列表中一次。 – Bitfiddler 2012-02-03 21:20:27