I'm struggling to understand why breaking the rules is considered bad.
import {DepClass} from './di-import' // <- some dependency imports here
class DI1 {
dep1: DepClass
constructor(){
this.dep1 = new DepClass() // <- not ideal
}
......
}
class DI2 {
dep2: DepClass
constructor(d: DepClass){ // <- slightly better
this.dep2 = d
}
......
}
So, I know that a class shouldn't create instances of its dependencies on its own as it breaks the IoC rule. But what are the negative consequences of this? What overhead occurs?
What is the difference between directly creating an instance of Dependency in the constructor and passing a copy of an already existing Dependency into the constructor as an argument? Other than both classes functioning correctly.
I have one thought though. Perhaps all of this is necessary for the proper functioning of the DI container, which carefully inspects constructor arguments.
Thanks in advance.