3

I’m trying to figure how best to define a Use Case using Clean Architecture and DDD. Let’s say I have an application that handles the pick, pack and shipping of a delivery. Here is the flow:

  1. User enters a deliver to populate the screen with shipping information
  2. User selects line items and clicks a button to Pick
  3. User enters package information(e.g. weights and dims) and clicks a button to Pack.
  4. User clicks Ship button to call an external system to get shipping labels

Here are the options I’m considering for defining my Use Case Interactors:

  1. Create 4 Interactor classes, one for each step listed above
  2. Create 1 Interactor class with 4 methods to handle the steps listed above
  3. Create 3 Interactor classes

    a. Interactor 1 would handle the Enter Delivery and Pick

    b. Interactor 2 would handle the Packing

    c. Interactor 3 would handle the Shipping

Thank you in advance!

0

2 Answers 2

4

It depends on the business rules: what are the system's valid states? In this case the system is the DeliveryAggregate.

  • if the system is allowed to be in any of the 4 states at a given moment then you can have 4 Interactors or a single Interactor with 4 methods.

  • if the system can be in only 3 states (i.e. Picked, Packed or Shipped) then again, 3 Interactors or only 1 but with 3 methods.

Here you can apply Single responsibility principle and choose separate Interactors.

So, in conclusion, the Interactos design is strongly driven by the Aggregates design because the Aggregates are the consistency boundaries.

3
  • 3
    If you want to look at a detailed example, I have blogged about the topic here: plainionist.github.io/Implementing-Clean-Architecture-UseCases Feb 11, 2018 at 20:47
  • @plainionist Your blog is great! Thank you. This is a pretty good answer to the question, I think: "The 'driving principle' for an Interactor for a Use Case is SRP, Single Responsibility Pattern." Your series of articles on implementing Clean Architecture is very helpful and practical.
    – allenjom
    Feb 7, 2019 at 15:56
  • @allenjom Thx a lot! Always great to get such feedback. I hope I will find time soon to continue the series ... Feb 7, 2019 at 20:20
3

To me, an interactor embodies a use case, and a use case is embodied by an interactor.

So in the case of a use case made out of several steps, I will wonder, for each step : is that step a valid use case by its own ?

It would make sense to see seeing shipping information as a use case, but would it make sense to see select line item and mark it as picked as a use case ?

If the answer is yes, then make an associated Interactor. Otherwise, it is probably not something that is going to be specific to one business rule and not re-used in your application (business-wide), so making an Interactor for that is unnecessary. You will not want to see it pop on your screen when you browse for use cases !

Note that the result from this perspective joins the result from Constantin's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.