Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Controller a single instance per application in ASP.NET MVC?

Over time controllers develop a lot of dependencies, and creating an instance of controller becomes too expensive for each request (especially with DI). Is there any solution to make controllers singletons?

like image 494
6opuc Avatar asked Sep 02 '13 18:09

6opuc


People also ask

Can a Controller have multiple views?

Controller is the boss, so a Controller decides which View to be rendered and Views does not / cannot care which Controller requested the View. You can / will absolutely have multiple Views from a Controller.

Can you have more than one Controller in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

Can single view access in multiple controllers?

Yes. Mention the view full path in the View method. If the name of your Views are same in both the controllers, You can keep the Common view under the Views/Shared directory and simply call the View method without any parameter. The View name should be same as the Action method name.


1 Answers

Creating instances of controllers is pretty fast and simple operation. What becomes too expensive is creating dependencies for each request. So, what you really need is many controllers which share same instances of dependencies.

E.g. you have following controller

public class SalesController : Controller
{
    private IProductRepository productRepository;
    private IOrderRepository orderRepository;

    public SalesController(IProductRepository productRepository,
                           IOrderRepository orderRepository)
    {
        this.productRepository = productRepository;
        this.orderRepository = orderRepository;
    }

    // ...       
}

You should configure your dependency injection framework to use same instances of repositories for all application (keep in mind, you can have synchronization problems). Now creating dependencies is not expensive any more. All dependencies are instantiated only once, and reused for all requests.

If you have many dependencies and you are worrying about costs of getting reference to instance of each dependency and providing these references to controller instance (which I don't think will be very expensive), then you can group your dependencies (something like Introduce Parameter Object refactoring):

public class SalesController : Controller
{
    private ISalesService salesService;

    public SalesController(ISalesService salesService)
    {
        this.salesService = salesService;
    }

    // ...       
}

public class SalesService : ISalesService
{
    private IProductRepository productRepository;
    private IOrderRepository orderRepository;

    public SalesService(IProductRepository productRepository,
                           IOrderRepository orderRepository)
    {
        this.productRepository = productRepository;
        this.orderRepository = orderRepository;
    }

    // ...       
}

Now you have single dependency, which will be injected very quickly. If you will configure your dependency injection framework to use singleton SalesService, then all SalesControllers will reuse same instance of service. Creation of controllers and providing dependencies will be very fast.

like image 116
Sergey Berezovskiy Avatar answered Sep 26 '22 00:09

Sergey Berezovskiy