Dependency Direction in Layers
What You'll Learn
You'll learn the fundamental rule that governs how layers interact: dependencies flow downward from higher layers to lower layers, never upward.
The One-Way Street Rule
In a layered architecture, dependencies must only point downward. This means an upper layer (like your presentation layer) can depend on and call into a lower layer (like your business logic layer), but the reverse is strictly forbidden. The business logic layer must never directly depend on or call back into the presentation layer.
Think of it like a building: the second floor rests on the first floor's foundation, not the other way around. The second floor "knows about" and relies on the first floor, but the first floor exists independently and has no knowledge of what's built above it.
Why This Matters
This one-way dependency rule creates several crucial benefits:
Modularity: Lower layers become reusable because they don't hard-code knowledge of their consumers. Your business logic can serve multiple presentation formats (web UI, mobile app, API) because it doesn't depend on any specific one.
Testability: You can test lower layers in isolation without needing the upper layers to exist yet.
Flexibility: You can replace or modify upper layers without touching lower ones. Want to swap your web framework? No problem—your business logic is unaffected.
Clear boundaries: Each layer has a well-defined responsibility and interface, making the system easier to understand and maintain.
The Violation Warning
If you ever find yourself needing a lower layer to call back into a higher layer, stop. This is a red flag. Instead, use techniques like callbacks, events, or dependency inversion (which you'll learn later) to maintain the proper dependency direction while achieving the communication you need.
Key Takeaway: Dependencies in layered architecture always flow downward—upper layers depend on lower layers, never the reverse, creating modularity and maintainability.