I've recently disputed with my colleague about the following situation:
I've developed a framework that processes the resources (in java, although its not really important). It goes (simplified, pseudo-code) like this:
class Algorithm {
Processor processor = ...;
execute() {
forEach(Resource r : readResourceNamesFromFileSystem()) {
DomainObject do = convertResourceToDomainObject(r);
processor.process(do);
}
}
}
interface Processor {
process(DomainObject do);
}
Class Algorithm is an entry point to my framework. Resource is a representation of some content in the filesystem (it has methods for reading the data from file and the file name, which is important)
Class DomainObject (declaration is omitted) is a java representation of the content of the file depicted by Resource. DomainObject only has data and doesn't have a filename inside.
Class Processor represents the business logic layer, the DomainObject can be processed, stored in Database and so forth.
The content data is supplied by others, we just know that these files contain the data that can be converted into DomainObject(s) which is a real object we work with in the framework. We store one DomainObject in one file.
Now we argue about the file name. Colleague of mine wants the file name to be propagated into the Processor and potentially to all the layers because its easy to print the logging message/throw an exception that contains a file name if something goes wrong during the processing, or maybe to write the message like "domain object stored in file (here__comes__filename) has been processed successfully".
I see the point of my colleague (convenience) but I'm sure it breaks the design and encapsulation because the processor module shouldn't really be aware of the "origin" of the DomainObject (today we store them in files one DomainObject per file, tomorrow we'll maybe use something else, something that doesn't necessarily has a filename).
I claim that we better catch the exception in the Algorithm.execute method and rethrow the exception with a filename/ log the filename. The log file will be less readable but the encapsulation won't be broken.
In my understanding its a design vs clearness. Could you please share your opinion, who is right, who is wrong? Should I sacrifice the design for the clearness of logs?
BTW I've promised my colleague to ask this question so we'll read the possible answers together :)
Thanks a lot in advance and have a nice day
source
property toDomainObject
and set it to the filename when loading from a file. This provides the info your colleague wants without breaking encapsulation or leaking the abstraction too much.