Dependency Injection in .NET, Second Edition
Steven van Deursen and Mark Seemann
  • MEAP began April 2017
  • Publication in January 2018 (estimated)
  • ISBN 9781617294730
  • 475 pages (estimated)
  • printed in black & white


An eBook copy of the previous edition, Dependency Injection in .NET (First Edition), is included at no additional cost. It will be automatically added to your Manning Bookshelf within 24 hours of purchase.

Dependency Injection in .NET, Second Edition teaches you to use Dependency Injection 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. Then, you'll work through concrete scenarios using C# and the .NET framework to implement DI in your own projects. As you dive into the thoroughly-explained examples, you'll develop a foundation you can apply to any of the many DI libraries for .NET and .NET Core.

"...this book has everything you need to make you an effective developer who will be building loosely coupled and highly maintainable applications."

~ Sergio Romero

"A solid 5!"

~ Björn Nordblom

"It is a rather well written book with a lot of patterns that are useful even for developers who are not using Dependency Injection itself."

~ Gustavo Gomes

Table of Contents detailed table of contents

Part 1: Putting Dependency Injection on the map

1. The Basics of Dependency Injection: What, Why and How

1.1. Writing maintainable code

1.1.1. Common Myths About DI

1.1.2. Understanding the purpose of DI

1.2. A Simple Example : Hello DI

1.2.1. Hello DI code

1.2.2. Benefits of DI

1.3. What to inject and what not to inject

1.3.1. Stable Dependencies

1.3.2. Volatile Dependencies

1.4. DI scope

1.4.1. Object Composition

1.4.2. Object Lifetime

1.4.3. Interception

1.4.4. DI in three dimensions

1.5. Conclusion

1.6. Summary

2. Writing tightly coupled code

2.1. Building a tightly coupled application

2.1.1. Meet Mary Rowan

2.1.2. Data Layer

2.1.3. Domain Layer

2.2. Evaluation

2.2.1. Dependency Graph

2.2.2. New User Interface

2.2.3. New Data Access Layer

2.2.4. Other Combinations

2.3. Analysis

2.3.1. Dependency Graph Analysis

2.3.2. Data Access Interface Analysis

2.3.3. Miscellaneous Other Issues

2.4. Conclusion

2.5. Summary

3. Writing loosely coupled code

3.1. Rebuilding the commerce application

3.1.1. Building a more maintainable User Interface

3.1.2. Building an independent Domain Model

3.1.3. Building a new Data Access layer

3.1.4. Composing the application in the Composition Root

3.2. Analyzing the loosely coupled implementation

3.2.1. Understanding the Interaction between Components

3.2.2. Analyzing the new Dependency Graph

3.3. Summary

Part 2: Catalog

4. DI Patterns

4.1. Composition Root

4.1.1. How Composition Root works

4.1.2. Using a DI Container in a Composition Root

4.1.3. Example: Implementing a Composition Root using Pure DI

4.1.4. The apparent dependency explosion

4.2. Constructor Injection

4.2.1. How Constructor Injection works

4.2.2. When to use Constructor Injection

4.2.3. Known use of Constructor injection

4.3. Method Injection

4.3.1. How Method Injection works

4.3.2. When to use Method Injection

4.3.3. Known use of Method Injection

4.3.4. Example: Adding currency conversions to the Product entity

4.4. Property Injection

4.4.1. How Property Injection works

4.4.2. When to use Property Injection

4.4.3. Known use of Property Injection

4.4.4. Example: Property Injection as extensibility model of a reusable library

4.5. Choosing which pattern to use

4.6. Summary

5. DI Anti-Patterns

5.1. Control Freak anti-pattern: Holding on too tight to volatile dependencies

5.1.1. Example: Control Freak though newing up Dependencies

5.1.2. Example: Control Freak though Factory

5.1.3. Example: Control Freak through overloaded constructors

5.1.4. Analysis of Control Freak

5.2. Service Locator anti-pattern: Supplying classes with access to an unbound set of Dependencies

5.2.1. Example: ProductService using a Service Locator

5.2.2. Analysis of Service Locator

5.3. Ambient Context anti-pattern: Supplying classes with global access to a Dependency

5.3.1. Example: Accessing system time through Ambient Context

5.3.2. Example: Logging through Ambient Context

5.3.3. Analysis of Ambient Context

5.4. Constrained Construction anti-pattern: Forcing a class’s constructor to have a particular signature

5.4.1. Example: late-binding ProductRepository

5.4.2. Analysis of Constrained Construction

5.5. Summary

6. Code Smells

6.1. Dealing with the Constructor Over-injection code smell

6.1.1. Recognizing Constructor Over-injection

6.1.2. Refactoring from Constructor Over-injection to Facade Services

6.1.3. Refactoring from Constructor Over-injection to Domain Events

6.2. Abuse of Abstract Factories

6.2.1. Abuse of Abstract Factories to overcome lifetime problems

6.2.2. Abuse of Abstract Factories to select Dependencies based on runtime data

6.3. Fixing cyclic Dependencies

6.3.1. Example: Dependency cycle caused by SRP violation

6.3.2. Analysis of Mary’s Dependency cycle.

6.3.3. Refactoring from SRP violation to resolve the Dependency cycle

6.3.4. Common strategies for breaking Dependency cycles

6.3.5. Last resort: Breaking the cycle with Property Injection

6.4. Summary

Part 3: Pure DI

7. Application Composition

7.1. Composing console applications

7.1.1. Example: updating currencies using the UpdateCurrency Program

7.1.2. Building the Composition Root of the UpdateCurrency Program

7.1.3. Composing object graphs in CreateCurrencyParser

7.1.4. A closer look at the UpdateCurrency Program’s layering

7.2. Composing UWP applications

7.2.1. UWP Composition

7.2.2. Example: wiring up a product-management rich client

7.2.3. Implementing the Composition Root in the UWP application

7.3. Composing ASP.NET Core MVC applications

7.3.1. Creating a custom Controller Activator

7.3.2. Constructing custom middleware components using Pure DI

7.4. Summary

8. Object Lifetime

8.1. Managing Dependency Lifetime

8.1.1. Introducing Lifetime Management

8.1.2. Managing lifetime with Pure DI

8.2. Working with disposable Dependencies

8.2.1. Consuming disposable Dependencies

8.2.2. Managing disposable Dependencies

8.3. Lifestyle catalog

8.3.1. Using the Singleton lifestyle to cache instances for the lifetime of a Composer

8.3.2. Using the Transient lifestyle to return a new instance on every request

8.3.3. Using the Scoped lifestyle to share instances within a single well-defined scope

8.3.4. Using the Per Graph lifestyle to reuse instances throughout an object graph

8.4. Bad lifestyle choices

8.4.1. Captive Dependencies: Keeping Dependencies referenced beyond their expected lifetime

8.4.2. Using Leaky Abstractions to leak lifestyle choices to consumers

8.4.3. Causing concurrency bugs by tying instances to the lifetime of a thread

8.5. Summary

9. Interception

10. Aspect-Oriented Programming

Part 4: DI Containers

11. DI Containers introduction

12. Autofac

13. Simple Injector

14. .NET Core DI

About the Technology

Dependency Injection is an application design pattern where components are loosely-coupled with the objects or data on which they have a dependency. The dependent component broadcasts its need, and an external service composes the objects, or "injects" the "dependency". This basic pattern allows objects to work together without being bound into rigid, pre-defined architectures and communication. Dependency Injection has become one of the most dominant practices in the past decade, and it's now required knowledge for .NET developers.

What's inside

  • DI patterns and anti-patterns
  • Aspect-oriented programming
  • Integration with common .NET application frameworks
  • Using Microsoft and open source DI libraries and containers

About the reader

This book is written for intermediate .NET developers and beyond. No previous experience with DI or DI frameworks is required.

About the authors

.NET legend Mark Seemann has teamed up with developer and architect Steven van Deursen in revising and updating this classic book originally written by Mark. Steven is the author and maintainer of a popular DI library for .NET called Simple Injector and is an active voice in the Dependency Injection community. Mark Seemann remains a regular speaker at international software development conferences.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook + liveBook
MEAP eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks