So I've read about this thing called SOLID (mixed with) Writing Testable Code. And then specifically about the D part. How does one follow these guidelines when using primitive types or Methods/Classes provided by the language library.
Does one also need to use Dependency Injection for arrays (java (new int[64]
) or class members of say, a FileWriter?. Do these need to be injected using DI or can the class still instantiate them?
How far should you go to following the above guidelines?
I am not looking for a language-specific answer (if possible). IMHO the answer should apply to, for ex, PHP, Python Java, heck, even C
You usually don't inject primitives or DTO/POJO-like objects. Reason is that those are:
FileWriter
is different as it's in exact opposition to points above. I can't simply stub it in test and make it work without few strong assumptions - like, I assume every future developer running this code will have certain file on his machine. This usually is no-go for unit tests and one of the reasons why DI is applied in those cases.
Those problems come from fact that FileWriter
serves as communication point between two unrelated components - your application and file system. Any class doing this kind of integration (between your app and DB/network/file/REST etc.) in most cases should be abstracted and injected.