Timeline for Do you usually instantiate dependency objects in the constructor?
Current License: CC BY-SA 4.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 2, 2021 at 16:05 | vote | accept | Fatima | ||
| Jun 8, 2021 at 6:50 | answer | added | Martin Maat | timeline score: 1 | |
| Jun 7, 2021 at 5:20 | comment | added | Fatima | @FilipMilovanović Yes, I made a mistake, fixed it. Thank you for explanation, I got it now | |
| Jun 7, 2021 at 5:19 | history | edited | Fatima | CC BY-SA 4.0 | deleted 2 characters in body |
| Jun 7, 2021 at 5:18 | comment | added | Fatima | @gnat Thanks for your suggestion, but actually it doesn't help, but I found a great answer below | |
| Jun 6, 2021 at 20:47 | comment | added | Filip Milovanović | I just realized dependency: AnotherClass might just be a default parameter. But shouldn't it then be @dependency = dependency? In any case, passing in a class is actually a similar process and the same overall idea, just a slightly different design - it's like passing in a factory that can create the instance for you at a later time. It's a known approach, that in other languages might manifest as say, passing in a lambda. The injected class is the factory; it's essentially an object that exists outside of your class, that you then pass in. | |
| Jun 6, 2021 at 20:37 | comment | added | Filip Milovanović | "it doesn't make sense to instantiate the dependency object in the constructor" - but that's not what's usually done in dependency injection; usually, the dependency is instantiated by some 3rd piece of code, outside of the class, and the object is passed in as a parameter. I'm not a Ruby-ist, but if I read your code correctly, it seems like you're storing the type of the dependency (ignoring the actual parameter completely), and than instantiating that type later on. But, unless I'm mistaken, that won't allow you to pass in an instance of a subtype, while passing/storing an object will. | |
| Jun 6, 2021 at 17:57 | answer | added | Doc Brown | timeline score: 6 | |
| Jun 6, 2021 at 17:31 | comment | added | Doc Brown | @gnat: I am not sure you understood the question - to be honest, I am pretty sure you did not. | |
| Jun 6, 2021 at 15:06 | review | Close votes | |||
| Jun 11, 2021 at 3:03 | |||||
| Jun 6, 2021 at 14:47 | comment | added | gnat | Does this answer your question? Legitimate "real work" in a constructor? | |
| Jun 6, 2021 at 14:45 | history | asked | Fatima | CC BY-SA 4.0 |