Part 3. DIY DI

In chapter 1, I gave a short outline of the three dimensions of DI: OBJECT COMPOSITION, LIFETIME MANAGEMENT, and INTERCEPTION. In this part of the book, I expand this viewpoint into three distinct chapters. Many DI CONTAINERS have features that directly relate to these dimensions. Some provide features in all three dimensions, whereas others only support some of them.

However, because a DI CONTAINER is an optional tool, I feel that it’s more important to explain the underlying principles and techniques that containers typically use to implement these features. Part 3 examines how you can do-it-yourself instead of using a DI CONTAINER. You can potentially use this information to build your own DI CONTAINER (but please don’t—the world doesn’t need yet another container) or apply DI without using a container at all—this is what we call POOR MAN’S DI. The main purpose of this part of the book, though, is to expose the underlying mechanisms of OBJECT COMPOSITION, LIFETIME MANAGEMENT, and INTERCEPTION without having to use a specific DI CONTAINER. I think that had I used a specific container, it would have been difficult to distinguish general principles from specific API details.

Chapter 7 explains how to compose objects in various concrete frameworks such as ASP.NET MVC, WPF, WCF, and so on. Not all frameworks support DI equally well, and even among those that do, the ways they do it differ a lot. For each framework, it can be difficult to identify the SEAM that enables DI in that framework. However, once that SEAM is found, you have a solution for all applications that use this particular framework. In chapter 7, I have done this work for the most common .NET application frameworks. Think of it as a catalog of framework SEAMS.

Even if your particular framework of choice isn’t covered, I’ve tried to address all the various sorts of framework limitations you may encounter. For instance, from a DI perspective, PowerShell is the most restrictive type of framework I can think of, so I use that as an example. You should be able to extrapolate a solution for similar frameworks, even if they aren’t explicitly covered.

Although composing objects isn’t particularly hard with POOR MAN’S DI, you should begin to see the benefits of a real DI CONTAINER after reading about LIFETIME MANAGEMENT. It’s possible to properly manage the lifetime of various objects in an object graph, but it requires more custom code than OBJECT COMPOSITION, and none of that code adds any particular business value to an application.

In addition to explaining the basics of LIFETIME MANAGEMENT, chapter 8 also contains a catalog of common lifestyles. This catalog serves as a vocabulary for discussing lifestyles throughout part 4, so although you don’t have to be able to implement any of these by hand, it’s good to know how they work.

In chapter 9, we look at the frequently occurring problem of implementing CROSS-CUTTING CONCERNS in a component-based way. Going from a simple application of the Decorator design pattern all the way to runtime INTERCEPTION, we look at ways to compose loosely coupled applications in a modular way. I consider this chapter the climax of the book—this is where many readers during the early access program said they began to see the contours of a tremendously powerful way to model software.

Although I use POOR MAN’S DI to explore and explain DI, I don’t recommend it for professional use. Many good DI CONTAINERS are available on .NET, and they are all free. Thus it’s only fitting that part 4 is dedicated to detailed API coverage of specific containers.

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

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