Re:You probably don't need a dependency injection framework
by pdpi on Sunday May 25, 2025 @05:17PM
(#44091220)
If the class instantiates its own dependencies then, by definition, you're not injecting those dependencies, so you're not doing dependency injection at all!
[ Parent
| on HN ]
Re:You probably don't need a dependency injection framework
by loevborg on Sunday May 25, 2025 @05:31PM
(#44091352)
That seems too dogmatic. Does the fact that Foo has a static function that knows how to create its dependencies disqualify the class from being a case of DI?
class Foo {
private nowFn: () => Date;
constructor(nowFn: () => Date) {
this.nowFn = nowFn;
}
doSomething() {
console.log(this.nowFn());
}
static create(opts: { nowFn?: () => Date } = {}) {
return new Foo(opts.nowFn ?? (() => new Date()));
}
}
Re:You probably don't need a dependency injection framework
by physPop on Sunday May 25, 2025 @06:15PM
(#44091704)
Yes because injecting nowFn is trivial and not a case for DI.
Consider a database handle, or network socket, or http response payload. Clearly each class shouldn't be making its own version of those.
Re:You probably don't need a dependency injection framework
by cyanydeez on Sunday May 25, 2025 @07:59PM
(#44092481)
You're nitpicking for no good reason. You can create global handles to each of those items, and let them instantiate with the class or override them with a create function.
Dependency injection boils down the question of whether or not you can dynamically change a dependency at runtime.