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 6842357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:05:25+00:00 2026-05-27T00:05:25+00:00

When using IoC for dependency injection the most recommended use is constructor injection (as

  • 0

When using IoC for dependency injection the most recommended use is constructor injection (as told by many articles), my question is :
Is it better to inject using the constructor parameters or passing the IoC container through the constructor to inject the classes needed, and what is more useful for unit testing ?

  • 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-27T00:05:26+00:00Added an answer on May 27, 2026 at 12:05 am

    Although passing the container through the constructor is better than making the container accessible as a singleton for the complete application, it is still a form of Service Locator (anti-pattern), and is not recommended. This has clear disadvantages:

    1. It makes your code much harder to follow.
    2. It makes it much less obvious when the Single Responsibility Principle is violated (since the class hides which things it depends on).
    3. It makes it much hard to test, since you need to pass a configured container and you need to look in the code to see what the test need.
    4. When requesting instances from the container directly from within every class, you will disable many features that IOC containers give you, because you don’t allow the container to build up the object graph. Depending on the framework of choice, certain lifestyles and features like context based injection will not be usable.
    5. All your tests use a container instance, making your tests come complex, and all your test have a dependency on the DI framework, which makes it very expensive to switch to another framework when needed.

    Just inject the dependencies into the constructor, never* the container itself.

    *Exception to this rule is when such class is located inside the appliation’s Composition Root. In that case it’s not considered to be the Simple Locator pattern, because that class is simply an infrastructure component.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On our current WPF project, we've been performing dependency injection using the Ninject IOC
I've got a project using Spring IoC for dependency injection, and I'm trying to
When using dependency injection (DI) and inversion of control (IoC) objects will typically have
I've been using Dependency Injection (DI) for a while, injecting either in a constructor,
I'm using IoC container for dependency injection in Asp.Net MVC 3 and everything seems
What is the best way to transparently inject dependencies (using IOC container) to user
When using an IoC container, is it considered good design to inject other classes
It was recommended to me that, when using an IOC container, I should change
I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft,
I'm a beginner on IoC and dependency injection. I'm reading about it, but I

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.