Topic 049 / Core
Separation of concerns and dependency management
Chapter 4.4 · Dependencies and Application Structure
What and why
As modules multiply, which module is allowed to depend on which becomes an unstated rule everyone breaks differently — until two modules depend on each other and neither can be understood, tested, or deployed alone. Separation of concerns assigns each module one clear responsibility; dependency management makes the allowed dependency directions explicit — usually by injecting collaborators rather than constructing them internally — and treats a dependency cycle as a defect to be broken, not routed around. Builds on: → 44 (Single Responsibility and Dependency Inversion, generalized from a class to a module).
Diagram
Before (cycle) After (inverted)
─────────────── ─────────────────
OrderModule ──► PaymentModule OrderModule ──► PaymentGateway (interface,
▲ │ ▲ owned by OrderModule)
└───────────────┘ │
(PaymentModule calls back into PaymentModule implements PaymentGateway;
OrderModule to update status — OrderModule depends on nothing from
neither module can change alone) PaymentModule's internals.
How it works
- State each module's responsibility in one sentence; a sentence that needs "and" is describing two modules.
- Draw the dependency graph between modules; a cycle (A needs B, B needs A) is the signal to act.
- Break the cycle with an interface owned by the module that should stay stable; the other module implements or calls through it (dependency inversion, → 44).
- Inject dependencies through constructors or parameters rather than having a module reach out and construct its own collaborators — this is what makes swapping or faking a dependency possible.
- Re-check the graph: dependencies should point in one direction only, from concrete, volatile modules toward stable abstractions.
Practical example
(illustrative). In the task manager, TaskModule needs to notify
on completion and NotificationModule needs task details to format a message — wiring
them to call each other directly creates a cycle. The fix: TaskModule defines a
NotificationSender interface it owns; NotificationModule implements it; TaskModule
is injected with the implementation at startup.
When to use it
- Use when: two modules reference each other, or a module is hard to test without standing up unrelated modules.
- Avoid when: a dependency will only ever have one implementation and never needs a fake — adding an interface then is indirection with no payoff.
- Failure scenarios: a cycle that "works" today because of import order or lazy loading, then breaks the moment one module is extracted as a separate deployable.
- Simpler alternative: direct calls between two modules with no cycle and no test-double need; add the interface once the cycle or the second implementation actually appears.
Practice
(15–20 min; no lab services — any language). Take two modules that call each other directly, draw the dependency graph, identify the cycle, and break it with one owned interface plus injection. Done when one module compiles and runs its tests with the other entirely absent.
Key takeaway
Separation of concerns names one responsibility per module; dependency management makes the allowed direction between modules explicit and acyclic. Done when you can: find a dependency cycle in a real codebase and name the exact interface that breaks it. Used next in → 50 (layered architecture fixes the allowed directions into a stack), → 53 (modular monolith enforces this at the module boundary), → 143 (the same discipline, one level up, decides whether to split a service at all).
References
None — this topic makes no external claims.
Source directory