Hacker News new | past | comments | ask | show | jobs | submit login

I'm not following. How is dependency injection like inheritance and also useless?



I think GP is saying that dependency injection is like currying, not that it's like inheritance.


"Favor composition over inheritence" is like "Favor pipelining over currying".


I'm still not getting the relationship to dependency injection. What is pipelining?


Dependency Injection is just another way to curry. In both you are passing an argument. SPBS's post[0] is very similar.

All together, "Favor composition over inheritence" is like "Favor pipelining over currying/dependency injection".

There's an example of a pipeline down the thread in p2t2p's comment[1].

[0]: https://news.ycombinator.com/item?id=28583343

[1]: https://news.ycombinator.com/item?id=28584270


But why do you have to curry to inject your dependencies? Dependency injection is satisfied by moving internal constructs in your function or method into parameters, no?


That's the style that functional programmers prefer. It makes it slightly easier to work with as you can now pass them together as one. Most OOP dependency injection is done through interfaces, but with the functional approach where you have functions as first class citizens you can just pass functions around. And as the best interface is a one function interface, that's the ideal way to do it.

The point being, essentially they are the same thing, they hold the same power, and it's just a stylistic difference.


I’m still not seeing it. As a Java dev by day, I see DI mostly through the lenses of Hollywood principle and IOC containers, neither of which bear much resemblance to currying (as I see it). When it comes to something like DI frameworks, currying is certainly not as powerful, but I don’t think that’s what you mean. Currying seems much more related to something like the builder pattern. Maybe there’s some article you know of that I could read? Googled around but didn’t find anything.


So there is two types of DI, one way automatically wired through an IOC container, and the other manually wired through constructors. They both are the same thing, but one uses some configuration and magic to make it easier to pass around these interfaces.

Mark Seeman's blog[0][1][2](many more related articles, these are particularly related) helped me grasp some of this stuff.

[0]: https://blog.ploeh.dk/2014/06/10/pure-di/

[1]: https://blog.ploeh.dk/2017/01/30/partial-application-is-depe...

[2]: https://blog.ploeh.dk/2017/01/27/dependency-injection-is-pas...




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: