Let's say I'm having a domain-model and I want to read and save it from any persistence layer - right now it might be a json file but in the future it could be xml or a database (which might also change in its type).
To generate the domain-model from the persistence layer, I've got an implementation of a simple Interface which, let's say, contains of a getAll()
and saveAll()
method. If I want to switch to another type of persistence I can then simply change the implementation of the interface. However, inside the implementation I'll use completely different solutions to read and store the data so I'll have to use different objects from other libraries to deal with the data.
Let's say I use a Json serializer in the first implementation, I'll then instantiate the instance of that serializer in my implementation directly. This will then lead to my implementation directly depending on that serializer, I can never give it another one. But this wouldn't be possible anyway, because there's no universal interface for serializers (or whatever kind of persistence). So if I want a to use a different serializer, the only thing I can do is write a completely new implementation instead of just passing in another one from the outside.
So is it okay to hard code dependencies in this case? Or is there a better option?