Dependency Injection in .NET

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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

Author(s): Mark Seemann
Edition: 1
Publisher: Manning Publications
Year: 2011

Language: English
Pages: 552

Front cover......Page 1
contents......Page 11
foreword......Page 19
preface......Page 21
acknowledgments......Page 23
about this book......Page 26
Roadmap......Page 27
Code conventions and downloads......Page 28
Author Online......Page 29
About the author......Page 30
about the cover illustration......Page 31
Part 1—Putting Dependency Injection on the map......Page 33
A Dependency Injection tasting menu......Page 35
1.1.1 Unlearning DI......Page 37
1.1.2 Understanding the purpose of DI......Page 40
1.2.1 Hello DI code......Page 45
1.2.2 Benefits of DI......Page 47
1.3.1 Seams......Page 54
1.3.3 Volatile Dependencies......Page 55
1.4 DI scope......Page 56
1.4.1 Object Composition......Page 57
1.4.3 Interception......Page 58
1.4.4 DI in three dimensions......Page 59
1.5 Summary......Page 60
A comprehensive example......Page 61
2.1 Doing it wrong......Page 62
2.1.1 Building a tightly coupled application......Page 63
2.1.2 Smoke test......Page 68
2.1.3 Evaluation......Page 69
2.1.4 Analysis......Page 71
2.2 Doing it right......Page 73
2.2.1 Rebuilding the commerce application......Page 75
2.2.2 Analyzing the loosely coupled implementation......Page 83
2.3.1 Architecture......Page 85
2.3.2 Basket feature......Page 86
2.4 Summary......Page 89
DI Containers......Page 90
3.1 Introducing DI Containers......Page 93
3.1.1 Hello container......Page 94
3.1.2 Auto-wiring......Page 96
3.2 Configuring DI Containers......Page 99
3.2.1 Configuring containers with XML......Page 100
3.2.2 Configuring containers with code......Page 102
3.2.3 Configuring containers by convention......Page 104
3.3.1 Composition Root......Page 107
3.3.2 Register Resolve Release......Page 113
3.4.1 Selecting a DI Container......Page 119
3.4.2 Microsoft and DI......Page 121
3.5 Summary......Page 123
Part 2—DI catalog......Page 125
DI patterns......Page 127
4.1.1 How it works......Page 130
4.1.2 When to use it......Page 131
4.1.3 Known use......Page 132
4.1.4 Example: Adding a currency provider to the shopping basket......Page 133
4.1.5 Related patterns......Page 135
4.2.1 How it works......Page 136
4.2.2 When to use it......Page 137
4.2.3 Known use......Page 139
4.2.4 Example: Defining a currency profile service for the BasketController......Page 140
4.2.5 Related patterns......Page 142
4.3.1 How it works......Page 143
4.3.2 When to use it......Page 144
4.3.3 Known use......Page 145
4.3.4 Example: Converting baskets......Page 146
4.3.5 Related patterns......Page 149
4.4.1 How it works......Page 150
4.4.2 When to use it......Page 152
4.4.4 Example: Caching Currency......Page 155
4.4.5 Related patterns......Page 162
4.5 Summary......Page 163
DI anti-patterns......Page 165
5.1.1 Example: newing up Dependencies......Page 168
5.1.2 Example: Factory......Page 169
5.1.3 Analysis......Page 175
5.2.1 Example: ProductService with Foreign Default......Page 176
5.2.2 Analysis......Page 178
5.3.1 Example: late-binding ProductRepository......Page 181
5.3.2 Analysis......Page 183
5.4 Service Locator......Page 186
5.4.1 Example: ProductService using a Service Locator......Page 188
5.4.2 Analysis......Page 189
5.5 Summary......Page 192
DI refactorings......Page 194
6.1 Mapping runtime values to Abstractions......Page 195
6.1.1 Abstractions with runtime Dependencies......Page 196
6.1.2 Example: selecting a routing algorithm......Page 198
6.1.3 Example: using a CurrencyProvider......Page 200
6.2.1 Closing connections through Abstractions......Page 202
6.2.2 Example: invoking a product-management service......Page 205
6.3 Resolving cyclic Dependencies......Page 207
6.3.1 Addressing Dependency cycles......Page 208
6.3.2 Example: composing a window......Page 210
6.4.1 Recognizing and addressing Constructor Over-injection......Page 214
6.4.2 Example: refactoring order reception......Page 217
6.5 Monitoring coupling......Page 220
6.5.1 Unit-testing coupling......Page 221
6.5.2 Integration-testing coupling......Page 223
6.5.3 Using NDepend to monitor coupling......Page 225
6.6 Summary......Page 227
Part 3—DIY DI......Page 229
Object Composition......Page 231
7.1.1 Example: updating currencies......Page 234
7.2.1 ASP.NET MVC extensibility......Page 238
7.2.2 Example: implementing CommerceControllerFactory......Page 240
7.3 Composing WCF applications......Page 242
7.3.1 WCF extensibility......Page 243
7.3.2 Example: wiring up a product-management service......Page 244
7.4.1 WPF Composition......Page 251
7.4.2 Example: wiring up a product-management rich client......Page 252
7.5.1 ASP.NET composition......Page 256
7.5.2 Example: wiring up a CampaignPresenter......Page 257
7.6 Composing PowerShell cmdlets......Page 262
7.6.1 Example: composing basket-management cmdlets......Page 263
7.7 Summary......Page 267
Object Lifetime......Page 268
8.1.1 Introducing Lifetime Management......Page 271
8.1.2 Managing lifetime with a container......Page 274
8.2 Working with disposable Dependencies......Page 279
8.2.1 Consuming disposable Dependencies......Page 280
8.2.2 Managing disposable Dependencies......Page 283
8.3.1 Singleton......Page 287
8.3.2 Transient......Page 290
8.3.3 Per Graph......Page 291
8.3.4 Web Request Context......Page 293
8.3.5 Pooled......Page 298
8.3.6 Other lifestyles......Page 303
8.4 Summary......Page 305
Interception......Page 307
9.1.1 Example: implementing auditing......Page 309
9.1.2 Patterns and principles for Interception......Page 313
9.2 Implementing Cross-Cutting Concerns......Page 317
9.2.1 Intercepting with a Circuit Breaker......Page 318
9.2.2 Handling exceptions......Page 324
9.2.3 Adding security......Page 325
9.3 Declaring aspects......Page 327
9.3.1 Using attributes to declare aspects......Page 328
9.3.2 Applying dynamic Interception......Page 332
9.3.3 Example: intercepting with Windsor......Page 335
9.4 Summary......Page 340
Part 4—DI Containers......Page 343
Castle Windsor......Page 345
10.1 Introducing Castle Windsor......Page 346
10.1.1 Resolving objects......Page 347
10.1.2 Configuring the container......Page 349
10.1.3 Packaging configuration......Page 354
10.2 Managing lifetime......Page 355
10.2.1 Configuring lifestyle......Page 356
10.2.2 Using advanced lifestyles......Page 357
10.2.3 Developing a custom lifestyle......Page 359
10.3.1 Selecting among multiple candidates......Page 365
10.3.2 Wiring sequences......Page 368
10.3.3 Wiring Decorators......Page 371
10.4.1 Configuring primitive Dependencies......Page 373
10.4.2 Registering components with code blocks......Page 375
10.4.3 Wiring with Property Injection......Page 376
10.5 Summary......Page 377
StructureMap......Page 379
11.1 Introducing StructureMap......Page 380
11.1.1 Resolving objects......Page 382
11.1.2 Configuring the container......Page 384
11.1.3 Packaging configuration......Page 390
11.2 Managing lifetime......Page 393
11.2.1 Configuring lifestyles......Page 394
11.2.2 Developing a custom lifestyle......Page 396
11.3 Working with multiple components......Page 402
11.3.1 Selecting among multiple candidates......Page 403
11.3.2 Wiring sequences......Page 406
11.3.3 Wiring Decorators......Page 409
11.4.1 Configuring primitive Dependencies......Page 412
11.4.2 Creating objects with code blocks......Page 413
11.4.3 Wiring with Property Injection......Page 414
11.5 Summary......Page 415
Spring.NET......Page 417
12.1 Introducing Spring.NET......Page 418
12.1.1 Resolving objects......Page 419
12.1.2 Configuring the container......Page 421
12.1.3 Loading XML......Page 425
12.2 Managing lifetime......Page 429
12.2.1 Configuring object scopes......Page 430
12.3 Working with multiple components......Page 431
12.3.1 Selecting among multiple candidates......Page 432
12.3.2 Wiring sequences......Page 434
12.3.3 Wiring Decorators......Page 437
12.3.4 Creating Interceptors......Page 439
12.4.1 Configuring primitive Dependencies......Page 444
12.4.2 Configuring static factories......Page 445
12.4.3 Wiring with Property Injection......Page 446
12.5 Summary......Page 448
Autofac......Page 449
13.1 Introducing Autofac......Page 450
13.1.1 Resolving objects......Page 452
13.1.2 Configuring the ContainerBuilder......Page 454
13.1.3 Packaging configuration......Page 459
13.2 Managing lifetime......Page 461
13.2.1 Configuring instance scope......Page 462
13.3 Working with multiple components......Page 465
13.3.1 Selecting among multiple candidates......Page 466
13.3.2 Wiring sequences......Page 470
13.3.3 Wiring Decorators......Page 472
13.4 Registering difficult APIs......Page 474
13.4.1 Configuring primitive Dependencies......Page 475
13.4.2 Registering objects with code blocks......Page 476
13.4.3 Wiring with Property Injection......Page 477
13.5 Summary......Page 479
Unity......Page 480
14.1 Introducing Unity......Page 482
14.1.1 Resolving objects......Page 483
14.1.2 Configuring the container......Page 485
14.1.3 Packaging configuration......Page 490
14.2 Managing lifetime......Page 491
14.2.1 Configuring lifetime......Page 492
14.2.2 Developing a custom lifetime......Page 496
14.3.1 Selecting among multiple candidates......Page 505
14.3.2 Wiring sequences......Page 508
14.3.3 Wiring Decorators......Page 511
14.3.4 Creating Interceptors......Page 513
14.4.1 Configuring primitive Dependencies......Page 518
14.4.2 Registering components with code blocks......Page 519
14.4.3 Wiring with Property Injection......Page 521
14.5 Summary......Page 522
MEF......Page 524
15.1 Introducing MEF......Page 527
15.1.1 Resolving objects......Page 528
15.1.2 Defining imports and exports......Page 531
15.1.3 Working with catalogs......Page 536
15.2.1 Declaring creation policy......Page 541
15.2.2 Releasing objects......Page 543
15.3.1 Selecting among multiple candidates......Page 545
15.3.2 Wiring sequences......Page 548
15.3.3 Wiring Decorators......Page 551
15.4.1 Compositing primitive parts......Page 553
15.4.2 Composing parts with non-public constructors......Page 554
15.4.3 Wiring with Property Injection......Page 555
15.5 Summary......Page 556
In print......Page 558
Online......Page 559
Other resources......Page 562
glossary......Page 563
A......Page 567
B......Page 568
C......Page 569
D......Page 571
G......Page 573
I......Page 574
L......Page 576
O......Page 577
P......Page 578
R......Page 579
S......Page 580
U......Page 582
X......Page 583
Back cover......Page 586