I came across information on DI and DI in Angular.js.
My understanding is that DI in Angular.js allows controllers, factories, services, and other components to specify dependencies without having to create those dependencies.
Some questions arise:
- At some point, a dependency must be created, which means the place where the dependency was created is not truly "DIed." How should this be interpreted?
Considering the following scenario:
var thing = function(dep){ this.dep = dep || new depCreator(); }
Is this considered as utilizing DI? Or does it depend on whether
dep
is passed to the function?From my perspective, DI enables setting dependencies within functions or objects, essentially separating initialization/configuration/data from other parts of the program (such as logic). For example:
var dep1 = 'qwe'; var thing = function(dep){ this.dep = dep; } var diedThing = new thing(dep1);
This setup would allow for configuring
dep1
in a separate file.If plain JavaScript implementing DI looks like this:
var thing = function(dep){ this.dep = dep; }
As opposed to:
var thing = function(){ this.dep = new depCreator(); }
Is the former approach correct?
Additionally, if
depCreator
relies on configuration files or external configurations, can it still be considered DI?When referring to Angular.js having DI, does this mean that Angular.js automatically creates and manages dependencies? Is there another interpretation?
Finally, if DI is perceived as complex but ultimately aims to separate configuration from implementation (or logic), could this not simply be referred to as the single responsibility principle? In other words, each component fulfills its designated role independently?
In essence, DI appears to be a subjective concept, influenced by how one envisions and divides responsibilities within an application. Is this assessment accurate?
Apologies for the lengthy inquiry.