Book description
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 Technology
Dependency 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 the Book
Dependency 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.
What's Inside
- Many C#-based examples
- A catalog of DI patterns and anti-patterns
- Using both Microsoft and open source DI frameworks
About the Reader
This book is written for C# developers. No previous experience with DI or DI frameworks is required.
About the Author
Mark Seemann is a software architect living in Copenhagen. Previously a developer and architect at Microsoft, Mark is now an independent consultant.
Quotes
Realistic examples keep the big picture in focus...A real treat.
- Glenn Block, Microsoft
Well-written, thoughtful, easy to follow, and...timeless.
- David Barkol, Neudesic, LLC
Fills a huge need for .NET designers.
- Paul Grebenc, PCA Services, Inc.
Takes the mystery out of a mystifying topic.
- Rama Krishna, 3C Software
All you need to know...and more!
- Jonas Bandi, TechTalk
Publisher resources
Table of contents
- inside front cover
- Dependency Injection
- Copyright
- Dedication
- Brief Table of Contents
- Table of Contents
- front matter
- Part 1. Putting Dependency Injection on the map
-
Chapter 1. A Dependency Injection tasting menu
- 1.1. Writing maintainable code
- 1.1.1. Unlearning DI
- 1.1.2. Understanding the purpose of DI
- 1.2. 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. Seams
- 1.3.2. Stable Dependencies
- 1.3.3. 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. Summary
-
Chapter 2. A comprehensive example
- 2.1. Doing it wrong
- 2.1.1. Building a tightly coupled application
- 2.1.2. Smoke test
- 2.1.3. Evaluation
- 2.1.4. Analysis
- 2.2. Doing it right
- 2.2.1. Rebuilding the commerce application
- 2.2.2. Analyzing the loosely coupled implementation
- 2.3. Expanding the sample application
- 2.3.1. Architecture
- 2.3.2. Basket feature
- 2.4. Summary
-
Chapter 3. DI Containers
- 3.1. Introducing DI Containers
- 3.1.1. Hello container
- 3.1.2. Auto-wiring
- 3.2. Configuring DI Containers
- 3.2.1. Configuring containers with XML
- 3.2.2. Configuring containers with code
- 3.2.3. Configuring containers by convention
- 3.3. DI Container patterns
- 3.3.1. Composition Root
- 3.3.2. Register Resolve Release
- 3.4. DI Container landscape
- 3.4.1. Selecting a DI Container
- 3.4.2. Microsoft and DI
- 3.5. Summary
- Part 2. DI catalog
-
Chapter 4. DI patterns
- 4.1. Constructor Injection
- 4.1.1. How it works
- 4.1.2. When to use it
- 4.1.3. Known use
- 4.1.4. Example: Adding a currency provider to the shopping basket
- 4.1.5. Related patterns
- 4.2. Property Injection
- 4.2.1. How it works
- 4.2.2. When to use it
- 4.2.3. Known use
- 4.2.4. Example: Defining a currency profile service for the BasketController
- 4.2.5. Related patterns
- 4.3. Method Injection
- 4.3.1. How it works
- 4.3.2. When to use it
- 4.3.3. Known use
- 4.3.4. Example: Converting baskets
- 4.3.5. Related patterns
- 4.4. Ambient Context
- 4.4.1. How it works
- 4.4.2. When to use it
- 4.4.3. Known use
- 4.4.4. Example: Caching Currency
- 4.4.5. Related patterns
- 4.5. Summary
-
Chapter 5. DI anti-patterns
- 5.1. Control Freak
- 5.1.1. Example: newing up Dependencies
- 5.1.2. Example: Factory
- 5.1.3. Analysis
- 5.2. Bastard Injection
- 5.2.1. Example: ProductService with Foreign Default
- 5.2.2. Analysis
- 5.3. Constrained Construction
- 5.3.1. Example: late-binding ProductRepository
- 5.3.2. Analysis
- 5.4. Service Locator
- 5.4.1. Example: ProductService using a Service Locator
- 5.4.2. Analysis
- 5.5. Summary
-
Chapter 6. DI refactorings
- 6.1. Mapping runtime values to Abstractions
- 6.1.1. Abstractions with runtime Dependencies
- 6.1.2. Example: selecting a routing algorithm
- 6.1.3. Example: using a CurrencyProvider
- 6.2. Working with short-lived Dependencies
- 6.2.1. Closing connections through Abstractions
- 6.2.2. Example: invoking a product-management service
- 6.3. Resolving cyclic Dependencies
- 6.3.1. Addressing Dependency cycles
- 6.3.2. Example: composing a window
- 6.4. Dealing with Constructor Over-injection
- 6.4.1. Recognizing and addressing Constructor Over-injection
- 6.4.2. Example: refactoring order reception
- 6.5. Monitoring coupling
- 6.5.1. Unit-testing coupling
- 6.5.2. Integration-testing coupling
- 6.5.3. Using NDepend to monitor coupling
- 6.6. Summary
- Part 3. DIY DI
-
Chapter 7. Object Composition
- 7.1. Composing console applications
- 7.1.1. Example: updating currencies
- 7.2. Composing ASP.NET MVC applications
- 7.2.1. ASP.NET MVC extensibility
- 7.2.2. Example: implementing CommerceControllerFactory
- 7.3. Composing WCF applications
- 7.3.1. WCF extensibility
- 7.3.2. Example: wiring up a product-management service
- 7.4. Composing WPF applications
- 7.4.1. WPF Composition
- 7.4.2. Example: wiring up a product-management rich client
- 7.5. Composing ASP.NET applications
- 7.5.1. ASP.NET composition
- 7.5.2. Example: wiring up a CampaignPresenter
- 7.6. Composing PowerShell cmdlets
- 7.6.1. Example: composing basket-management cmdlets
- 7.7. Summary
-
Chapter 8. Object Lifetime
- 8.1. Managing Dependency Lifetime
- 8.1.1. Introducing Lifetime Management
- 8.1.2. Managing lifetime with a container
- 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. Singleton
- 8.3.2. Transient
- 8.3.3. Per Graph
- 8.3.4. Web Request Context
- 8.3.5. Pooled
- 8.3.6. Other lifestyles
- 8.4. Summary
-
Chapter 9. Interception
- 9.1. Introducing Interception
- 9.1.1. Example: implementing auditing
- 9.1.2. Patterns and principles for Interception
- 9.2. Implementing Cross-Cutting Concerns
- 9.2.1. Intercepting with a Circuit Breaker
- 9.2.2. Handling exceptions
- 9.2.3. Adding security
- 9.3. Declaring aspects
- 9.3.1. Using attributes to declare aspects
- 9.3.2. Applying dynamic Interception
- 9.3.3. Example: intercepting with Windsor
- 9.4. Summary
- Part 4. DI Containers
-
Chapter 10. Castle Windsor
- 10.1. Introducing Castle Windsor
- 10.1.1. Resolving objects
- 10.1.2. Configuring the container
- 10.1.3. Packaging configuration
- 10.2. Managing lifetime
- 10.2.1. Configuring lifestyle
- 10.2.2. Using advanced lifestyles
- 10.2.3. Developing a custom lifestyle
- 10.3. Working with multiple components
- 10.3.1. Selecting among multiple candidates
- 10.3.2. Wiring sequences
- 10.3.3. Wiring Decorators
- 10.4. Configuring difficult APIs
- 10.4.1. Configuring primitive Dependencies
- 10.4.2. Registering components with code blocks
- 10.4.3. Wiring with Property Injection
- 10.5. Summary
-
Chapter 11. StructureMap
- 11.1. Introducing StructureMap
- 11.1.1. Resolving objects
- 11.1.2. Configuring the container
- 11.1.3. Packaging configuration
- 11.2. Managing lifetime
- 11.2.1. Configuring lifestyles
- 11.2.2. Developing a custom lifestyle
- 11.3. Working with multiple components
- 11.3.1. Selecting among multiple candidates
- 11.3.2. Wiring sequences
- 11.3.3. Wiring Decorators
- 11.4. Configuring difficult APIs
- 11.4.1. Configuring primitive Dependencies
- 11.4.2. Creating objects with code blocks
- 11.4.3. Wiring with Property Injection
- 11.5. Summary
-
Chapter 12. Spring.NET
- 12.1. Introducing Spring.NET
- 12.1.1. Resolving objects
- 12.1.2. Configuring the container
- 12.1.3. Loading XML
- 12.2. Managing lifetime
- 12.2.1. Configuring object scopes
- 12.3. Working with multiple components
- 12.3.1. Selecting among multiple candidates
- 12.3.2. Wiring sequences
- 12.3.3. Wiring Decorators
- 12.3.4. Creating Interceptors
- 12.4. Configuring difficult APIs
- 12.4.1. Configuring primitive Dependencies
- 12.4.2. Configuring static factories
- 12.4.3. Wiring with Property Injection
- 12.5. Summary
-
Chapter 13. Autofac
- 13.1. Introducing Autofac
- 13.1.1. Resolving objects
- 13.1.2. Configuring the ContainerBuilder
- 13.1.3. Packaging configuration
- 13.2. Managing lifetime
- 13.2.1. Configuring instance scope
- 13.3. Working with multiple components
- 13.3.1. Selecting among multiple candidates
- 13.3.2. Wiring sequences
- 13.3.3. Wiring Decorators
- 13.4. Registering difficult APIs
- 13.4.1. Configuring primitive Dependencies
- 13.4.2. Registering objects with code blocks
- 13.4.3. Wiring with Property Injection
- 13.5. Summary
-
Chapter 14. Unity
- 14.1. Introducing Unity
- 14.1.1. Resolving objects
- 14.1.2. Configuring the container
- 14.1.3. Packaging configuration
- 14.2. Managing lifetime
- 14.2.1. Configuring lifetime
- 14.2.2. Developing a custom lifetime
- 14.3. Working with multiple components
- 14.3.1. Selecting among multiple candidates
- 14.3.2. Wiring sequences
- 14.3.3. Wiring Decorators
- 14.3.4. Creating Interceptors
- 14.4. Configuring difficult APIs
- 14.4.1. Configuring primitive Dependencies
- 14.4.2. Registering components with code blocks
- 14.4.3. Wiring with Property Injection
- 14.5. Summary
-
Chapter 15. MEF
- 15.1. Introducing MEF
- 15.1.1. Resolving objects
- 15.1.2. Defining imports and exports
- 15.1.3. Working with catalogs
- 15.2. Managing lifetime
- 15.2.1. Declaring creation policy
- 15.2.2. Releasing objects
- 15.3. Working with multiple components
- 15.3.1. Selecting among multiple candidates
- 15.3.2. Wiring sequences
- 15.3.3. Wiring Decorators
- 15.4. Composing difficult APIs
- 15.4.1. Compositing primitive parts
- 15.4.2. Composing parts with non-public constructors
- 15.4.3. Wiring with Property Injection
- 15.5. Summary
- Resources
-
Glossary
- Abstraction
- Ambient Context
- Aspect-Oriented Programming (AOP)
- Auto-wiring
- Auto-registration
- Bastard Injection
- Code as Configuration
- Composer
- Composition Root
- Constrained Construction
- Constructor Injection
- Control Freak
- Convention-based Configuration
- Cross-Cutting Concern
- Dependency
- DI Container
- DTO
- Entity
- Foreign Default
- Humble Object
- Interception
- Inversion of Control
- Leaky Abstraction
- Lifetime Management
- Liskov Substitution Principle
- Local Default
- Method Injection
- Object Composition
- Object Lifetime
- Open/Closed Principle
- Per Graph Lifestyle
- Pooled Lifestyle
- Poor Man’s DI
- Property Injection
- Register Resolve Release
- Seam
- Service Locator
- Single Responsibility Principle
- Singleton Lifestyle
- Solid
- Stable Dependency
- Testability
- Transient Lifestyle
- Volatile Dependency
- Web Request Context Lifestyle
- inside back cover
- Index
- List of Figures
- List of Tables
- List of Listings
Product information
- Title: Dependency Injection in .NET
- Author(s):
- Release date: October 2011
- Publisher(s): Manning Publications
- ISBN: 9781935182504
You might also like
book
Software Architecture by Example: Using C# and .NET
Design system solutions using modern architectural patterns and practices. This book discusses methods to keep a …
book
Hands-On Network Programming with C# and .NET Core
A comprehensive guide to understanding network architecture, communication protocols, and network analysis to build secure applications …
book
Microservices in .NET, Second Edition
In Microservices in .NET, Second Edition you will learn how to: Build scalable microservices that are …
book
Hands-On Design Patterns with C# and .NET Core
Apply design patterns to solve problems in software architecture and programming using C# 7.x and .NET …