Dependency Injection in .NET

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

View/Submit Errata

Table of contents

  1. inside front cover
    1. Glossary conceptual map
  2. Dependency Injection
  3. Copyright
  4. Dedication
  5. Brief Table of Contents
  6. Table of Contents
  7. front matter
    1. Foreword
    2. Preface
    3. Acknowledgments
    4. About this Book
    5. Who should read this book?
    6. Roadmap
    7. Code conventions and downloads
    8. Author Online
    9. About the author
    10. About the Cover Illustration
  8. Part 1. Putting Dependency Injection on the map
  9. Chapter 1. A Dependency Injection tasting menu
    1. 1.1. Writing maintainable code
    2. 1.1.1. Unlearning DI
    3. 1.1.2. Understanding the purpose of DI
    4. 1.2. Hello DI
    5. 1.2.1. Hello DI code
    6. 1.2.2. Benefits of DI
    7. 1.3. What to inject and what not to inject
    8. 1.3.1. Seams
    9. 1.3.2. Stable Dependencies
    10. 1.3.3. Volatile Dependencies
    11. 1.4. DI scope
    12. 1.4.1. Object Composition
    13. 1.4.2. Object Lifetime
    14. 1.4.3. Interception
    15. 1.4.4. DI in three dimensions
    16. 1.5. Summary
  10. Chapter 2. A comprehensive example
    1. 2.1. Doing it wrong
    2. 2.1.1. Building a tightly coupled application
    3. 2.1.2. Smoke test
    4. 2.1.3. Evaluation
    5. 2.1.4. Analysis
    6. 2.2. Doing it right
    7. 2.2.1. Rebuilding the commerce application
    8. 2.2.2. Analyzing the loosely coupled implementation
    9. 2.3. Expanding the sample application
    10. 2.3.1. Architecture
    11. 2.3.2. Basket feature
    12. 2.4. Summary
  11. Chapter 3. DI Containers
    1. 3.1. Introducing DI Containers
    2. 3.1.1. Hello container
    3. 3.1.2. Auto-wiring
    4. 3.2. Configuring DI Containers
    5. 3.2.1. Configuring containers with XML
    6. 3.2.2. Configuring containers with code
    7. 3.2.3. Configuring containers by convention
    8. 3.3. DI Container patterns
    9. 3.3.1. Composition Root
    10. 3.3.2. Register Resolve Release
    11. 3.4. DI Container landscape
    12. 3.4.1. Selecting a DI Container
    13. 3.4.2. Microsoft and DI
    14. 3.5. Summary
  12. Part 2. DI catalog
  13. Chapter 4. DI patterns
    1. 4.1. Constructor Injection
    2. 4.1.1. How it works
    3. 4.1.2. When to use it
    4. 4.1.3. Known use
    5. 4.1.4. Example: Adding a currency provider to the shopping basket
    6. 4.1.5. Related patterns
    7. 4.2. Property Injection
    8. 4.2.1. How it works
    9. 4.2.2. When to use it
    10. 4.2.3. Known use
    11. 4.2.4. Example: Defining a currency profile service for the BasketController
    12. 4.2.5. Related patterns
    13. 4.3. Method Injection
    14. 4.3.1. How it works
    15. 4.3.2. When to use it
    16. 4.3.3. Known use
    17. 4.3.4. Example: Converting baskets
    18. 4.3.5. Related patterns
    19. 4.4. Ambient Context
    20. 4.4.1. How it works
    21. 4.4.2. When to use it
    22. 4.4.3. Known use
    23. 4.4.4. Example: Caching Currency
    24. 4.4.5. Related patterns
    25. 4.5. Summary
  14. Chapter 5. DI anti-patterns
    1. 5.1. Control Freak
    2. 5.1.1. Example: newing up Dependencies
    3. 5.1.2. Example: Factory
    4. 5.1.3. Analysis
    5. 5.2. Bastard Injection
    6. 5.2.1. Example: ProductService with Foreign Default
    7. 5.2.2. Analysis
    8. 5.3. Constrained Construction
    9. 5.3.1. Example: late-binding ProductRepository
    10. 5.3.2. Analysis
    11. 5.4. Service Locator
    12. 5.4.1. Example: ProductService using a Service Locator
    13. 5.4.2. Analysis
    14. 5.5. Summary
  15. Chapter 6. DI refactorings
    1. 6.1. Mapping runtime values to Abstractions
    2. 6.1.1. Abstractions with runtime Dependencies
    3. 6.1.2. Example: selecting a routing algorithm
    4. 6.1.3. Example: using a CurrencyProvider
    5. 6.2. Working with short-lived Dependencies
    6. 6.2.1. Closing connections through Abstractions
    7. 6.2.2. Example: invoking a product-management service
    8. 6.3. Resolving cyclic Dependencies
    9. 6.3.1. Addressing Dependency cycles
    10. 6.3.2. Example: composing a window
    11. 6.4. Dealing with Constructor Over-injection
    12. 6.4.1. Recognizing and addressing Constructor Over-injection
    13. 6.4.2. Example: refactoring order reception
    14. 6.5. Monitoring coupling
    15. 6.5.1. Unit-testing coupling
    16. 6.5.2. Integration-testing coupling
    17. 6.5.3. Using NDepend to monitor coupling
    18. 6.6. Summary
  16. Part 3. DIY DI
  17. Chapter 7. Object Composition
    1. 7.1. Composing console applications
    2. 7.1.1. Example: updating currencies
    3. 7.2. Composing ASP.NET MVC applications
    4. 7.2.1. ASP.NET MVC extensibility
    5. 7.2.2. Example: implementing CommerceControllerFactory
    6. 7.3. Composing WCF applications
    7. 7.3.1. WCF extensibility
    8. 7.3.2. Example: wiring up a product-management service
    9. 7.4. Composing WPF applications
    10. 7.4.1. WPF Composition
    11. 7.4.2. Example: wiring up a product-management rich client
    12. 7.5. Composing ASP.NET applications
    13. 7.5.1. ASP.NET composition
    14. 7.5.2. Example: wiring up a CampaignPresenter
    15. 7.6. Composing PowerShell cmdlets
    16. 7.6.1. Example: composing basket-management cmdlets
    17. 7.7. Summary
  18. Chapter 8. Object Lifetime
    1. 8.1. Managing Dependency Lifetime
    2. 8.1.1. Introducing Lifetime Management
    3. 8.1.2. Managing lifetime with a container
    4. 8.2. Working with disposable Dependencies
    5. 8.2.1. Consuming disposable Dependencies
    6. 8.2.2. Managing disposable Dependencies
    7. 8.3. Lifestyle catalog
    8. 8.3.1. Singleton
    9. 8.3.2. Transient
    10. 8.3.3. Per Graph
    11. 8.3.4. Web Request Context
    12. 8.3.5. Pooled
    13. 8.3.6. Other lifestyles
    14. 8.4. Summary
  19. Chapter 9. Interception
    1. 9.1. Introducing Interception
    2. 9.1.1. Example: implementing auditing
    3. 9.1.2. Patterns and principles for Interception
    4. 9.2. Implementing Cross-Cutting Concerns
    5. 9.2.1. Intercepting with a Circuit Breaker
    6. 9.2.2. Handling exceptions
    7. 9.2.3. Adding security
    8. 9.3. Declaring aspects
    9. 9.3.1. Using attributes to declare aspects
    10. 9.3.2. Applying dynamic Interception
    11. 9.3.3. Example: intercepting with Windsor
    12. 9.4. Summary
  20. Part 4. DI Containers
  21. Chapter 10. Castle Windsor
    1. 10.1. Introducing Castle Windsor
    2. 10.1.1. Resolving objects
    3. 10.1.2. Configuring the container
    4. 10.1.3. Packaging configuration
    5. 10.2. Managing lifetime
    6. 10.2.1. Configuring lifestyle
    7. 10.2.2. Using advanced lifestyles
    8. 10.2.3. Developing a custom lifestyle
    9. 10.3. Working with multiple components
    10. 10.3.1. Selecting among multiple candidates
    11. 10.3.2. Wiring sequences
    12. 10.3.3. Wiring Decorators
    13. 10.4. Configuring difficult APIs
    14. 10.4.1. Configuring primitive Dependencies
    15. 10.4.2. Registering components with code blocks
    16. 10.4.3. Wiring with Property Injection
    17. 10.5. Summary
  22. Chapter 11. StructureMap
    1. 11.1. Introducing StructureMap
    2. 11.1.1. Resolving objects
    3. 11.1.2. Configuring the container
    4. 11.1.3. Packaging configuration
    5. 11.2. Managing lifetime
    6. 11.2.1. Configuring lifestyles
    7. 11.2.2. Developing a custom lifestyle
    8. 11.3. Working with multiple components
    9. 11.3.1. Selecting among multiple candidates
    10. 11.3.2. Wiring sequences
    11. 11.3.3. Wiring Decorators
    12. 11.4. Configuring difficult APIs
    13. 11.4.1. Configuring primitive Dependencies
    14. 11.4.2. Creating objects with code blocks
    15. 11.4.3. Wiring with Property Injection
    16. 11.5. Summary
  23. Chapter 12. Spring.NET
    1. 12.1. Introducing Spring.NET
    2. 12.1.1. Resolving objects
    3. 12.1.2. Configuring the container
    4. 12.1.3. Loading XML
    5. 12.2. Managing lifetime
    6. 12.2.1. Configuring object scopes
    7. 12.3. Working with multiple components
    8. 12.3.1. Selecting among multiple candidates
    9. 12.3.2. Wiring sequences
    10. 12.3.3. Wiring Decorators
    11. 12.3.4. Creating Interceptors
    12. 12.4. Configuring difficult APIs
    13. 12.4.1. Configuring primitive Dependencies
    14. 12.4.2. Configuring static factories
    15. 12.4.3. Wiring with Property Injection
    16. 12.5. Summary
  24. Chapter 13. Autofac
    1. 13.1. Introducing Autofac
    2. 13.1.1. Resolving objects
    3. 13.1.2. Configuring the ContainerBuilder
    4. 13.1.3. Packaging configuration
    5. 13.2. Managing lifetime
    6. 13.2.1. Configuring instance scope
    7. 13.3. Working with multiple components
    8. 13.3.1. Selecting among multiple candidates
    9. 13.3.2. Wiring sequences
    10. 13.3.3. Wiring Decorators
    11. 13.4. Registering difficult APIs
    12. 13.4.1. Configuring primitive Dependencies
    13. 13.4.2. Registering objects with code blocks
    14. 13.4.3. Wiring with Property Injection
    15. 13.5. Summary
  25. Chapter 14. Unity
    1. 14.1. Introducing Unity
    2. 14.1.1. Resolving objects
    3. 14.1.2. Configuring the container
    4. 14.1.3. Packaging configuration
    5. 14.2. Managing lifetime
    6. 14.2.1. Configuring lifetime
    7. 14.2.2. Developing a custom lifetime
    8. 14.3. Working with multiple components
    9. 14.3.1. Selecting among multiple candidates
    10. 14.3.2. Wiring sequences
    11. 14.3.3. Wiring Decorators
    12. 14.3.4. Creating Interceptors
    13. 14.4. Configuring difficult APIs
    14. 14.4.1. Configuring primitive Dependencies
    15. 14.4.2. Registering components with code blocks
    16. 14.4.3. Wiring with Property Injection
    17. 14.5. Summary
  26. Chapter 15. MEF
    1. 15.1. Introducing MEF
    2. 15.1.1. Resolving objects
    3. 15.1.2. Defining imports and exports
    4. 15.1.3. Working with catalogs
    5. 15.2. Managing lifetime
    6. 15.2.1. Declaring creation policy
    7. 15.2.2. Releasing objects
    8. 15.3. Working with multiple components
    9. 15.3.1. Selecting among multiple candidates
    10. 15.3.2. Wiring sequences
    11. 15.3.3. Wiring Decorators
    12. 15.4. Composing difficult APIs
    13. 15.4.1. Compositing primitive parts
    14. 15.4.2. Composing parts with non-public constructors
    15. 15.4.3. Wiring with Property Injection
    16. 15.5. Summary
  27. Resources
    1. In print
    2. Online
    3. Other resources
  28. Glossary
    1. Abstraction
    2. Ambient Context
    3. Aspect-Oriented Programming (AOP)
    4. Auto-wiring
    5. Auto-registration
    6. Bastard Injection
    7. Code as Configuration
    8. Composer
    9. Composition Root
    10. Constrained Construction
    11. Constructor Injection
    12. Control Freak
    13. Convention-based Configuration
    14. Cross-Cutting Concern
    15. Dependency
    16. DI Container
    17. DTO
    18. Entity
    19. Foreign Default
    20. Humble Object
    21. Interception
    22. Inversion of Control
    23. Leaky Abstraction
    24. Lifetime Management
    25. Liskov Substitution Principle
    26. Local Default
    27. Method Injection
    28. Object Composition
    29. Object Lifetime
    30. Open/Closed Principle
    31. Per Graph Lifestyle
    32. Pooled Lifestyle
    33. Poor Man’s DI
    34. Property Injection
    35. Register Resolve Release
    36. Seam
    37. Service Locator
    38. Single Responsibility Principle
    39. Singleton Lifestyle
    40. Solid
    41. Stable Dependency
    42. Testability
    43. Transient Lifestyle
    44. Volatile Dependency
    45. Web Request Context Lifestyle
  29. inside back cover
    1. Charts
  30. Index
  31. List of Figures
  32. List of Tables
  33. List of Listings

Product information

  • Title: Dependency Injection in .NET
  • Author(s): Mark Seemann
  • Release date: October 2011
  • Publisher(s): Manning Publications
  • ISBN: 9781935182504