DDD Entities making use of Services

后端 未结 5 1336
生来不讨喜
生来不讨喜 2021-02-01 08:18

I have an application that I\'m trying to build with at least a nominally DDD-type domain model, and am struggling with a certain piece.

My entity has some business logi

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 09:10

    Given what we've seen of the classes, I don't think they're really services in the blue book sense, and I would keep the calculators in Ticket.

    Neither FinancialCalculatorService or RateCalculationService has dependencies on domain entities - they both operate on primitive values. Applications shouldn't have to worry about how to calculate the financial gain that would result from a ticket, so it's valuable to encapsulate that information inside the ticket itself.

    If they really don't have dependencies on domain entities, consider thinking of them as 'standalone classes' rather than 'services' (once again, in blue book terminology). It's certainly appropriate for Ticket depend on strategy objects (FinancialCalculator and RateCalculator) that do not themselves have exotic dependencies and do not themselves modify the state of domain entities.

    Update for Edit 2. I think one of the advantages of making the calculators separate classes is that you can test them independently of Ticket. Strictly speaking, tickets aren't responsible for performing those calculations, they're responsible for making the right calls to those collaborating classes. So I'd be inclined to make them inject-able / mock-able as they were in your initial example.

提交回复
热议问题