About this Book

This is a book about Dependency Injection first and foremost. It’s also a book about .NET, but that’s much less important. C# is used for code examples, but much of the discussion in this book can be easily applied to other languages and platforms. In fact, I learned a lot of the underlying principles and patterns from reading books where Java or C++ was used in examples.

Dependency Injection (DI) is a set of related patterns and principles. It’s a way to think about and design code more than it’s a specific technology. The ultimate purpose of using DI is to create maintainable software within the object-oriented paradigm.

The concepts used throughout this book all relate to object-oriented programming. The problem that DI addresses (code maintainability) is universal, but the proposed solution is given within the scope of object-oriented programming in statically typed languages: C#, Java, Visual Basic .NET, C++, and so on. You can’t apply DI to procedural programming, and it may not be the best solution in functional or dynamic languages.

DI in isolation is just a small thing, but it’s closely interconnected with a large complex of principles and patterns for object-oriented software design. Whereas the book focuses consistently on DI from start to finish, it also discusses many of these other topics in the light of the specific perspective that DI can give. The goal of the book is more than just teaching you about DI specifics: it’s to make you a better object-oriented programmer.

Who should read this book?

It would be tempting to state that this is a book for all .NET developers. However, the .NET community today is vast and spans developers working with web applications, desktop applications, smartphones, RIA, integration, office automation, content management systems, and even games. Although .NET is object-oriented, not all of those developers write object-oriented code.

This is a book about object-oriented programming, so at minimum readers should be interested in object orientation and understand what an interface is. A few years of professional experience and knowledge of design patterns or SOLID will certainly be a benefit as well. In fact, I don’t expect beginners to get much out of the book; it’s mostly targeted towards experienced developers and software architects.

The examples are all written in C#, so readers working with other .NET languages must be able to read and understand C#. Readers familiar with non-.NET object-oriented languages such as Java and C++ may also find the book valuable, because the .NET platform-specific content is relatively light. Personally, I read a lot of pattern books with examples in Java and still get a lot out of them, so I hope the converse is true as well.

Roadmap

The contents of this book are divided into four parts. Ideally, I’d like you to first read it from cover to cover and then subsequently use it as a reference, but I understand if you have other priorities. For that reason, a majority of the chapters are written so that you can dive right in and start reading from that point.

The first part is the major exception. It contains a general introduction to DI and is probably best read sequentially. The second part is a catalog of patterns and the like, whereas the third part is an examination of DI from three different angles. The fourth and largest part of the book is a big catalog of six DI CONTAINER libraries.

There are a lot of interconnected concepts and because I introduce them the first time it feels natural, this means that I often mention concepts before I’ve formally introduced them. To distinguish these universal concepts from more local terms, I consistently use SMALL CAPS to make them stand out. All these terms are briefly defined in the glossary, which also contains references to a more extensive description.

  • Part 1 is a general introduction to DI. If you don’t know what DI is, this is the place to start; but even if you do, you may want to familiarize yourself with the contents of part 1, as it establishes a lot of the context and terminology used in the rest of the book. Chapter 1 discusses the purpose and benefits of DI and provides a general outline. Chapter 2 contains a big and rather comprehensive example, and chapter 3 explains how DI CONTAINER libraries fit into the overall picture. Compared to the other parts, part 1 has a much more linear progression of its content. You’ll need to read each chapter from the beginning to gain the most from it.
  • Part 2 is a catalog of patterns, anti-patterns, and refactorings. This is where you’ll find prescriptive guidance on how to implement DI, and the dangers to look out for. Chapter 4 is a catalog of DI design patterns, and, conversely, chapter 5 is a catalog of anti-patterns. Chapter 6 contains generalized solutions to commonly occurring issues. As a catalog, each chapter contains a set of loosely related sections that are designed to be read in isolation as well as in sequence.
  • Part 3 examines DI from three different angles: OBJECT COMPOSITION, LIFETIME MANAGEMENT, and INTERCEPTION. In chapter 7, I discuss how to implement DI on top of existing application frameworks such as WCF, ASP.NET MVC, WPF, and others. In many ways, you can use chapter 7 as a catalog of how to implement DI on a set of frameworks. Chapter 8 describes how to manage dependency lifetimes to avoid resources leaks. Whereas the structure is a little less stringent than previous chapters, a large part of the chapter can be used as a catalog of well-known lifetime styles. Chapter 9 finally describes how to compose applications with CROSS-CUTTING CONCERNS. This is where we harvest the benefits of all the work that came before, so, in many ways, I consider this to be the climax of the book.
  • Part 4 is a catalog of DI CONTAINER libraries. Six chapters each cover a specific container in a fair amount of detail: Castle Windsor, StructureMap, Spring.NET, Autofac, Unity, and MEF. Each chapter covers its container in a rather condensed form to save space, so you may want to read about only the two or three containers that interest you the most. In many ways, I regard part 4 as a very big set of appendixes.

To keep the discussion of the DI principles and patterns free of any specific container APIs, most of the book, with the exception of part 4, is written without referencing a particular container. This is also why the containers appear with such force in part 4. It’s my hope that by keeping the discussion general, the book will be useful for a longer period of time.

You can also take the concepts from parts 1 through 3 and apply them to container libraries not covered in part 4. There are good containers available that, unfortunately, I couldn’t cover, but even for users of these libraries, I hope that this book has a lot to offer.

Code conventions and downloads

There are many code examples in this book. Most of it is C#, but there’s also a bit of XML here and there. Source code in listings and text is in a fixed-width font to separate it from ordinary text.

All the source code for the book is written in C# and Visual Studio 2010. The ASP.NET MVC applications are written against ASP.NET MVC 3.

Only a few of the techniques described in this book hinge on modern language features. I started writing loosely coupled code in .NET 1.1 and I could have written most of the book’s code examples on that platform without having to change my conclusions. As it were, I wanted to strike a reasonable balance between conservative and modern coding styles. When I write code professionally I use the modern language features to a much greater degree, but here the most advanced features are generics and LINQ. The last thing I want is for you to get the idea that DI can only be applied with ultra-modern languages.

Writing code examples for a book presents its own set of challenges. Compared to a modern computer monitor, a book only allows for very short lines of code. It was very tempting to write code in a terse style with short but cryptic names for methods and variables. Such code is already difficult to understand as real code when you still have an IDE and a debugger nearby, but it becomes really difficult to follow in a book. I found it very important to keep names as readable as possible. To make it all fit, I’ve often had to resort to some unorthodox line breaks. All the code compiles, but sometimes the formatting looks a bit funny.

The code also makes extensive use of the var keyword. In my professional code I use this almost exclusively, but for written text I often find it helpful when paired with explicit declarations because the IDE isn’t around to help. Still, to save space, I use var wherever I judge that an explicit declaration is unnecessary.

The word class is often used as a synonym for a type. In .NET, classes, structs, interfaces, enums, and so on are all types, but because the word type is also a word with a lot of overloaded meaning in ordinary language, it would often make the text less clear if used.

Most of the code in this book relates to an overarching example running through the book: an online store complete with supporting internal management applications. This is about the least exciting example you can expect to see in any software text, but I chose it for a few reasons:

  • It’s a well-known problem domain for most readers. Although it may seem boring, I think this is an advantage because it doesn’t steal focus from DI.
  • I also have to admit that I couldn’t really think of any other domain that was rich enough to support all the different scenarios I had in mind.

I wrote a lot of code to support the code examples, and most of that code is not even in the book. In fact, I wrote almost all of it using Test-Driven Development (TDD), but as this isn’t a TDD book, I generally don’t show the unit tests in the book.

The source code for all examples in this book is available from Manning’s website: http://manning.com/DependencyInjectionin.NET. The ReadMe.txt in the root of the download contains instructions for compiling and running the code.

Author Online

The purchase of Dependency Injection in .NET includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to http://manning.com/DependencyInjectionin.NET. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It isn’t a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the author

Mark Seemann is a programmer, software architect, and speaker living in Copenhagen, Denmark. He has been working with software since 1995 and TDD since 2003, including six years with Microsoft as a consultant, developer, and architect. Mark is currently professionally engaged with software development, and is working out of Copenhagen. He enjoys reading, painting, playing the guitar, good wine, and gourmet food.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.29.119