Steven van Deursen Mark Seemann M A N N I N G TEMPORAL causes COUPLING (ch. 4) causes causes PROPERTY AMBIENT IN(JcEhC.T 4IO)N references C(cOhN.T E5X)T LOCAL DEFAULT refactor to (ch. 4) FOREIGN SERVICE METHOD DEFAULT LOCATOR refactor to INJECTION (ch. 5) (ch. 4) (ch. 5) references CONTROL FREAK refactor to (ch. 5) CONSTRUCTOR INJECTION (ch. 4) may understand CONSTRAINED CONSTRUCTION (ch. 5) refactor to relies on COMPOSITION implements ROOT PURE DI (ch. 4) (part 3) AUTO-WIRING (ch. 12) CROSS- CUTTING CONCERNS SINGLETON (ch. 9) (ch. 8) AUTO- REGISTRATION uses (ch. 12) addresses uses is a SCOPED configures DI CONTAINER (ch. 8) configures (ch. 12) may enable INTERCEPTION is a CONFIGURATION (ch. 9) OBJECT AS CODE LIFETIME (ch. 12) (ch. 8) TRANSIENT enables (ch. 8) is a formalized manages by COMPOSER is a AOP (ch. 8) (ch. 10) LIFESTYLE applies (ch. 8) Praise for the First Edition “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 “Fills a huge need for .NET designers.” — Paul Grebenc PCA Services “Takes the mystery out of a mystifying topic.” — Rama Krishna 3C Software “A uniquely personal way to learn about modern software development principles in depth. Highly recommended!” — Darren Neimke HomeStart Finance “All you ever need to know about dependency injection ... and more!” — Jonas Bandi TechTalk “A must read on Dependency Injection.” — Braj Panda Capgemini India “This book will be the definitive guide to Dependency Injection for the .NET stack.” — Doug Ferguson Improving Enterprises Dependency Injection Principles, Practices, and Patterns Dependency Injection Principles, Practices, and Patterns STEVEN VAN DEURSEN MARK SEEMANN MANNING Shelter ISland For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2019 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. ∞Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-f ree paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Acquisitions editor: Mike Stephens 20 Baldwin Road Development editors: M arina Michaels and PO Box 761 Dan Maharry Shelter Island, NY 11964 Technical development editor: Karsten Strøbæk Review editor: Ivan Martinovic´ Production editor: Anthony Calcara Copy editor: Frances Buran Proofreader: Katie Tennant Technical proofreader: Chris Heneghan Typesetter: Happenstance Type-O-Rama Cover designer: Marija Tudor ISBN 9781617294730 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – SP – 24 23 22 21 20 19 brief contents Part 1 Putting Dependency Injection on the map ............1 1 ■ The basics of Dependency Injection: What, why, and how 3 2 ■ Writing tightly coupled code 34 3 ■ Writing loosely coupled code 52 Part 2 Catalog ....................................................................81 4 ■ DI patterns 83 5 ■ DI anti-patterns 124 6 ■ Code smells 163 Part 3 Pure DI ....................................................................209 7 ■ Application composition 211 8 ■ Object lifetime 236 9 ■ Interception 281 10 ■ Aspect-Oriented Programming by design 301 11 ■ Tool-based Aspect-Oriented Programming 341 Part 4 DI Containers ........................................................357 12 ■ DI Container introduction 359 13 ■ The Autofac DI Container 393 14 ■ The Simple Injector DI Container 427 15 ■ The Microsoft.Extensions.DependencyInjection DI Container 466 v
Description: