concept Create in category dependency injection

appears as: Create
Dependency Injection

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

When the application starts, the code in Startup creates a new custom controller activator and looks up the connection string from the application’s configuration file. When a page request comes in, the application invokes Create on the controller activator.

The activator supplies the stored connection string to a new instance of CommerceContext (not shown in the diagram). It injects CommerceContext into a new instance of SqlProductRepository. In turn, the SqlProductRepository instance together with an instance of AspNetUserContextAdapter (not shown in the diagram) are injected into a new instance of ProductService. Similarly, ProductService is injected into a new instance of HomeController, which is then returned from the Create method.

Listing 6.14 Abstract Factory with parameterless Create method
public interface IProductRepositoryFactory
{
    IProductRepository Create();    #1  
}

#1   A parameterless factory method returning a new instance of a Volatile Dependency

Abstract Factories with parameterless Create methods are often used to allow consumers to control the lifetime of their Dependencies. In the following listing, HomeController controls the lifetime of IProductRepository by requesting it from the factory, and disposing of it when it finishes using it.