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 - User
, UserGroup
, GroupPermission
, etc.
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 -
ExternalUser
has a copy of a subset of properties or derived properties fromUser
andUserGroup
1.1.
ExternalUser
can have for exampleusername
,fullName
,userGroups
, etc. as properties.1.2 All those properties will be populated on object creation and copied from the internal
User
andUserGroups
Wrapping Object -
ExternalUser
has a references toUser
andUserGroup
.2.1
ExternalUser
will have a privateuser
anduserGroup
that will hold reference to the internal models.2.2 Methods like
getName
will delegate to the internaluser.getName
, etc.
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?