Suppose we have a layered architecture like onion architecture or clean architecture with three modules representing each layer:
domain.py usecase.py databaseadapter.py
The domain layer contains the business logic, so i have functions, which transforms data:
in usecase.py we have:
There comes a point, when the business logic needs to do I/O and hence has to call an adapter. I could now either inject the adapter into other domain in order to call it return the data, handle the adapter in the usecase and continue the logic.
- variant: injection
in the domain.py call data_adapter to save in database
- variant: return to usecase everytime i do I/O:
value = domain.business_logic() value_2 = database_adapter.save(value) domain.business_logic2(value_2)
What is a better way? I would argue there is no problem with injecting adapters into the domain layer, but i would like to hear arguments against it and if its better to handle all I/O in the usecase and why?