If you want to implement Dependency Injection in your application, this book is for you.
If you want to get into Dependency Injection, this book is for you.
If you don’t know which Dependency Injection’s Container to choose, this book is for you.
If haven’t heard about Dependency Injection before and you want to make your code loosely coupled, this book is for you.
If you are not interested in good practices at all and still want to write a bad code, this book is not for you.
Dependency Injection in .NET by Mark Seemann is a complete guide to DI in the .NET platform. It contains a lot of theoretical knowledge and practical instructions with many examples and instructions about using DI Containers.
Mark introduces to us an example project which is being created without knowledge about DI and shows with this how loose coupling can be misunderstood. During the next chapters we gain knowledge about DI basics and how to design good and scalable architecture for example project. Author enters into DI word with some basic requirements for DI Containers (dependency configuration and resolving, lifetime management). Next we are introduced with Composition Root and explanation why we should place there our dependencies’ configuration. We get into DI patterns (Constructor Injection, Property Injection, Method Injection and Ambient Context) and we will get knowledge how to choose the right one. We will also get into DI anti-patterns, which we all know for sure, but we didn’t give a name for them (Control Freak – too much new keyword) or we can meet them when we start to implement DI in our applications (Bastard Injection, Service Locator). We will know how to deal with complex problems during refactoring (constructor’s over-injection). We will also get knowledge about object’s lifetime management styles and how to choose a good option for us.
Practical examples are big part of this book. Besides e-commerce platform example there is a chapter instructing how to find Composition Root and how to configure container in different Microsoft’s frameworks (ASP.NET MVC, Web Forms, WPF, WCF, console application and event Power Shell script). A lot of place in the book is taken by DI Containers section where the most popular DI container libraries are analyzed and described. We can find there Castle Windsor, Structure Map, Sprint.NET, AutoFac, Unity and MEF (which is not really a DI Container).
Uncle Bob in Clean Code: A Handbook of Agile Software Craftsmanship mentioned that the setup is a global concern and object should not be responsible for instantiating dependencies itself. Otherwise Single Responsibility Principle is violated. Dependency management should be moved to the one common place in our application. Mark Seemann teaches us how to do this and solve setup problem.
This book is written in easy to understand way. Even for developers who knows nothing about DI. There are a lot of references to books and blog posts so it is easy to learn more about patterns which we don’t know.
I truly recommend this book. After lecture you will think twice before using new keyword.