concept controller in category dependency injection

appears as: controller, A controller, controllers, controllers
Dependency Injection

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

ASP.NET Core MVC takes its name from the Model View Controller design pattern.4  In this context, the most important thing to understand is that when a web request arrives, a controller handles the request, potentially using a (domain) model to deal with it, and forms a response that’s finally rendered by a view.

A controller is normally a class that derives from the abstract Controller class. It has one or more action methods that handle requests; for example, a HomeController class typically has a method named Index that handles the request for the default page. When an action method returns, it passes on the resulting model to the view through a ViewResult instance.

Listing 2.4 Index method on the default controller class
public ViewResult Index()
{
    bool isPreferredCustomer =    #1  
        this.User.IsInRole("PreferredCustomer");    #1  

    var service = new ProductService();    #2  

    var products = service.GetFeaturedProducts(    #3  
        isPreferredCustomer);    #3  

    this.ViewData["Products"] = products;    #4  

    return this.View();
}

#1   Determines whether a customer is a preferred customer
#2   Creates ProductService from the domain layer
#3   Gets the list of featured products (defined in the data access layer) from the ProductService
#4   Stores the list of products in the controller’s generic ViewData dictionary for later use by the view
  • A request comes in for a controller named Home.
  • Dependency Injection in .NET

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

  • By default, ASP.NET MVC expects Controllers to have default constructors. Because I introduced a parameter to HomeController’s constructor, the MVC framework doesn’t know how to create an instance of HomeController. This issue can be solved by developing a custom IControllerFactory. How this is done is outside the scope of this chapter, but it’s a subject that will be discussed in chapter 7. Suffice it to say that this custom factory will create an instance of the concrete ProductRepository and supply it to HomeController’s constructor.
  • To enable the container to wire up Controllers in the application, you must employ the appropriate SEAM in ASP.NET MVC, called an IControllerFactory (discussed in detail in section 7.2). For now, it’s enough to understand that to integrate with ASP.NET MVC, you must create an Adapter[9] around the container and tell the framework about it.

    // This constructor is used by the MVC framework to
    // instantiate the controller using the default forms
    // authentication and membership providers.
    public AccountController()
        : this(null, null)
    {
    }
    // This constructor is not used by the MVC framework but
    // is instead provided for ease of unit testing this type.
    // See the comments at the end of this file for more
    // information.
    public AccountController(IFormsAuthentication formsAuth,
        IMembershipService service)
    {
        this.FormsAuth =
            formsAuth ?? new FormsAuthenticationService();
        this.MembershipService =
            service ?? new AccountMembershipService();
    }
    Listing 7.3. Creating Controllers