Timeline for Initialization of objects in a system using dependency injection
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 26, 2014 at 20:39 | comment | added | Robert Harvey | I think you're getting a little hung up on automatic instantiation. The principle of inversion of control is that whoever is instantiating an object is responsible for handing that class whatever dependencies it needs through its constructor method. That's it; nothing else follows. | |
| Mar 26, 2014 at 20:38 | comment | added | Erik Sapir | So what you are saying is that it is Ok if B will create C by itself? Isn't it a violation of DI? | |
| Mar 26, 2014 at 20:32 | comment | added | Robert Harvey | Create a D, and hand it to the constructor of C. What happens after that is up to you... the remainder of the objects can create their own objects, or you can new up the entire chain yourself. The point of DI is not automatic instantiation of objects; it's about inversion of control. | |
| Mar 26, 2014 at 20:28 | comment | added | Erik Sapir | Not sure what you mean. Lets assume that A is the entry point to the application. We want to create B. In order to create B, C should first be created and in order to create C, D should first be created. Should A be responsible to create D, C and B? Or are there better practices | |
| Mar 26, 2014 at 20:24 | comment | added | Robert Harvey | You do, in whatever way that you would do that in C++ canonically (in C# or Java, it would be new). That first object creation is what triggers the creation chain. | |
| Mar 26, 2014 at 20:22 | comment | added | Erik Sapir | In my example C received D as a dependency and B receives C as a dependency. So who constructs D? | |
| Mar 26, 2014 at 20:21 | history | answered | Robert Harvey | CC BY-SA 3.0 |