concept caller in category dependency injection

appears as: caller, callers, The caller
Dependency Injection

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

Mary’s happy that she has encapsulated the data access technology (Entity Framework Core), configuration, and domain logic in the ProductService class. She has delegated the knowledge of the user to the caller by passing in the isCustomerPreferred parameter, and she uses this value to calculate the discount for all the products.

Constrain the design to a single (public) constructor. Because the constructor is the definition of a class’s Dependencies, it makes little sense to have multiple definitions. Overloaded constructors lead to ambiguity: which constructor should the caller (or a DI Container) use?

Listing 4.5 Injecting a Dependency using Constructor Injection
public class HomeController
{
    private readonly IProductService service;    #1  

    public HomeController(    #2  
        IProductService service)    #3  
    {
        if (service == null)    #4  
            throw new ArgumentNullException("service");  #4  

        this.service = service;    #5  
    }
}

#1   Private instance field to store supplied Dependency
#2   Constructor that statically defines its Dependencies
#3   Argument for supplying the required Dependency
#4   Guard Clause to prevent clients from passing in null
#5   Storing the Dependency in the private field for later use. The constructor contains no other logic than verifying and storing its incoming Dependencies

The IProductService Dependency is a required constructor argument of HomeController; any client that doesn’t supply an instance of IProductService can’t compile. But, because an interface is a reference type, a caller can pass in null as an argument to make the calling code compile. You need to protect the class against such misuse with a Guard Clause.1  Because the combined efforts of the compiler and the Guard Clause guarantee that the constructor argument is valid if no exception is thrown, the constructor can store the Dependency for future use without knowing anything about the real implementation.