shake
shake

Reputation: 333

DDD element: Aggregates in c#

When analyzing life cycle of domain objects, aggregate is basic element for objects grouping. I am having trouble implementing aggregetes in C#.

One short example, with couple of classes, would be very helpful. Or any link on this theme.

Upvotes: 2

Views: 3827

Answers (2)

Fossmo
Fossmo

Reputation: 2892

You should check out Udi Dahans blog and Greg Youngs blog. A lot of great stuff there concerning DDD and CQRS. A lot of good questions and answers can be found and the Yahoo Doman Driven Design group. I know I haven't linked to a specific example, but if you look in this links you will find a lot of material and examples.

Upvotes: 1

jason
jason

Reputation: 241789

class Order {
    public int OrderNumber { get; private set; }
    public Address ShippingAddress { get; private set; }
    public Address BillingAddress { get; private set; }
    private readonly IList<OrderLine> OrderLines { get; private set; }
    public void AddItem(Item item, int quantity) {
        OrderLine orderLine = new OrderLine(item, quantity);
        OrderLines.Add(orderLine);
    }
    // constructor etc.
}

class OrderLine {
    public Item Item { get; private set; }
    public int Quantity { get; private set; }        
    public OrderLine(Item item, int quantity) {
        Item = item;
        Quantity = quantity;
    }
}

At no point should logic involving OrderLines be exposed outside of an instance of Order. That's the point of aggegrate roots.

For a .NET specific reference, see Applying Domain-Driven Design and Patterns: With Examples in C# and .NET. Of course, the standard reference here is Domain Driven Design: Tackling Complexity in the Heart of Software . There's a good article on MSDN too.

Upvotes: 8

Related Questions