concept construction in category dependency injection

appears as: construction, The construction, construction
Dependency Injection

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

The construction of objects inside the Main method is a basic example of Pure DI. No DI Container is used to compose the Salutation and its ConsoleMessageWriter Dependency. Figure 1.10 shows the relationship between the collaborators.

Listing 5.18 Factory that creates SqlProductRepository instances
public class SqlProductRepositoryFactory
    : IProductRepositoryFactory
{
    private readonly string connectionString;

    public SqlProductRepositoryFactory(
        IConfigurationRoot configuration)    #1  
    {
        this.connectionString =
            configuration.GetConnectionString(    #2  
                "CommerceConnectionString");
    }

    public IProductRepository Create()
    {
        return new SqlProductRepository(    #3  
            new AspNetUserContextAdapter(),
            new CommerceContext(this.connectionString));
    }
}

#1   Constructor with a particular signature that all IProductRepositoryFactory implementations must have. By accepting Microsoft’s IConfigurationRoot, the factory loads its required configuration values. It also throws an error during construction in case such a value is missing.
#2   Loads the connection string from the configuration file and stores it for later use
#3   Creates a new SqlProductRepository using Dependencies that are located in different assemblies
Dependency Injection: Design Patterns Using Spring and Guice

This is an excerpt from Manning's book Dependency Injection: Design Patterns Using Spring and Guice.

While construction by hand definitely helps with testing, it has some problems, the most obvious one being the burden of knowing how to construct object graphs being placed on the client of a service. If I use the same object in many places, I must repeat code for wiring objects in all of those places. Construction by hand, as the name suggests, is really tedious! If you alter the dependency graph or any of its parts, you may be forced to go through and alter all of its clients as well. Fixing even a small bug can mean changing vast amounts of code.

Another grave problem is the fact that users need to know how object graphs are wired internally. This violates the principle of encapsulation and becomes problematic when dealing with code that is used by many clients, who really shouldn’t have to care about the internals of their dependencies in order to use them. There are also potent arguments against construction by hand that we will encounter in other forms in the coming chapters. So how can we offload the burden of dependency creation and not shoot ourselves in the foot doing so? One answer is the Factory pattern.

What else can we say about their differences? Plenty: dependencies wired by constructor mean they are wired at the time of the object’s creation. The fact that all dependences are wired and that the dependent is ready for use immediately upon construction is a very compelling one. It leads us to the notion of creating valid objects that are good citizens. Were a dependency not available (either in a unit test or due to faulty configuration), an early compile error would result. No field can refer to half-constructed dependencies. Moreover, a constructor-injected object cannot be created unless all dependencies are available. With setter injection such mistakes are easy to make, as shown in listing 3.12 and illustrated in figure 3.6.

In listing 3.18, both dependents in the circle attempt to use each other within their constructors. One calls a method on the other and is doomed to fail. Since Host has been proxied but does not yet hold a valid reference to its delegate, it has no way of passing through calls to the dependency. This is the in-construction problem in a nutshell.

The in-construction problem has no obvious solution with constructor injection. The only recourse in such cases is setter injection, as shown in listing 3.19.

Listing 3.19. In-construction problem solved via setter injection (using Guice)

Now initialization is guaranteed to succeed because both objects hold references to each other prior to any call to host.calculateAge(). This is not the whole story—the in-construction problem can run even deeper. Consider listing 3.20’s addendum, which exacerbates the problem.

Listing 3.20. In-construction problem not quite solved

Now both objects not only refer to one another but also use each other when dependencies are being wired. Essentially, this is an in-construction variation of the circular reference problem. And there is no known solution to this circular initialization mess with constructor injection. In fact, there is no solution to the problem with setter injection either!