Architecture & DesignIntermediate
Dependency Injection
Pronunciationdih-PEN-den-see in-JEK-shun
Definition
A class receives the things it needs (database, mailer) from outside instead of creating them itself, so they are easy to replace in tests.
Where you hear it
NestJS, Spring, Angular, and testing discussions.
Examples
Thanks to dependency injection, we replaced the real mailer with a fake in tests.
Inject the repository instead of creating it with
new.
Common mistake
Creating dependencies with new inside the class. Then you cannot replace them for testing.
Keep exploring
More in Architecture & Design