Matt
Matt

Reputation: 2843

DDD modify child object through parent

I'm writing a monthly expenditure application where you add 'expenditures' to a month, these expenditures contain a description of what the expenditure is and the amount. For example you may have April 2015, and within that you have as expenditure items such as:

etc...

I've written the following, thus allowing you to add items to the month

class ExpenditureMonth
{
    public function addExpenditureItem(String description, Money amount)
    {
        items.add(new ExpenditureItem(this, description, amount));
    }
}

Therefore the object creation of ExpenditureItem happens within ExpenditureMonth. An expenditure month can contain many items.

When the account holder has paid for an item, it needs to be marked as paid.

How would I set an item as paid?

My only thinking is set an identifier for an ExpenditureItem and pass this instance to the addExpenditureItem method but this doesn't seem right to me?!

Upvotes: 0

Views: 670

Answers (1)

Arvin Yorro
Arvin Yorro

Reputation: 12097

I had a similar question before (please read the answers).

In DDD, the parent or the aggregate root changes depending on the bounded context. So in your case, this is your context:

When the account holder has paid for an item, it needs to be marked as paid.

For that particular context, the expenditure item is now the aggregate root, and now it requires ID to be identified on its own. Setting up an ID or a GUID is the generally accepted solution, I don't think it is reliable to uniquely identify an object without a proper key.

However, if the case is "the account holder paid for all his monthly unpaid expenditures (or all unpaid expenditures)", then for that context the aggregate root is your MonthlyExpenditure.

Upvotes: 1

Related Questions