Does the number of controllers increase performance? MVC

I want to ask a simple question about MVC controllers. I've talked a lot about controllers for "different controllers for each base table", it cleared up a lot of things, but I have one question that I couldn't find an answer to.

My question is, if I create a controller for each base table, I say that I have 10 base tables that would create 10 controllers. Are there so many controllers that slow down application performance?

- When moving from view to controller.

- in case of transfer from controller to another controller.

I'm so kindly calm :)

+3


source to share


3 answers


Usually one request is processed by one controller. And if it (the controller) is small and has several dependencies, it's fast. When you have one huge controller with many dependencies of other classes that have their own dependencies, etc., this can be a problem.



+2


source


Short answer

Not.

Long answer

The number of controllers does not have as much of a performance impact as how expensive each controller instance is to create.

The amount of overhead you can get for the number of controllers is negligible. Although MVC uses .NET Reflection to identify the current controller type, it is optimized for namespace lookups <Project Name>.Controllers

in the first place. But this list is cached in the file, so after the first hit, the performance is pretty good.

If you run into performance issues, this is when you do heavy processing in the controller constructor. The framework instantiates the controller for every request, so you should make it as cheap as possible to instantiate the controller. If you follow DI-centric (dependent introduction), even if you don't actually use DI in your project, you will be able to keep the cost of creating a controller instance to a minimum.



What this means in plain English - inline your dependencies into the constructor when the controller is just created. Don't actually do the processing in the constructor, defer to calling the actual action method.

public interface IHeavyProcessingService
{
    IProcessingResult DoSomethingExpensive();
}

public class HeavyProcessingService : IHeavyProcessingService
{
    public HeavyProcessingService() { 
    }

    public IProcessingResult DoSomethingExpensive() {
        // Lots of heavy processing
        System.Threading.Thread.Sleep(300);
    }
}

public class HomeController
{
    private readonly IHeavyProcessingService heavyProcessingService;

    // The constructor does no heavy processing. It is deferred until after
    // the instance is created by HeavyProcessingService. 
    // The only thing happening here is assignment of dependencies.
    public HomeController(IHeavyProcessingService heavyProcessingService) {

        if (heavyProcessingService == null)
            throw new ArgumentNullException("heavyProcessingService");

        this.heavyProcessingService = heavyProcessingService;
    };

    public ActionResult Index()
    {
        var result = this.heavyProcessingService.DoSomethingExpensive();

        // Do something with the result of the heavy processing

        return View();
    }

    public ActionResult About()
    {
        return View();
    }

    public ActionResult Contact()
    {
        return View();
    }
}

      

See this answer for details .

If you actually use a DI container in your application, you can improve performance even more by choosing the right lifestyle for each dependency. If you can share the same dependency instance across multiple controller instances (single user lifestyle), it makes the controller instance even cheaper to create.

"What are spins" is not necessarily true. The number of dependencies doesn't really matter, as they must create expensive dependencies. As long as the constructors remain light and simple, and the correct lifestyle is used for each dependency, performance won't be an issue no matter how many dependencies the controller has. However, the controller should have no more than 5 direct dependencies, after which you should refactor the service aggregation , making the dependency hierarchy look more like an inverted pyramid rather than a flat set that is all injected into the controller.

+2


source


It depends on the number of controller calls. If you make frequent call to the controller for table 2 to 3 so that it can slow down. Instead, a group containing 3 tables in one controller and calling this. If your application needs to run on a separate table than a thin one, you will get a quiker's answer. But if your application needs 2 to 3 table content than you should call that 3 controllers. So the best way is to combine this into one controller. Hope you have a point

0


source







All Articles