I have a parent (Portfolio)/child (Trade) relationship, where each portfolio has 0 or more trades, and each trade has exactly on portfolio.
I currently have this coded as follows :
However this doesn't capture the requirement that Trades always have exactly one Portfolio, ie, parent cannot be null. I would ideally like the code to reflect this knowledge.
The other way I thought to do it is :
I worry that this makes my code less testable as it is more hassle to create trades now.
Also during the trade constructor, portfolio.addTrade() gets called, which isn't ideal (calling parameters methods within a constructor.
After writing this down, I'm beginning to think that the 2nd style is the best.
Does anyone have any views on this and/or
patterns that they use to handle this situation?
thanks, Don.