2. Agenda
• Dependency injection explained
• SOLID again
• Loose coupling & high cohesion
• Example
• Containers
• Configuration
• Patterns and anti-patterns
• Unit testing with auto-mocking containers
3. Dependency Injection &
Inversion of Control
• Pt copii de 5 ani
– Nu te bagi direrect in frigider
– Ceri exact ce ai nevoie si altcineva (mami si tati) iti
ofera de mancare
• Pt adulti
– The mafia principle: Don’t call us.. We’ll call U!
• Pt programatori
– O metoda care calculeaza totalul unei facturi nu
trebuie sa extraga fiecare item din baza de date. Este
de ajuns doar sa ceara pretul pentru fiecare item.
Responsabilitatea citirii din db revine caller-ului..
4. SOLID
• S – Single Responsibility Principle
• O – Open/Closed Principle
• L – Liskov Substitution Principle
• I – Interface Segregation Principle
• D – dependency inversion
• High level modules should not depend upon low level
modules. Both should depend upon abstractions.
• Abstractions should not depend upon details. Details
should depend upon abstractions.
9. Dependency Injection
• Dependency Injection is Inversion of Control
applied to dependency management
– Ask for dependencies using constructor
parameters, properties and method arguments
– Always leave the responsibility of instantiating
dependencies to the caller
• Side effect
– Nobody wants to be the top caller (dependency
injection hell)
11. The DI container
• Solution: the DI container
• Step1 - Unlearning StructureMap
– If you think it’s a factory on steroids, you’re using
it the wrong way!
• Step 2 – wire-up the container
• Step.. DONE
18. Patterns
• Constructor injection
• Property injection
• Method injection
• What to inject and what NOT!
– Unstable dependencies
• Doesn’t yet exist
• Machine/environment specific
• Nondeterministic
– No need to inject stable dependencies
• Already exist, behave deterministically, will not be replaced
ISP - Clients should not be forced to depend upon interfaces that they don't use. - Many client specific interfaces are better than one general purpose interface??