concept console application in category dependency injection

appears as: console application, console applications, A console application
Dependency Injection

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

To get a sense of the structure of the program, we’ll start by looking at the Main method of the console application. Then we’ll show you the collaborating classes; but first, here’s the Main method of the Hello DI! application:

Listing 4.2 The application’s object graph as part of a console application
public static class Program
{
    public static void Main(string[] args)    #1  
    {
        string connectionString = args[0];    #2  

        HomeController controller =
            CreateController(connectionString);    #3  

        var result = controller.Index();

        var vm = (FeaturedProductsViewModel)result.Model;

        Console.WriteLine("Featured products:");

        foreach (var product in vm.Products)
        {
            Console.WriteLine(product.SummaryText);
        }
    }

    private static HomeController CreateController(    #4  
        string connectionString)
    {
        var userContext = new ConsoleUserContext();    #5  

        return
            new HomeController(    #6  
                new ProductService(    #6  
                    new SqlProductRepository(    #6  
                        new CommerceContext(    #6  
                            connectionString)),    #6  
                    userContext));    #6  
    }
}

#1   The application’s entry point
#2   Extracts a connection string from the supplied command-line arguments
#3   Requests that the application’s Composition Root build a new controller instance
#4   Acts as the application’s Composition Root
#5   IUserContext implementation that allows the ProductService to function and calculate the discounts
#6   Builds the application’s object graph

A console application is, hands down, the easiest type of application to compose. Contrary to most other .NET BCL application frameworks, a console application involves virtually no Inversion of Control. When execution hits the application’s entry point (usually the Main method in the Program class), you’re on your own. There are no special events to subscribe to, no interfaces to implement, and precious few services you can use.

Dependency Injection in .NET

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

To get a sense of the structure of the program, we’ll start by looking at the Main method of the console application, and then I’ll show you the collaborating classes:

A console application is probably the easiest type of application in which to apply DI.

7.1. Composing console applications

A console application is hands down the easiest type of application to compose. Contrary to most other BCL application frameworks, a console application involves virtually no INVERSION OF CONTROL. When execution hits the application’s entry point (usually the Main method), we’re on our own. There are no special events to subscribe to, no interfaces to implement, and precious few services we can use.