In the general case, creating instances of classes using methods, but without fields or properties, has a lot of overhead?
I am developing an ASP.NET MVC application that makes extensive use of constructor injection, and some controllers have up to 10 dependencies. But due to the large number of dependencies, I resorted to an interface and a class IMyAppServiceProviderthat provides shared access to all dependencies through DependencyResolver in MVC 3.
I pulled out all my application code and created a Gist with my basic setup (this doesn't include the BaseController setting below, though).
I also created a BaseController class that accepts IMyAppServiceProvider. All controllers inherit from this base class. The base class accepts an object IMyAppServiceProviderand has protected variables for all different services. The code looks something like this:
public class BaseController
{
protected IService1 _service1;
protected IService2 _service2;
protected IService3 _service3;
public BaseController(IMyAppServiceProvider serviceProvider)
{
_service1 = serviceProvider.GetService<IService1>;
_service2 = serviceProvider.GetService<IService2>;
_service3 = serviceProvider.GetService<IService3>;
}
}
This makes the code for the controllers scratchy. No private / protected variables, assignments in the constructor, or services refer to variables protected by the base class. However, each request will create an instance of each service used by my application, regardless of whether any particular controller uses all of them.
- . . , , , ( , ).