High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend in details. Details should depend on abstractions
- Framework
- Third party libraries
- Database
- File System
- Web Services
- System Resources (Clock)
- Configuration
- The new Keyword
- Static methods
Dependency Injection is a technique that is used to allow calling ode to inject dependencies a class needs when it is instantiated
!!! Do not call us; we will call you
- Constructor Injection
- Property injection
- Parameter Injection
Dependencies are passed in via consturctor !! Strategy Pattern !!
- Classes self document what they need to perform theirs work\
- Works well with or without container
- Classes are always in valid state once constructed
- Constructors can have many parameters/dependencies (design smell)
- Some features (Serialization) may require a default constructor
- Some methods in the class may not require things other methods require (design smell)
Dependencies are passed in via property Also known as "setter injection"
- Dependency can be changed at any time during object lifetime
- Very flexible
- Objects may be in an invalid state between construction and setting of dependencies via setters
- Less intuitive
Dependencies are passed in via parameter
- Most granular
- Very flexible
- Requires no change to rest of class
- Breaks methods signature
- Can result in many paramters (design smell)
- Extract dependencies into interfaces
- Injectt implementations of interfaces into Main
- Reduce Main claas's responsibilities
- Facade Pattern
- Inversion of Control Containers