Ramesh
Ramesh

Reputation: 1752

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>.

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...

Upvotes: 0

Views: 106

Answers (3)

Skizz
Skizz

Reputation: 71060

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>>.

Upvotes: 0

Kamil T
Kamil T

Reputation: 2216

I'd say that having List<Order> in Product class can do much more bad than good. 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.

Instead, you can easily take all orders containing the product with :

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

Upvotes: 1

MotoSV
MotoSV

Reputation: 2368

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. The link from Order to Product makes sense because this way you can see what products make up the order.

Upvotes: 0

Related Questions