Timeline for concerns about constructor dependency injection logic
Current License: CC BY-SA 3.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 13, 2018 at 14:19 | comment | added | Ewan | Which essentially boils down to 'labeling' when you think about it. | |
| Apr 13, 2018 at 14:18 | comment | added | Ewan | "the constructor should throw an exception" so have changed the subject to constructor injection + validation of parameters in the constructor. To which I would reply Parameter Injection + DI Framework which checks the (labeled) parameters have all been injected. gives you the same protection. | |
| Apr 12, 2018 at 11:10 | vote | accept | dkokkinos | ||
| Apr 13, 2018 at 11:37 | |||||
| Apr 11, 2018 at 15:09 | comment | added | Robert Harvey | @Ewan: That was just a salvo. Here's the complete thought: constructor injection is about creating valid objects. If a valid object cannot be constructed from the arguments provided to the constructor, the constructor should throw an exception. So it's about more than just "labeling." I mentioned immutability because property injection modifies the object's internal state from the outside, and you can't have that happening (without further validation) if you expect to maintain a consistently valid object. | |
| Apr 11, 2018 at 13:44 | comment | added | JimmyJames | @RobertHarvey Instead of 'true' immutability, I would offer 'deep' immutability as an alternative. It's not necessary to have immutability all the way down in order to get benefits from this. | |
| Apr 11, 2018 at 6:54 | history | edited | Ewan | CC BY-SA 3.0 |
added 52 characters in body
|
| Apr 11, 2018 at 6:30 | comment | added | Ewan | It is slightly annoying that your arguably incorrect comment gets more votes than my techincally correct answer | |
| Apr 10, 2018 at 20:32 | comment | added | Robert Harvey | True immutability in an object-oriented language is harder than most people think it is. You don't need to pass value types, but you do need to pass objects that are themselves immutable. | |
| Apr 10, 2018 at 20:12 | comment | added | Ewan | hmm I think my point is different, or rather that immutable construction requires value types, which is not usual for DI | |
| Apr 10, 2018 at 20:04 | comment | added | Robert Harvey | Yes, I'm having that discussion below Mark Seeman's answer right now. (He's the author of "Dependency Injection in .NET," by the way. Good book.) | |
| Apr 10, 2018 at 20:01 | comment | added | Ewan | ahh but if you pass in a reference type and keep a reference to it, say a list, you can alter it from outside | |
| Apr 10, 2018 at 19:58 | comment | added | Robert Harvey |
Constructor injection beats setter injection for only one reason... -- Not quite. Constructor injection is associated with immutable objects.
|
|
| Apr 10, 2018 at 19:34 | history | answered | Ewan | CC BY-SA 3.0 |