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

  • Home
  • SEARCH
  • 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 6011655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:17:29+00:00 2026-05-23T02:17:29+00:00

I’ve been relying on IoC pattern for 1 year now using structuremap & asp.net

  • 0

I’ve been relying on IoC pattern for 1 year now using structuremap & asp.net mvc. IoC is really neat especially if you have a lot of dependencies in your class, just inject dependency in your constructor and your done, the IoC will auto-inject it for you.

My question is that im using asp.net mvc with a controller class which have IServices dependencies.

I need the dependency services only for a certain controller action let say “/customer/add”
but i don’t need those dependency on other action let say “/Customer/Index”. However, since i’m using a DI (Dependency injection) in constructor, the dependency are always instantiated even if i don’t need them. Is that any good? object creation is expensive and consume memory footprint.

Of course, i can do container.GetInstance inside an action, but it is not a good practice since you will heavily be dependent on IoC in your code and unit testing.

Any suggestion? correct me if im wrong with something.

  • 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-23T02:17:29+00:00Added an answer on May 23, 2026 at 2:17 am

    Updated in response to jgauffins comment

    I see two straightforward ways to solve your issue:

    Inject a service factory to your constructor and create the service on demand.

    interface IFooService
    {
        void Bar();
    }
    
    interface IFooServiceFactory
    {
        IFooService CreateFooService();
    }
    
    class YourController
    {
        public YourController(IFooServiceFactory fooServiceFactory)
        {
            this.fooServiceFactory = fooServiceFactory;
        }
    
        public void YourAction()
        {
            IFooService fooService = this.fooServiceFactory.CreateFooService();
            fooService.Bar();
        }
    }
    

    Inject a service proxy to your constructor and let the proxy create the real service on demand.

    interface IFooService
    {
        void Bar();
    }
    
    class FooServiceProxy : IFooService
    {
        private IFooService realFooService;
    
    
        void IFooService.Bar()
        {
             IFooService realFooService = GetRealFooService();
             realFooService.Bar();
        }
    
        private IFooService GetRealFooService()
        {
            if(this.realFooService == null)
            {
                this.realFooService = CreateRealFooService();
            }
            return this.realFooService;
        }
    
        private IFooService CreateRealFooService()
        {
            // You could inject the service factory in the constructor of the proxy and use it here to create the real service.
        }
    }
    
    class YourController
    {
    
                              Inject FooServiceProxy here
                              v
                              v 
        public YourController(IFooService fooService)
        {
             this.fooService = fooService;
        }
    
        public void YourAction()
        {
             this.fooService.Bar();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.