Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are value objects valid dependencies for DI design pattern?

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.

like image 975
Zombies Avatar asked Mar 21 '13 11:03

Zombies


1 Answers

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.

like image 158
James Avatar answered Oct 28 '22 11:10

James