Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    The potential issue with the proposed solution is that it often leads to separate constructors for separate use cases, e.g. a "mock" constructor and a "real" constructor. This starts defeating the purpose of DI and unit testing as the test code path has partially diverged from the production code path. I'm not saying this always leads to issues, but it's a "straw that broke the camel's back" situation. It would be a much better approach to only have DI-friendly constructors, which guarantess that you use the exact same logic in your test and production runtimes. Commented Mar 19, 2019 at 9:08
  • Of course it is and I just try to show some way of this. Also there is no need to create parameter-less constructor for B and C class and even A class. It is better to create factory method Service Locator but, as I said before, I try to show way of that using Service Locator (coming from Container) is not mandatory. Commented Mar 19, 2019 at 9:15
  • I removed redundant parameter-less constructor from B and C class and add Edit Note. Commented Mar 19, 2019 at 10:59
  • 2
    @Flater: There is nothing wrong with having a "paremeter-less" constructor, as long is it ultimately calls the same constructor that your tests or "DI" uses. The A() constructor should just call the A(IDeviceManager) constructor. That way your test code is your production code. Commented Mar 19, 2019 at 11:22
  • @GregBurghardt: Having the class define its own dependencies (regardless of whether it's the mock, real, or both) defeats the purpose of dependency injection where that responsibility is passed to whoever consumes (an object of) that class. Commented Mar 19, 2019 at 11:25