0

I have a financial application that processes 'bonds'. I need to

  1. Model the application to avoid an anaemic model (which i understand is bad).
  2. 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,

  1. Is this the best way to model?
  2. 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...

4

1 回答 1

2

一些想法:

  • Instruction似乎它服务于命令 DTO 和用于存储审计跟踪的值对象的目的。解耦这两个概念。

  • Bond作为金融领域的典型实体,该实体需要事件溯源。实际上,通过存储所有指令和解决方案,您已经在那里。让它明确。如果您改为对键进行任何更改,则可能不需要存储所有说明作为域事件。一条指令可能导致多个事件。

  • 域服务的示例实际上是应用程序服务或命令处理程序。应用程序服务协调存储库并委托给域对象。为了最好地实现应用程序服务,将尽可能多的业务逻辑委托给域对象,Bond在这种情况下是 a。因此,让Bond实体准确决定调用哪些子行为,以便应用程序服务仅调用实体上的单个方法。

  • 要提供多态性,请创建一个值对象以允许表示不同的键类型。让 Bond 实体委托给这个多态的债券类型值。您可能需要一个工厂来初始实例化此键类型 VO,但是一旦它与 Bond 实体相关联,您就不再需要调用工厂,Bond 只需引用键类型 VO。

于 2013-05-26T15:24:12.103 回答