Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
shake Avatar asked Dec 11 '25 04:12

shake


1 Answers

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.

like image 57
jason Avatar answered Dec 12 '25 19:12

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!