Jump to ratings and reviews
Rate this book

Dependency Injection Principles, Practices, and Patterns

Rate this book
Summary Dependency Injection in .NET presents core DI patterns in plain C# so you'll fully understand how DI works, covers integration with standard Microsoft technologies like ASP.NET MVC, and teaches you to use DI frameworks like Structure Map, Castle Windsor, and Unity. About the TechnologyDependency Injection is a great way to reduce tight coupling between software components. Instead of hard-coding dependencies, such as specifying a database driver, you inject a list of services that a component may need. The services are then connected by a third party. This technique enables you to better manage future changes and other complexity in your software. About this BookDependency Injection in .NET introduces DI and provides a practical guide for applying it in .NET applications. The book presents the core patterns in plain C#, so you'll fully understand how DI works. Then you'll learn to integrate DI with standard Microsoft technologies like ASP.NET MVC, and to use DI frameworks like StructureMap, Castle Windsor, and Unity. By the end of the book, you'll be comfortable applying this powerful technique in your everyday .NET development.This book is written for C# developers. No previous experience with DI or DI frameworks is required. What's InsideMany C#-based examples A catalog of DI patterns and anti-patterns Using both Microsoft and open source DI frameworksTabel of ContentsPART 1 PUTTING DEPENDENCY INJECTION ON THE MAP A Dependency Injection tasting menu A comprehensive example DI Containers PART 2 DI CATALOG DI patterns DI anti-patterns DI refactorings PART 3 DIY DI Object Composition Object Lifetime Interception PART 4 DI CONTAINERS Castle Windsor StructureMap Spring.NET Autofac Unity MEF

552 pages, Unknown Binding

First published September 28, 2011

167 people are currently reading
1291 people want to read

About the author

Mark Seemann

3 books483 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
429 (55%)
4 stars
269 (34%)
3 stars
61 (7%)
2 stars
17 (2%)
1 star
1 (<1%)
Displaying 1 - 30 of 84 reviews
Profile Image for Christian Dalager.
160 reviews11 followers
January 25, 2012
Not just a good DI Container book, but a great OO software design book, building on top of other great classics.

Even in the last chapters going through the different containers, Seemann continues to provide examples that makes you smarter.

And never have I built up an appetite like that for Sauce Bearnaise in a book about programming.

I'll keep this close to my desk.
Profile Image for Arek.
1 review1 follower
March 30, 2019
Excellent book! One of the best you can find regarding software design principles.
Profile Image for Brett.
18 reviews
May 17, 2013
Seemann did a fantastic job presenting Dependency Injection in concept and practice with several different .NET frameworks. Perfect length and structure for a quick read, but also deep enough for my long-term reference on the subject.

Wish all my technical literature was produced like this.
Profile Image for Ismail Mayat.
95 reviews11 followers
November 11, 2019
This is a must read, love how authors show how DI leads to clean code based on SOLID principles. Some real eye openers let's put it this way I won't be accidently making volatile dependencies singletons ever again!!
Profile Image for Cristian.
41 reviews
February 16, 2024
Though called Dependency Injection, the book is much more than that: A guide to good practices in object-oriented programming. The authors analyze usual practices against the SOLID principles and others, such as Command Query Segregation (CQS) and Reused Abstraction Principle. It has many references to other books and blogs from where the ideas originate or where more in-depth discussion can be found. Though all the book is high quality, the cherry on the top is chapter 10, starting with an example of an all-too-common Service interface, how it violates 3 out of 5 SOLID principles, and how to correct this by refactoring it following those same principles and CQS, reaching a set of general interfaces for queries and commands that enable aspect oriented programming without the need of tooling (using Pure DI, i.e. dependency injection without a DI container). These set of interfaces for a command query responsibility segregation (CQRS) pattern seem to be somewhat known in the C# world, but not that common in Java as far as my experience goes.

However, though hesitant, I cannot give it 5 stars. In part because it is too C# centric, and longer than it needs to be. Part 4 consists of a long, repetitive comparison of 3 DI frameworks in C#. It could for the most part be removed, lowering the lenght of the book by around 150 pages. I can see it being useful for someone trying to choose which DI framework to use in C#, but to any other reader not needing to have an in-depth comparison of those 3 C# DI frameworks, it just bloats the book. For someone not coming from a C# background, reading through it was mostly tedious without much gain.

Aside from that, some other things merit discussion. The authors add guard clauses to constructor arguments in most examples at the beginning of the book. Though I agree with the spirit (attempting to disallow invalid state by prohibiting null for required dependencies), the guard clauses for constructors are mostly unnecessary and even counter-productive. Unnecessary because (though not strictly required) the most common is using a DI container, not pure DI, and DI containers usually don't inject null unless explicitly told to do so. For instance, in Java, Spring will throw an exception if there is no appropriate dependency to autowire, not inject null. You can inject null, but then you need to explicitly do so (e.g. in an XML configuration, using @Autowired(required = false), or in some other way). So, for the most part, the guard clause is redundant. Counter-productive because it can be annoying when testing. Sometimes you want to test some subset of the functionality, for which not all the dependencies are needed. In that case, simply instantiating the class with some null dependencies is totally fine, and can also communicate "this dependency doesn't matter for this particular use-case". If all are covered by guard clauses, then we are forced to instantiate those dependencies, which might simply be "noise" for a particular test. I first heard this argument from Miško Hevery in one of his "Google Clean Code" talks.

Another thing that caught my attention were the repeated mentions in Part 4 about enums being a code smell. I get the point of dispatching on different classes instead of having if-else-if chains based on enum constants, but a lot of the times doing so simply overcomplicates things or mixes responsibilities, while enums are a perfectly fine solution.

But such things aren't to be taken into consideration for an otherwise wonderful book. Overall, I highly recommend it to new to mid-range developers, maybe even experienced ones looking for a coherent and fresh view on commonly known OOP best practices.
Profile Image for Iman Jahandideh.
13 reviews8 followers
October 16, 2020
I found the book too verbose and too long. It seems the book can be a lot less. Specially in the first parts where other topics like SOLID and Design Patterns are introduced. If someone already knows about these topics these section are just pure filler.

The book also failed to answer my main questions about Dependency injection, at least explicitly; Mainly how to deal with long composition chains and how to deal with dynamic creation of objects that need dependencies. I couldn't find any explicit discussion about these concerns in the book.

Also, for a book called "Dependency Injection Principles, Practices, and Patterns" the focus on examples in Web applications and .NET plugins and frameworks doesn't work well.
Profile Image for Sohrab Sardari.
2 reviews1 follower
October 9, 2024
I recommend this book to not only dotnet developers, but to all developers. good job mark!
1 review
September 10, 2017
I like to read books cover to cover, and as an experienced developer I found this one extremely verbose. It could have cut down the same info to half the size. If you use it more as a reference, the redudancy might be helpful.

The book did open my eyes to the concept of Composition Root, and the role of Abstract Factories that basically bridge the gap between your static object graph created at application start (the objects that would typically be Singletons in less enlightened architectures) and the runtime objects that flow through it.

The section about applying DI to various .NET frameworks is a very good reference that could save a lot of headaches. The same can be said to the chapters on the various DI containers, it's basically the most exhaustive comparison that has ever been done, you won't find the equivalent online.

Overall, due to the absence of competition I have to recommend it, but there's definitely room for a better book on dependency injection in .NET (2nd edition is in the works, who knows...).
112 reviews
September 15, 2020
This book is full of design pattern goodness - not just Dependency Injection(DI). In fact, they're great information on anti-patterns covered as well. Both authors leverage their extensive experience using DI on a bunch of different projects. As design pattern purists they chose to teach DI first without fancy DI containers or frameworks. Once the reader gets a full understanding of the patterns, and anti-patterns, involved here then finally the authors tackle the most popular .NET DI frameworks (Autofac and Simple Injector). I wish they would have covered Unity, but I get free access to the first edition book where they cover more tools.

Great book, I highly recommend it. Use DI whenever possible and feasible.
Profile Image for Sarah Dutkiewicz.
Author 2 books2 followers
April 14, 2023
This book gives a lot of insight into dependency injection and inversion of control - both in terms related to .NET and in general. It points out common patterns that work well with DI, code smells that make you want to look more carefully for potential problems, and anti-patterns that are more a problem than a solution. When pointing out patterns, he mentions common cases, pros, and cons. When pointing out code smells and anti-patterns, he talks of how to refactor and get the code in a better spot.

Even if you aren't one to read books like this from cover to cover, I recommend having this book as a reference for DI, good practices when working with DI, what to avoid, how to recover, and even some demos with common DI containers.
Profile Image for Dimitris Kokkinos.
1 review
April 7, 2018
A very well structured book. Despite the fact that it addresses a specific subject (dependency injection), it provides a much wider and deeper insight to the structuring of certain parts of an OO project.
It paints the whole picture of dependency injection (composition, lifetime management, interception) with a lot of examples and many principles to support them.
Additionally, it pays off to have this book as a reference, because it provides a lot of material for many well known DI containers.
Also, it has a whole section on how to avoid bad practices in this domain, which helps to get a better idea and develop intuition about dependency injection.
Profile Image for DHEERAJ Kumar.
15 reviews4 followers
May 14, 2018
This book is a rare gem, a must must read for OOP developers. Every line you read will make you smarter developer.
It will not only help you understanding Dependency Injection to the deepest, but also help you making better architecture your applications which would adhere to the principles and best practices.

I would recommend few prerequisites for beginners to make most of this book.
1. Understanding of C#
2. Basic understanding of Design Patterns.(Decorator, AbstractFactory, Facade, Singleton)
3. Understanding of OOP Concepts.
4. Understanding of Solid Principles.

Happy Coding!

Profile Image for Ivan Ivanov.
26 reviews
November 8, 2020
I liked the book. It explains DI very well. Also it shows some design patterns (Decorator, Command) and principles(SOLID, DRY, Command-Query) which can be used with DI. The result is a book about writing clean and maintainable code with the help of DI. There is a chapter for anti-patterns as well.

Even though the title is DI in .NET the book could be helpful to people using other technologies. However there are some sections specifc to .NET like DI libraries and some other sections focused on applying DI in ASP.NET Core and WPF.
624 reviews5 followers
January 7, 2019
I worked for a while this fall changing our C# web projects (ASP.NET Web API v2) to use dependency injection. I'd flipped through this book before, but it was much more relevant now.

This is a well written book that drives home the concepts at play for DI, and traps to avoid. I found the material helpful. It helped frame how I think about the structure of the code, and the DI work itself (apart from this book) has been paying dividends.
22 reviews
Read
November 8, 2020
This was my first introduction to dependency injection along with SOLID principles.
Along with Mark's Pluralsight course this is everything you need.
All the popular frameworks have been presented along with a comparison to others.
The initial chapters presenting the ideas are amazing.
Mark's blog on these principles are a must read.
Profile Image for Jeff Patterson.
106 reviews6 followers
November 4, 2021
Excellent. Although the book is older, I couldn't tell that it was dated. Everything in it still seemed current and applicable.
If you are interested in learning how to keep your programs maintainable and the classes "single responsibility," then read this book. I promise you will learn something interesting.
Profile Image for Dayton Outar.
113 reviews5 followers
March 4, 2022
The first 3 chapters basically summarizes what this book is about. All the other ensuing chapters fill out the details, which is a pretty effective strategy used by the Manning publishers for years now.

Chapter 10 was an amazing chapter for me. Loved the insights. This chapter proves the importance and usefulness of Design Patterns. It is another great reference and companion text to the GoF book, "Design Patterns: Elements of Reusable Object-Oriented Software".
5 reviews
December 23, 2023
A ton of great knowledge on Dependency Injection. The Why, the How. Plenty of code examples showing how using DI improves the design of your application. Some advance/though provoking design suggestions that you may want to consider moving forward.

It’s true the book is long, but you can always focus k n the sections you are more interested in.
Profile Image for Andrew.
43 reviews
July 20, 2018
Despite the second edition not being completed yet, this is an absolutely invaluable resource to any programmer interested in Object Oriented Design. The concepts are clearly communicated with focused examples that are general enough to be actually practically useful.

This book is a must-read.
Profile Image for Branislav Petrović.
5 reviews
September 23, 2022
Excellent book! For me, this is by far the best book for explaining dependency injection in theory and practice that I have come across so far. Beside DI, it also explains best practices for great software design embracing OOP and SOLID principles.
Profile Image for Asiful Nobel.
27 reviews
April 8, 2023
A very good book on the topic of dependency injection with thorough examples. I would have given this a 5 star, but the content seems to drag on a bit after chapter 6. In addition, the constant cooking references to add some humour into a book about a dry topic felt tiring.
Profile Image for Mateusz Stępniak.
52 reviews1 follower
October 1, 2017
The amazing AutoFixture library creator shares some of his deep knowledge. I enjoyed culinary metaphors.
Profile Image for Anton Yakovenko.
4 reviews
March 2, 2020
One of the most mind-changing books in my career. Definitely recommend if for all architects that want to create a beautiful product and not just code.
Profile Image for Lam Từ.
4 reviews1 follower
September 9, 2020
Indispensable for writing a maintainable and testable system.
Displaying 1 - 30 of 84 reviews

Can't find what you're looking for?

Get help and learn more about the design.