views:

48

answers:

1

I have several business classes, like Address, Product, Order, OrderLine. In this case, I have a question concerning my Order class and how it should be updated in the repository.

The Order class can have more than 1 OrderLine. The class looks something like the following:

class Order {
  private List<OrderLine> orderLines;

  public Address CustomerAddress { get; set; }
  public Address DeliveryAddress { get; set; }

  public void Add(OrderLine line) {
    // stuff to add order line.
  }

  public void Remove(int index) {
    // stuff to remove order line by index.
  }
}

When I need to fetch an Order I can do the following:

Order myOrder = Orders.Get(5); // Gets the order with ID 5 from repository.

Now, I can add or remove order lines as required and modify the CustomerAddress or DeliveryAddress.

When I want to update the modified order I can do:

bool updated = order.Update();

Which internally calls the Update method of the Orders repository like:

public bool Update() {
  return Orders.Update(this); // Let repository handle updating of this order.
}

Now suppose the order I previously got using Orders.Get(5) has 10 orderlines, and I modified 1 order line, added 1 orderline and removed 1 orderline.

Suppose that I assigned a different delivery address:

myOrder.DeliveryAddress = Addresses.Get(64); // Get address with ID 64 from repository and assign it to order.

What should happen in the Order Repository's Update method?

Should it update the whole order, inclusive all order lines, delete the order line that was removed, save the order line that was added and update the order line that was changed?

I'm a bit lost here as it gets very complex when all these internal items (Address and OrderLines) have to be updated, inserted or removed.

Anybody have suggestions?

Thank you very much in advance :)

A: 

If you follow the Domain Driven Design school of though, you would participate your model into aggregates, where each aggregate would consists of a number of related classes, Order, OrderLine, and so on. Within each aggregate one class is designated the aggregate root, e.g. Order, and takes on the responsibility of managing the model integrity of all objects within the aggregate. In DDD repositories operate on aggregates of objects, not single objects. Exactly how to participate the model into aggregates may be difficult, but once you've done that the responsibility becomes clear.

Felix Ungman