Are values ​​objects valid dependencies for the DI design pattern?

For all the DI examples I've seen, I always see dependencies like other classes, such as services. But an object can depend heavily, mainly and / or to a decisive extent, on configuration values ​​such as strings and resource wrappers (file / path / URL URI / URL, rather than an entire line or document of great importance) or <reader> .

Please note: this is a DI design pattern only in Java or C # syntax, not how any specific DI environment does it.

For example, suppose I have this class that returns String (a relative path based on some obscure implementation logic). It (rather, its various developers) has a configuration / initialization dependency on "projectLocation", since the user can have different projects on his machine, and this class will execute some logic based on this 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 don’t use DI only for unit testing (suffocating, I don’t even test unit, existing project). I just want to separate my addiction / creation problems and logical moments.

+7
source share
1 answer

Provided that the thing you want to enter, such as the location of the file, is what will be used directly by the class, then it is absolutely true to introduce it.

In the case of an Object , such as File or String , this is no different from what is called Service. This is a dependency of your class, so DI applies.

+3
source

All Articles