c# - Accessing container properties from child list -
i have container class called paymentplan, holds basic payment summary info. holds list of expectedpayments. basic oop type question, brain appears have gone sleep - have property in expectedpayment class needs interrogate property on paymentplan class determine result.
i'm storing reference paymentplan property on expectedpayment class, however, has public getter, , following ddd, feel bit of code smell. there better way achieve want?
i've removed necessary bits example:
public class paymentplan { private readonly list<expectedpayment> _payments; public paymentplan(list<expectedpayment> payments) { ... other stuff //todo: fix smell _payments = payments; _payments.foreach(p => p.plan = this); } ... other properties }
and expectedpayment class:
public class expectedpayment { public expectedpayment(... args removed example) { } //todo: attempting avoid public setter have no control.. public paymentplan plan { get; set; } public paymentstate paymentstate { { if (plan.somepropertyonplan == "somevalue") { return paymentstate.somestate; } else { ... other logic determine payment state of expected payment. } } } ... other properties }
any appreciated! - know techniques if possible know there multiple ways achieve i'm after.
thanks, alex
as if expectedpayment
shouldn't exist if hasn't paymentplan
, must force expectedpayment()
constructor provide paymentplan
caller , add "friendly" method adding new expectedplan
on paymentplan
.
public class expectedpayment { public expectedpayment(paymentplan plan, ..... other args) { plan = plan ; plan.addexpectedplan(this) ; ...... } ......
....
public class paymentplan { ......... internal void addexpectedplan(expectedplan expectedplan) { ........... _payments.add(expectedplan) ; } }
Comments
Post a Comment