I’ve the following piece of model:
Deal -< TradeInstruction // a deal has multiple trade instructions
and need to calculate balance for a Deal
which is transient and defined as follows:
public BigDecimal sumDealTradeInstructions(Long dealId) { return getDealTradeInstructions(dealId).stream() .filter(ti -> ti.getStatus() != CANCELLED) .map(TradeInstruction::getValue) .reduce(ZERO, BigDecimal::add); }
Now, in code there’s no relation between these two entities (yes, both beings are JPA entities) and the method for calculating the balance is defined (along with a handful of other let put this here methods) in TradeInstructionService
. I don’t think this is a good place for this responsibility. In a perfect world there would be mapping between the entities and balance would be calculated in Deal
:
class Deal { List<TradeInstruction> tradeInstructions ... BigDecimal getBalance() { return tradeInstructions ... } }
but here it’s not possible to introduce the mapping, hence I want to move the calculation to DealBalanceCalculator
.
Questions:
- Is my reasoning correct?
- How do I obtain the list of
TradeInstruction
s in this to-be-defined calculator to calculate the balance? Via service, repository, another being?