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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:55:27+00:00 2026-05-29T06:55:27+00:00

I don’t like constructor based dependency injection. I believe that it increases code complexity

  • 0

I don’t like constructor based dependency injection.

I believe that it increases code complexity and decreases maintainability, and I’d like to know if there are any viable alternatives.

I’m not talking about the concept of separating implementation from interface, and having a way of dynamically resolving (recursively) a set of a objects from an interface. I completely support that. However, the traditional constructor based way of doing this seems to have a few issues.

1) All tests depend on the constructors.

After using DI extensively in an MVC 3 C# project over the last year, I find our code full of things like this:

public interface IMyClass {
   ...
}

public class MyClass : IMyClass { 

  public MyClass(ILogService log, IDataService data, IBlahService blah) {
    _log = log;
    _blah = blah;
    _data = data;
  }

  ...
}

Problem: If I need another service in my implementation I have to modify the constructor; and that means that all of the unit tests for this class break.

Even tests which have nothing to do with the new functionality require at least refactoring to add additional parameters and injecting a mock in for that argument.

This seems like a small issue, and automated tools like resharper help, but its certainly annoying when a simple change like this causes 100+ tests to break. Practically speaking I’ve seen people doing dumb things to avoid changing the constructor rather than bite the bullet and fix all the tests when this happens.

2) Service instances get passed around unnecessarily, increasing code complexity.

public class MyWorker {
  public MyWorker(int x, IMyClass service, ILogService logs) {
    ...    
  }
}

Creating an instance of this class if only possible if I am inside a context where the given services are available and have automatically been resolved (eg. controller) or, unhappily, by passing the service instance down multiple chains of helper classes.

I see code like this all the time:

public class BlahHelper {

  // Keep so we can create objects later
  var _service = null;

  public BlahHelper(IMyClass service) {
    _service = service;
  }

  public void DoSomething() {
    var worker = new SpecialPurposeWorker("flag", 100, service);
    var status = worker.DoSomethingElse();
    ...

  }
}

In case the example is not clear, what I’m talking about is passing resolved DI interface instances down through multiple layers from no reason other than at the bottom layer they are needed to inject into something.

If a class does not depend on a service, it should be have a dependency on that service. This idea that there is a ‘transient’ dependency where a class doesn’t use a service but simply passes it on is, in my opinion, nonsense.

However, I don’t know of a better solution.

Is there anything that provides the benefits of DI without these problems?

I’ve contemplated using the DI framework inside the constructors instead as this resolves a couple of the problems:

public MyClass() {
  _log = Register.Get().Resolve<ILogService>();
  _blah = Register.Get().Resolve<IBlahService>();
  _data = Register.Get().Resolve<IDataService>();
}

Is there any downside to doing this?

It means that the unit tests must have ‘prior knowledge’ of the class to bind mocks for the correct types during test init, but I can’t see any other downsides.

NB. My examples are in c#, but I’ve stumbled into the same issues in other languages too, and especially languages with less mature tooling support these are major headaches.

  • 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-29T06:55:28+00:00Added an answer on May 29, 2026 at 6:55 am

    In my opinion, the root cause of all the problems is not doing DI right. The main purpose of using constructor DI is to clearly state all the dependencies of some class. If something depends on something, you always have two choices: making this dependency explicit or hiding it in some mechanism (this way tends to bring more headaches than profits).

    Let go through your statements:

    All tests depend on the constructors.

    [snip]

    Problem: If I need another service in my implementation I have to modify the constructor; and that means that all of the unit tests for this class break.

    Making a class depend on some other service is a rather major change. If you have several services implementing the same functionality I would argue that there is a design issue. Correct mocking and making tests satisfy SRP (which in terms of unit tests boils down to “Write a separate test per each test case”) and independent should resolve this problem.

    2) Service instances get passed around unnecessarily, increasing code complexity.

    One of the most general uses of the DI is to separate object creation from business logic. In you case we see that what you really need is to create some Worker which in turn needs several dependencies which are injected through the whole object graph. The best approach to resolve this issues is to never do any news in the business logic. For such cases I would prefer to inject a worker factory abstracting business code from actual creation of the worker.

    I’ve contemplated using the DI framework inside the constructors instead as this resolves a couple of the problems:

    public MyClass() {
      _log = Register.Get().Resolve<ILogService>();
      _blah = Register.Get().Resolve<IBlahService>();
      _data = Register.Get().Resolve<IDataService>();
    }
    

    Is there any downside to doing this?

    As a benefit you will get all the downsides of using the Singleton pattern (untestable code and a huge state space of your application).

    So I would say that DI should be done right (as any other tool). The solution to your problems (IMO) lies in understanding DI and education of your team members.

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

Sidebar

Related Questions

I don't know if this has been asked before, but what i'd like to
Don't know if there is a better way to do this, so that is
Don't let below code scare you away . The question is really simple, only
Don't be scared of the extensive code. The problem is general. I just provided
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
I don't know when to add to a dataset a tableadapter or a query
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question

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.