infuerno.github.io

Seemann: Dependency Injection in .NET

References

Source code: http://manning.com/DependencyInjectionin.NET

Chapter 1: A Dependency Injection tasting menu

Fallacies about DI

Purpose of DI

Liskov Substitution Principle - should be able to replace one implementation of an interface without breaking either client or implementation. One of the most important software design principles for DI.

Benefits of DI

When to use DI

Not everything needs to be abstracted away and made pluggable. Distinguish between types that pose no danger (stable) and types that may tighten an application’s degree of coupling (volatile).

DI Scope

By removing the responsibility of instantiating a dependency from a consuming class, that class loses control, but the control is not lost, just moved to another place. In doing so there are several benefits:

There are many misconceptions about DI. Some people think that it only addresses narrow problems, such as late binding or unit testing; although these aspects of soft- ware design certainly benefit from DI, the scope is much broader. The overall purpose is maintainability.

The idea of DI as a service modeled along the lines of a dictionary leads directly to the SERVICE LOCATOR anti-pattern. This is why I put so much emphasis on the need to clear your mind of even the most basic assumptions. After all, when we’re talking about dictionaries, we’re talking about stuff that belongs in the “reptile brain of programming.”

DI or IoC

DI is a subset of IoC. Inversion of Control simply refers to any style of programming where a framework or runtime controls program flow e.g. ASP.NET - where the ASP.NET page lifecycle controls the flow. Nowadays, the term IoC is often used to refer specifically to IoC over dependencies, or Dependency Injection. However IoC is actually much broader.

To Read