Professional Documents
Culture Documents
The Dependency Inversion Principle: Elias Fofanov
The Dependency Inversion Principle: Elias Fofanov
Elias Fofanov
http://engineerspock.com
Outline
• Definition of DIP
• What is a dependency and what types of dependencies exist
• Inversion of Control and Dependency Injection
• Problems caused by DIP violation
• Refactoring to a better design
• Architectural implications of adhering to DIP
• IoC-containers and how to build a simple one on your own
• Non-trivial example of an application with an IoC-Container
• Common smells of DIP violation
• Conclusion
Next Video
Definition of DIP
Definition of DIP
DIP
• Framework
• 3rd party libs
• External Systems like File System, Database, any system resource
• Dependency on a custom type built on top of the .NET framework
Policy depends on Details
PersonRepository
Policy doesn’t depend on Details
Person
<<interface>>
IPersonRepository
PersonRepository
Policy depends on Details
<<interface>>
A Interface
+f()
+f()
Next Video
Volatile and Stable Dependencies
Volatile and Stable Dependencies
What dependencies should we abstract away?
Dependencies can be divided into two camps:
• Volatile
• Stable
Volatile Dependencies
<<interface>>
A Interface
+f()
+f()
Inversion of Control (IoC)
Possible pitfall:
• tends to accumulate many dependencies
• smell of SRP violation, consider extracting a class
Constructor Injection
Possible pitfall:
• several dependencies tend
to be passed in together
interface IDependency1 { } class Infrastructure : IInfrastructure
interface IDependency2 { } {
interface IDependency3 { } public Infrastructure(IDependency1 d1,
interface IDependency4 { } IDependency2 d2,
IDependency3 d3,
class ViewModel IDependency4 d4)
{ { }
public ViewModel(IDependency1 d1, }
IDependency2 d2,
IDependency3 d3, class ViewModel
IDependency4 d4) {
{ } public ViewModel(IInfrastructure infrastructure)
} { }
}
Constructor Injection
Possible pitfall:
• Non-obligatory dependencies
public Customer() {}
Possible pitfall:
• 3rd party framework imposes a public default constructor
//HAVE TO EXPOSE
public Customer() {}
Possible pitfall:
• a certain dependency is used only in a single method
Possible pitfall:
• Breaks encapsulation
Pitfalls:
• SRP Violation
• IoC-Containers don’t inject dependencies into methods
Next Video
Architectural Implications
Architectural Implications
Application Boundaries
<<interface>>
A Interface
+f()
+f()
Utils
Business
UI The Database
Logic
Utils
Factory Main
Factory
Factory Impl
• Conforms to “Single Choice” principle Main Partition
Factory
• Only Main knows about dependencies Impl
and their relationships
Next Video
Pure DI and IoC-Containers
Pure DI and IoC-Containers
Pure DI and IoC-Containers
WindowManager creates:
• ViewModel via IoC
clicks on a button MainViewModel calls • locates View
User
IWindowManager.Show(typeof(ViewModel)) • creates View
• Sets its data context to a newly
created ViewModel
Next Video
Common Smells of DIP Violation
Common Smells of DIP Violation
Common Smells
• A class explicitly creates one or more dependencies hiding them from a client
• A class uses nondeterministic dependencies like DateTime or Random
• extract a class which works with non-deterministic dependencies and cover it by integration tests
• create an adapter
• A class uses static dependencies, very often singletons
DIP implies that high-level policies should not depend on low-level details
Two major types of dependencies: stable and unstable (or volatile)
Unstable dependencies are those to which we want to apply the inversion of control
What IoC and DI are and how they’re related to the DIP and to each other
3 techniques of DI:
constructor injection which should be used in 95% of cases
property and method injection
Adhering to DIP leads to a plugin architecture which is known as the
“Ports and Adapters” architecture
There should be a single place which knows everything about application dependencies and their
relationships and this place is Main.
Manual dependency injection may become tedious;
that’s why IoC-containers exist, they help to simplify DI in difficult cases
How to build a simple IoC-container and
Example of a real-world application which relies on an IoC-container and sets it up in Main
Common smells of the DIP violation and how to fix them
Learn Metaprinciples in the next section.
Next Video
SRP Outline
Single Point Slide
● Good for making multiple points in one slide, or for anything that needs a
large amount of space in general
● In general between three to five bullet points is about right, any more than
that and everything starts to look a bit like a wall of text
● Also ideal for large charts, still images - animated GIFs can work as well if
you’re using Google Slides
Using Diagrams and Shapes
Step 3
Step 2 We challenge you to create
Step 1 and build your own examples
We show you how to analyse
We help you understand
and apply what you’ve seen
and remember key points
This format is ideal for quick
summaries and explaining the
reasoning behind certain
Why? decisions.
Development can be
Because this is the responsible
thing to do opinionated; and that’s totally
fine. Just bring the audience
along with you.