I have a financial application that processes 'bonds'. I need to
- Model the application to avoid an anaemic model (which i understand is bad).
- Instantiate different implementations depending on the type of bond.
The system gets instructions from an external system, and applies the instructions to the specified bond. hence i have an Instruction entity
Instruction[Id,BondReference,Action,Value]
e.g. of an instruction to vote 'yes' for a resolution passed on the bond
Instruction
{
BondReference: Bond1,
Action: Vote
Value: VoteYes
ResolutionReference: Resolution1
}
and a Bond entity
Bond
{
Id: 1,
Reference: Bond1
Resolutions: [
{Resolution1: We have resolved to increase our stake in Google},
{Resolution2: We have resolved to fire the CEO}
...
]
Instructions: [Inst1,Inst2,Inst3...]
}
However, an instruction usually does more than just one thing (it is effectively many instructions in one), e.g. an instruction to cancel a previous instruction, that would mean, firstly, it cancels a previous transaction, then certain values need to be recalculated. Also, a single instruction can come overloaded, it can be to cancel a previous instruction as well as vote for a resolution.
I have been advised to use a domain service to process a new instruction.
BondService
{
public void Apply(Instruction newInstruction)
{
var bond = _bondRepository.GetByReference(newInstruction);
bond
.RecalculateNominalValue(newInstruction)
.CalculateInterest(newInstruction)
.CancelInstruction(newInstruction)
.Approve(newInstruction);
}
}
The problem i see with this structure is that for each instruction, all methods are called even if the method is not relevant. I could use some if statements but then the code would look untidy.
Now my question is,
- Is this the best way to model?
- And for the calculation, depending on the bond type the calculation differs. So i want to implement polymorphism. I have been told that i need to use a factory to instantiate the correct implementation. Is this the best approach?
For BondType 1,3,5 i use calculationA, for bondType 2,7... i need to use Calculation B. how do i INSTANTIATE the different calc types??? NB, i am well versed with polymorphism, however, i have been unable to find solid examples of how to instantiate the correct implementation. thnx for reading this far...