Skip to content

Book Review: Dependency Injection

Dependency Injection

Principles, Practices, and Patterns

Dependency Injection
Principles, Practices, and Patterns

Steven van Deursen

Mark Seeman

Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components.

Buy Now

Many developers would probably overlook this book, purely based on the title alone. I know I did. The thought process behind this would be, what more do I need to know about dependency injection after I've wired up my dependency in Program.cs and have my dependency appear when I need it. Simple right!

That is just one very simple aspect of Dependency Injection, but also as you'll undoubtedly discover reading this book there are is a whole lot more to know and this book is jam packed with a whole load of practical tips and insights to help you write better code.

One of the reviews on the back cover of this book states

Actually three books in one: a really good introduction to DI in .net, an even better one to DI in general and an absolutely excellent introduction to OO principles and software design

Mikkel Arentoft, Danske Bank

This is exactly what you'll get from this book, and I would go as far as too say, it really doesn't matter what level of developer you are, you will undoubtedly learn to become an even better one after reading this book.

Dependency Injection is one aspect and dependency inversion is another. The two are not exactly the same thing.

Why I read this book

Truth be told, I was looking for a book that would guide me through the three different common lifestyles of .net DI Singleton, Transient and Scoped. More specifically I was primarily looking to understand the Advantages and Disadvantages of each and when to leverage the best of them.

I was working on a large application stack that seem to be leveraging different lifestyle strategies which seem to be having an impact in the application performance, but I could figure out why. I found the answer in this book, but I also found a number of the anti-patterns defined in the book within the code base!

It was during this time that I stumbled across the Dependency Injection Book Reviewed from Steve Smith, one of the dotnet bloggers I follow, which praised and recommended the book.

When I was about to start initially reading the book, I initially thought I'd skip the first few chapters as they seemed to be from the contents description to be stuff that I already knew. However, it being a dark and cold mid November evening with nothing else to do but read a book, I thought I'd just start reading them anyway. All I can say, is I'm glad I did because straight from the off I was taken in by the authors writing style and even picked a few useful nuggets of information.

If you've read books like Adaptive Code , Clean Code or even Pragmatic Programmer, then most likely you'll undoubtedly know a lot the information and have experience in implementing it, but I still found the chapters in part 1 of the book well worth the time invested.

The authors also take time to mention in the opening chapters that if you think you know DI, then you should attempt to read the book as if you know nothing about DI, and I can certainly recommend that you follow their advice. I think I managed to extract more value from the book, just by following that simple piece of advice

Applying principles from this book, will help you create code that accommodates new requirements and unforeseen scenarios without significant rewrites.

I came to realise that many of the things I thought I'd understood about Dependency Injection were just almost right, but thanks to to this book I completely understand them now.

Chapter 4 provides a good pattern decision process, while chapter 5 and 6 are really helpful for intermediate developers, not only author shows anti-patterns, but also refactoring techniques to overcome them. 

Chapter 5 is also really illuminating understanding the different Dependency Injection anti-patterns.

The chapter I learned most from chapter 8, Object Lifetime. Learning to understand how to manage the dependency lifetime with the needed clarity has been particularly useful when trouble shooting performance problems in applications.

Why I like this book

Despite the subject, the book is a nice easy read and in parts can be really entertaining. The authors do a great job of balancing between being a great reference book and an entertaining read for software developers.

The authors also do a great job presenting Dependency Injection in concept and practice with several different .NET frameworks and how DI leads to clean code based on SOLID principles.

Personally, I was introduced to the concept of Composition Root, and the role of Abstract Factories that bridge the gap between your static object graph created at application start.

Why I recommend this book

If you're a developer who is learning about Software Design Patterns, then you find this book a great reference and companion text to the GoF book, Design Patterns: Elements of Reusable Object-Oriented Software

23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves

This is great resource to learn from and provide insights into the principles of dependency injection without getting lost in the various implementation details of various libraries. The books content is mostly focused around the implementation of design patterns and how dependency injection facilitates these best practices. Providing guidance regarding what patterns and tools to use in which situations has continued to improve, and is both clear-headed and actionable.

Be warned however, that this book will most likely inspire you to revisit your old projects and most likely inspire you to start refactoring a lot of your code because you will have learned a whole new set of skills and fundamental knowledge of software development best practices, helping to improve your software design and architecture skills.

Dependency Injection Principles, Practices, and Patterns

 teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. 

Gary Woodfine
Latest posts by Gary Woodfine (see all)