Can you show simple example of accessing the contents of an entity in an aggregate via ita aggregate root? I am not clear on how you would represent the Aggregate permissions to reflect these concepts. tia.
+2
A:
You typically would encapsulate this in commands that the Aggregate exposes on its contract.
For example, with an Order Aggregate, you might add OrderLines using data obtained from your GUI.
// This is the Order Aggregate Root
public class Order
{
private readonly int id;
private readonly Customer customer; // Customer is another Aggregate
private readonly IList<OrderLine> orderLines;
private readonly IOrderLineFactory orderLineFactory;
public Order(int id, Customer customer, IOrderLineFactory orderLineFactory)
{
this.id = id;
this.customer = customer;
this.orderLines = new List<OrderLine>();
this.orderLineFactory = orderLineFactory;
}
public void AddOrderLine(Item item, int quantity)
{
OrderLine orderLine = orderLineFactory.Create(this, item, quantity);
orderLines.Add(orderLine);
}
}
Michael Hart
2009-04-05 14:24:10
I know this is an old question but...In your model, if I wanted to update an OrderLine, what would that look like....should there be a public orderLines list? And then pass the order back to a repository to update the Order/Customer/OrderItems?
Cedar Teeth
2010-01-20 17:35:12
A:
I dont have enough points to comment on the post (I hate to keep writing this everytime.. I still dont understand why I need certain points just to comment, but not to post).
Anyway, you (Michael Hart) gave an example about creating a new orderline, which is straightforward. What about changing the quantity? Say, you add more items of the same product to the order. How do you allow the Order class to change OrderLine quantity, without giving the same capability to other objects outside the aggregate boundary?
Sheepy
2010-10-29 02:54:56