0

Let's assume that I have an Order object that has a List<OrderItem> , the user goes to page and creates his order, now I should save his selection to the database, I have two data service classes one for each entity, OrderDataService and OrderItemDataService, which is more correct?

1- At presentation level in the Add button I write a code that calls OrderDataService.Add(Order order) method then loop through List<OrderItem> and call OrderItemDataService.Add(OrderItem item) for each OrderItem making the transaction happen at the level of presentation behind the Add button.

OR

2- Create a method in the OrderService.AddWithItems(Order order, List<OrderItem> items) that takes Order and OrderItem as parameter and make this method handle the looping and transaction?

Note: This is a very simple example, in practice things are more complicated and creating an OrderService.AddWithItems() method could end up taking too many paramters.

1

If you have an order object that has a list of items in it I would expect a service with an add method taking an order to save all of the order which includes the items. There shouldn't need to be additional parameters to add an order, the order object should know everything about orders, there shouldn't be a need to pass items, customers, or anything else outside of the order object, at least when adding an order.

2
  • As I mentioned this is just a simple example, but in practice the Order object in this example doesn't have a navigation property to 'OrderItem` that I can use. In many cases I need to manipulate multiple objects that represent a business transactions but are not necessarily related. – Sisyphus May 31 '18 at 14:09
  • @Sisyphus then you need to modify your abstraction or objects. they need to related or you need to create new objects that are more useful to you. passing around and working with a bunch of objects to do things is a sign they are related and you aren't modeling it properly, or your objects are a poor choice to model the process. – Ryathal Jun 1 '18 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.