Almost every software has a number of internal data structures that we don't want to expose externally (through APIs for example).
Let's say we have a few classes representing 1:1 database tables -
Then we can have an external class
ExternalUser that will be exposed on an API, exposing some user details and user group details.
We have two options for what the properties of
ExternalUser can be:
Copying properties -
ExternalUserhas a copy of a subset of properties or derived properties from
ExternalUsercan have for example
userGroups, etc. as properties.
1.2 All those properties will be populated on object creation and copied from the internal
Wrapping Object -
ExternalUserhas a references to
ExternalUserwill have a private
userGroupthat will hold reference to the internal models.
2.2 Methods like
getNamewill delegate to the internal
I can see pros and cons of both in terms of leaking data, memory use and session management if you are dealing with transactions.
Is there any pattern / standard or a reason why one option is much better than the other?