A normal medium or complex application that discuss with different services of 3rd parties or stores data in the different repositories has different modules for each communication layer. In general, we can have a different component for each external service that is used (as in the image below).
Each external service usually has its own custom entities that are used to send data on the wire. For example the user entity for Twitter, Facebook and your own repository would be different. Each of this modules would have a wrapper for that specific service that is used over the wire (and to convert the entity to the POCO of the specific service).
It is not important where the logic from the yellow box is (in the same class, in different class or modules). But in one way or another we would have something like this.
The logic of your application could work with specific objects that are defined only in your application. For example you could have your own entity User, which would contains all the attributes and functionality that are supported by your application.
Let’s assume that the entity that is stored in DB is called UserDB, the own used by Facebook module is called UserFB and the one for Twitter is UserTW. For example when you want to save the attributes from User to the DB, you would need to obtain an instance of UserDB, update the fields and save them. It is not important where this action take place for us for now. Our question is how you need to identify the UserDB entity for the given user?
One possibility would be store the UserDB ID from the database in our application (in User or in another location). This action would happen for UserFB and UserTW. This may be a good solution that would work for us.
If we could not identify the user with other unique identification attributes, this solution may be the only one that we could use.
The only thing that is happening in this situation is that you create an indirect reference from your logic module that contains the User entity to the other external services (modules). Even if this is not a direct reference, you have an information that can be found in User and is used to identify the unique user in other services used by your application.
A possible solution would be to create a mapper in each modules user to communicate with different services (in the wrapper module), that can match and get the unique id of the user for each external services.
This solution may be better than the one that has the UserDB id, UserFB id and UserTW id in the User entity, but you would have to manage the mapping between them (this is not all the time an easy task).
The happiest scenario is to have in the User entity some attributes that can be used to unique identify a user in each services that is used. For example the social number can be used in the UserDB and user email could be used for Facebook and Twitter. In this way we would not need the mapper between different entities in each module. It would be pretty simple to retrieve the entity that it is needed based on this information. In this case we would not need anymore the mapper, we could directly retrieve the entity from the service based on the specific attribute(s)
What other solutions would you try to use?
Each external service usually has its own custom entities that are used to send data on the wire. For example the user entity for Twitter, Facebook and your own repository would be different. Each of this modules would have a wrapper for that specific service that is used over the wire (and to convert the entity to the POCO of the specific service).
It is not important where the logic from the yellow box is (in the same class, in different class or modules). But in one way or another we would have something like this.
The logic of your application could work with specific objects that are defined only in your application. For example you could have your own entity User, which would contains all the attributes and functionality that are supported by your application.
Let’s assume that the entity that is stored in DB is called UserDB, the own used by Facebook module is called UserFB and the one for Twitter is UserTW. For example when you want to save the attributes from User to the DB, you would need to obtain an instance of UserDB, update the fields and save them. It is not important where this action take place for us for now. Our question is how you need to identify the UserDB entity for the given user?
One possibility would be store the UserDB ID from the database in our application (in User or in another location). This action would happen for UserFB and UserTW. This may be a good solution that would work for us.
If we could not identify the user with other unique identification attributes, this solution may be the only one that we could use.
The only thing that is happening in this situation is that you create an indirect reference from your logic module that contains the User entity to the other external services (modules). Even if this is not a direct reference, you have an information that can be found in User and is used to identify the unique user in other services used by your application.
A possible solution would be to create a mapper in each modules user to communicate with different services (in the wrapper module), that can match and get the unique id of the user for each external services.
This solution may be better than the one that has the UserDB id, UserFB id and UserTW id in the User entity, but you would have to manage the mapping between them (this is not all the time an easy task).
The happiest scenario is to have in the User entity some attributes that can be used to unique identify a user in each services that is used. For example the social number can be used in the UserDB and user email could be used for Facebook and Twitter. In this way we would not need the mapper between different entities in each module. It would be pretty simple to retrieve the entity that it is needed based on this information. In this case we would not need anymore the mapper, we could directly retrieve the entity from the service based on the specific attribute(s)
What other solutions would you try to use?
The biggest problem with storing a reference/id in the User class/entity to each external service is that it doesn't play nice with open-closed principle - each time support for a new external service will have to be implemented in the application, the User class will have to be modified..
ReplyDeleteBetter would be to reverse the dependency - in a class specific to that external service to store a reference to the User class.
@Tudor, this is another good ideas. Is similar with the Mapper from each external service module, that can map the logic entity (app entity) to the one used by external service.
Delete