1

After years of people trying to convince me that Singletons Are Evil, I'm finding I have to use Dependency Injection because it's the latest design pattern (like singletons once were). But I can't get away from the code smell: passing around dependencies, sometimes deep down into a view hierarchy, is just another anti-pattern that we used to call tramp data (data that only gets passed into an entity just so it can be passed on to another entity).

I'm not asking this to provoke anyone, I genuinely want to know what people think about this and, more importantly, how are they dealing with it? I have seen first-hand insanely long chains of DI that took maybe half a day of solid hacking to implement, simply to remove a singleton. And then someone decided to add another dependency, and we had to go through the same process again. I do not believe this was worth the effort, and all those complex interfaces create fragility and future problems.

2
  • 1
    This may be more suited to the Software Engineering site. Check their help page to be sure. If you decide to post there, delete this one. Commented Nov 14, 2017 at 17:42
  • DI has particular reasons to exist and solves particular tasks. Probably you are abstracting to interfaces wrong entities and that's why you feel uncomfortable doing that. Definitely, you should not pass any interfaces to views, as you can pass there data itself. DI isn't for everything, it's just a tool to make testing easier and implementation changes more flexible. Commented Nov 15, 2017 at 8:38

1 Answer 1

0

Singletons are not evil. But they can be, when used inappropriate.

They are perfectly fine if and only when:

  1. You only need one of something.
  2. You need that one thing to be passed around in a large number of method calls.

For example, managing external resources, like hardware or state full API's.

But they are evil if one uses them just to create global accessible variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.