I agree with @Amir Samary. Even though by using inheritance you're allowed to use method generators, it's still against against the SIngle Responsibility Principle, since the code is binding directly to the persistence (which it's SRP should be to work as a storage representation).
Binding another responsibility to it could mean that sometime in the future the persistence would need to be rewritten to match the newer requirements, thus risking to break current applications and elevating technical debt.
So my golden rule is: unless you're 120% sure that the result generated by the binded class won't change, or you're 120% sure that it still submits to the SRP, only then you can "adapt" the persistent class.
But before that, I would recommend (priority order):
1 - Use an utility method that takes the binding class name and returns the JSON or vice-versa. Can take an optional configuration object or flags to overwrite and determine the serialization behavior for the current case.
2 - Create a mirror class that extends exclusively from the Adapter.
This keeps the persistent class clean and still allows it to be serialized.
Also, there's a method for doing what I suggest on 1, I think it's been discussed here already.
- Log in to post comments