Object Design Question

Object Design Question

Old forum URL: forums.lhotka.net/forums/t/12700.aspx


MadGerbil posted on Wednesday, July 16, 2014

I've the following object graph:

Client Singleton
Charges Collection
Payments Collection
ChargePayments Collection

The Client object has the 3 collections as child objects.
The Charges collection holds a collection of all charges to the client.
The Payment collection holds a collection of all payments to the client.
The ChargePayments collection manages the many to many relationships bewteen Charges and Payments.

The balance on a charge is the total of all amount applied to that charge as located in the ChargePayments collection.
The amount applied for a payment is the total of all amount applied for that payment located in the ChargePayments collection.

When a payment is deleted (this can happen while creating the record) all the ChargePayments related to that payment are deleted which means the totals on any Charges that used that payment need to be updated.
Likewise, if a charge is deleted the ChargePayments related to that Charge are deleted and the amount applied for each of the Payments needs to be updated.

Long story short, the removal of items in the ChargePayments collection (and addition of items) needs to cause each member of the Charges and Payments collections to update their totals.

POSSIBLE FIXES
1: Should this be managed by the parent object such that the parent Client object flips through children collections updating values
2: Should each Charge and each Payment have a reference to the ChargePayments collection and update themselves when the ChargePayments collection changes
3: Is there a better way altogether to handle this?

RockfordLhotka replied on Tuesday, July 22, 2014

The less objects know about each other (or reference each other) the better - as a general rule.

I'd probably have the root object listen for ChildChanged events and use that knowledge to tell the correct child object graphs to update themselves as necessary.

MadGerbil replied on Thursday, July 31, 2014

That is the route I ended up taking - it not only preserves the object integrity but it is the easiest to work with by far.

 

Copyright (c) Marimer LLC