For all DI examples I have seen, I always see the dependencies as other classes, like services. But an object may depend, heavily and/or crucially in fact, on configuration values such as Strings and resource wrappers (File/Path/URI/URL, as opposed to an entire big value string/document, or a reader).
Note, this is about the DI design pattern in Java or C# syntax alone, not how any particular DI framework handles this.
For example, let's say I have this class which returns a String (relative path, based on some obscure implementation logic). It (rather its various implementors) has a configuration/initialization dependency on the "projectLocation", as a user could have various projects on their machine and this class will perform some logic based on a given project whenever it is called.
public abstract class PathResolver {
protected File projectFilesLocation;
public RoutinePathResolver(File projectFilesLocation) {
this.projectFilesLocation = projectFilesLocation;
}
public abstract String getPath(String someValue);
}
I am not using DI just for unit-testing (gasp I'm not even unit testing, existing project). I just want to separate my dependency/creational concerns and logic concerns period.
Provided that the thing you want to inject, e.g., a file location, is something that would be directly used by the class then it is perfectly valid to inject it.
In the case of an Object
such as a File
or a String
then this is no different to something called Service. It is a dependency of your class thus DI applies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With