Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:43:57+00:00 2026-05-31T07:43:57+00:00

In general does the instantiation of classes with methods, but no fields or properties,

  • 0

In general does the instantiation of classes with methods, but no fields or properties, have much overhead?

I’m developing an ASP.NET MVC application that heavily uses constructor injection and some controllers have up to 10 dependencies so far. But due to the high number of dependencies, I resorted to a IMyAppServiceProvider interface and class that provides generic access to all the dependencies, through the DependencyResolver in MVC 3.

I ripped out all my application specific code and created a Gist with my basic setup (This doesn’t include the BaseController setup mentioned below though).

I also created a BaseController class that accepts the IMyAppServiceProvider. All controllers inherit from this base class. The base class takes the IMyAppServiceProvider object and has protected variables for all of the various services. 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 “squeaky clean”. No private/protected variables, no assignments in the constructor, and the services are referenced by the base class protected variables. However, every request will instantiate every single service that my application uses, whether or not the specific controller uses all of them.

My services are simple and just contain method calls with some business logic and database interaction. They are stateless and have no class fields or properties. Therefore, instantiation should be fast, but I’m wondering if this is a best practice (I know that’s a loaded term).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:43:58+00:00Added an answer on May 31, 2026 at 7:43 am

    every request will instantiate every single service that my
    application uses, whether or not the specific controller uses all of
    them.

    I believe you’ve answered your question yourself, this is not a good approach.
    Moreover using such kind of dependency resolving (Service Locator injection) is a bad practice since Controller’s API becomes messy. Controller clients are not aware of which services really necessary for a specific controller so you might end up with unexpected run time errors, unit testing will be a mess as well.

    BTW one more suggestion – mark all classes which are considered to be a base class by abstract keyword, in this way you can avoid using it as a concrete class. Designing and implementing a base class is a concrete design decision, so make your design intentions clear.

    Regarding a cost of instantiation, in your case it would not make a big difference, but in general to reduce a cost of heavy objects instantiation you can:

    • Use Prototype pattern to “avoid the inherent cost of creating a new object in the standard way (e.g., using the ‘new’ keyword) when it is prohibitively expensive for a given application” (c) Wikipedia
    • Use Lazy Initialization for services which are not needed from the start of the owner object life time, so those would be initialized on demand. Since .NET Framework 4.0 yo can use built in Lazy(T) class
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any general tips for reducing the size of a graph generated
I think this is a general Java question, but it does involve some Android
I was recently introduced to the HTMLhelper class....along with MVC in general. Does MVC,
This is a general how does DataBind work questions... I have a simple page
I have an ASP.NET 3.5 SP1 Webforms Application. I use the MVP pattern (supervising
Does objective C have a general print command like Python? NSLog seems to log
While it does not make much sense in the general case as it should
In Python, and in general - does a close() operation on a file object
My application does not fit into the general purpose RDBMS schema category, I do
Emacs does not recognize my correct Python path. I think it is a general

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.