concept StructureMap in category .net

appears as: StructureMap
Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

Using StructureMap’s configuration API, you can express the configuration from listing 3.1 more compactly, as shown in the following listing.

Listing 3.2. Configuring StructureMap with code
Figure 11.2. The overall usage pattern of StructureMap is simple: First we configure the container, and then we resolve components from it. In the vast majority of cases, we create an instance of the Container class and completely configure it before we start resolving components from it. We resolve components from the same instance that we configure.

StructureMap’s implementations of the different lifestyles are equivalent to the general lifestyles described in chapter 8, so I won’t spend much time on them in this chapter.

In most cases, we should be able to get by with the comprehensive selection of lifestyles already offered by StructureMap, but if we have special needs, it’s possible to implement a custom lifestyle. In this section, you’ll see how to do this. After a brief review of the SEAM that makes this possible, we’ll spend most of the time going through an example.

In section 11.2.1, you already got a glimpse of StructureMap’s lifestyle API. The LifecycleIs method takes an instance of the ILifecycle interface, which models how lifestyles interact with the rest of StructureMap:

public interface ILifecycle
{
    string Scope { get; }
    void EjectAll();
    IObjectCache FindCache();
}

Of those three methods, the FindCache method is the fulcrum. It returns a cache that StructureMap uses to look up and insert objects with that particular lifestyle. The ILifecycle interface mostly serves as an Abstract Factory[5] for IObjectCache instances that contain the implementation of the lifestyle. This interface is far more complex, but not terribly difficult to implement:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest