9

As far as i understand, use cases or interactors use entities to perform some action, whereas, entities holds our business rules.

According to this definition:

The use cases interact with and depend on the entities

In what way use cases depend on entities, what is the relationship ?

For example let's say we have a banking app with three functionalities: login, view balance and transfer funds.

So, to be able to transfer funds the user must be logged in and should have sufficient balance.

I guess our uses case here is transfer funds and the statement above is business rule, and if that's the case, how to implement it?

There are so many analogies but very few proper implementations.

eddy
  • 193
  • 3
  • 8

1 Answers1

10

I'd like to share my understanding.

  1. Entity does not depend on anything except possibly other entities. It holds data (state) and logic reusable for various applications. I've also seen entities as plain POJOs (but it is not the same thing as what's returned by your data access layer)
  2. Use cases interact with entities (thus depend on them) and hold logic of the specific application (and typically execute that logic via various repositories or data access layer(s) gateway(s)

For the example you've specified:

  1. Entities:
    • User (holds user name, hashed&salted password; logic like validate user name, hash plain-text password)
    • Balance (holds user dependency, amount, limits, logic like verify if given transfer amount is OK)
  2. Use cases:
    • Authenticate (based on user-name/password input, validate it and (using some sort of repository or gateway to data) pull user entity from backend, along with some token likely), likely cache it if success or report errors if any
    • View Balance (based on user entity input, pull balance entity from backend (same as above...), report errors if any
    • Transfer Funds (based on user entity and amount input, pull balance entity, verify if transfer permitted, perform if so or report error if not)
ror
  • 3,295
  • 1
  • 19
  • 27
  • 2
    I'm still confused, because i haven't seen any proper implementation. You mentioned User entities holds user name, hashed etc,but from where do we pass all these credentials? – eddy Dec 04 '19 at 10:20
  • From the UseCases. The UseCase is about how and when the Entities will be executed. Imagine that an entity is something so fundamental that it doesn't contain conditionals. The entity only transfers money, but the UseCase checks if the user can transfer money. – Daniel Jul 31 '22 at 04:53
  • @Daniel what happens if conditional checks in the use-case layer becomes stale between execution of entities ? since it's totally possible that the the database would have been altered in that time? would it be a violation of clean architecture to put the logic in the entity interface that DB will implement so its closer to the database to run checks as part of its query? i.e. createUser: insert if email/username not found. – cozycoder Oct 05 '22 at 15:19