订单和订单详细信息

时间:2012-02-03 20:42:03

标签: c# oop

我是Business Logic组件,可以让客户可以下订单。到目前为止,我的简化业务逻辑看起来像这样:

  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列表不支持包含多个数量产品的订单。如何在此处添加该支持?我如何从客户端调用它?

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

请勿使用List,使用Dictionary<Product,int>int参数是数量,或Dictionary<int,int>,其中第一个int是产品ID,第二个是数量。

您始终可以覆盖.EqualsProduct,以便根据您的产品ID实施,因此您仍然使用int来定义产品,但它可能会让事情变得更简单(或者你需要改变它)。

答案 2 :(得分:0)

我会添加第三个数据对象,其中包含包含返回产品的链接的订单商品。原因是你现在需要数量,但后来我猜你会想要给大件折扣,你可以调整每件商品的价格:

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

答案 3 :(得分:0)

你可以使它像

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

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

答案 4 :(得分:0)

您可以根据自己的需求来构建购物车的外观。单行将是某个产品的数量。像ProductLine对象那样引用产品和数量的东西。根据您的逻辑具体情况,您可能在产品(如制造商,SKU等)上拥有其他属性。有时您可能会从多个制造商那里获得可比较的产品,并且为了订单而感兴趣但需要跟踪它。

答案 5 :(得分: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?

相关问题