Download presentation
Presentation is loading. Please wait.
1
Mark Seemann - Dependency Injection in .NET
2
DI Definition Dependency Injection is a set of software design principles and patterns that enable us to develop loosely coupled code. * DI Container is not required. * Inversion of Control is a wider term than Dependency Injection.
3
Benefits of DI Late binding
Services can be swapped with other services. Extensibility & Maintainability Code can be extended and reused in ways not explicitly planned for. Classes with clearly defined responsibilities are easier to maintain. Parallel development Code can be developed in parallel. Testability Classes can be unit tested.
4
Stable & Volatile Dependencies
Stable dependency satisfies all these rules: The class or module already exists. You expect that new versions won’t contain breaking changes. The types in question contain deterministic algorithms. You never expect to have to replace the class or module with another. If dependency does not satisfy one or several points from above list, then it’s a volatile dependency that should be injected.
5
Configuring DI Containers
6
Configuration ways: pros & cons
7
Composition Root
8
Register Resolve Release
9
DI Patterns Constructor Injection Property Injection Method Injection
Ambient Injection
10
Constructor Injection
11
Property Injection
12
Property Injection usage
Unity DependencyAttribute
13
Method Injection
14
Ambient Context
15
Choosing DI pattern
16
DI anti-pattern: Control Freak
17
DI anti-pattern: Bastard Injection
18
DI anti-pattern: Constrained Construction
*Is only applicable for late binding.
19
DI anti-pattern: Service Locator
20
DI refactorings (challenges)
Mapping runtime value to ABSTRACTIONS Route algorithm example. Use Abstract Factory. Working with short-lived DEPENDENCIES Connections, clients, etc. Use Abstract Factory. Resolving cyclic DEPENDENCIES Break dependency through Property Injection.
21
Object Composition Console application ASP.NET MVC application
WCF service WPF application ASP.NET application PowerShell cmdlets
22
Lifestyle catalog
23
Interception
24
Cross-Cutting Concerns
25
DI Container Feature comparison
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.