简体   繁体   English

多对多关系类设计

[英]Many To Many Relationship class design

How to handle the following scenario. 如何处理以下情况。

I have a Product Class Which has List<Orders> and Order Class which has List<Products> . 我有一个具有List<Orders>Product类和一个具有List<Products> Order类。

Is it good to have the above structure or it will create any problem in future. 具有上述结构是否很好,否则将来会出现任何问题。 If its causing the problem, then is there any other way to implement it... 如果是引起问题的,那么还有其他方法可以实现它...

I'd say that having List<Order> in Product class can do much more bad than good. 我想说,在Product类中使用List<Order>可能弊大于利。 One product can be - in dependency of size of your project - in thousands of orders. 根据项目的大小,一种产品可以成千上万个订单。

In that case, even when you simply want to create a list of products, you will have to create thousands of instances of Order in it - a completely useless data. 在这种情况下,即使您只想创建产品列表,也将不得不在其中创建数千个Order实例-完全无用的数据。

Instead, you can easily take all orders containing the product with : 取而代之的是,您可以轻松地通过以下方式接受包含该产品的所有订单:

List<Order> _Orders = AllOrders.Where(_Order => _Order.ProductIds.Contains(YourProductId));

You really don't need a link from Product to Order because if you want to find out if a particular product has been ordered in the past, then you can search the products associated with each order using the ID of the product. 您确实不需要从ProductOrder的链接,因为如果您想了解某个特定产品在过去是否已订购,则可以使用该产品的ID搜索与每个订单关联的产品。 The link from Order to Product makes sense because this way you can see what products make up the order. OrderProduct的链接很有意义,因为通过这种方式,您可以看到由哪些产品构成订单。

You could uncouple the Order and Product types by using a tuple to link a Product with an Order. 您可以通过使用元组将产品与订单链接来解除订单和产品类型的耦合。

Remove List<Order> from Product and List<Product> from Order and create a third class that contains a List<Tuple <Order, Product>> . Product移除List<Order>并从Order移除List<Order> List<Product>并创建一个包含List<Tuple <Order, Product>>的第三类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM