Professional Documents
Culture Documents
14 DependencyInjectionOnRamp
14 DependencyInjectionOnRamp
- Dependency injection is a software design pattern that allows the removal of hard-coded
dependencies and makes it possible to change them, whether at run-time or compile-time –
Wikipedia, 2013 – this means: A little better: includes both compile-time and run-time
bindings
- Dependency injection is a set of software design principles and patterns that enable us to
develop loosely coupled code – Mark Seemann
Patterns - There are a number of different patterns that we can use to implement Dependency Injection:
- Constructor Injection – most used patterns
- Property Injection – most used patterns
- Method Injection
- Ambient Context
- Service Locator
1
Object Composition - Another concept of Dependency Injection is how we compose our objects. Our
goal is loose coupling, but we have to be able to snap these loosely coupled pieces of code together to
form our application. In Dependency Injection, we refer to this as the Composition Root.
DI Containers - When we implement Dependency Injection on our application, we most likely will not do
this manually. Instead, we will use one of the many Dependency Injection Containers that are available
to us. These include:
- Unity – from the Microsoft Patterns and Practices Team
- Ninject
- Castle Windsor
- Autofac
- StructureMap
- Spring.NET – which is a .NET port of the Spring framework in the Java world
- And many others
Dependency injection containers – There are a number of dependency injection containers to choose
from. These are sometimes referred as Inversion of Control or IoC containers. Here are some of the
most. The good news is that the most popular ones support the same features. Here are just a few of
the more popular Containers:
- Unity comes from the Microsoft Patterns & Practices Team. It's available as a free download.
- Spring.NET is more than just a DI Container, it's an entire framework with many features.
Dependency Injection is just one of those. Spring.NET is a port of the Spring framework from
the Java world, so this may be a good choice if you have both .NET and Java applications in
your portfolio.
- Castle Windsor comes out of the Castle Project.
- Ninject
- Autofac
- StructureMap
- And many others.
Late binding- Late Binding allows us to make decisions about what objects we want to use at run-time.
We can dynamically load assemblies and use those in our applications.
2
- Dynamic Loading through Configuration - we could use XML Configuration to dynamically
load types and assemblies at run-time. This lets us swap out dependencies without needing
to recompile our code.