Skip to main content

Where to store the ID’s of the entities used by external services

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?


  1. 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..
    Better would be to reverse the dependency - in a class specific to that external service to store a reference to the User class.

    1. @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.


Post a Comment

Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

Fundamental Books of a Software Engineer (version 2018)

More then six years ago I wrote a blog post about fundamental books that any software engineer (developer) should read. Now it is an excellent time to update this list with new entries.

There are 5 different categories of books, that represent the recommended path. For example, you start with Coding books, after that, you read books about Programming, Design and so on.
There are some books about C++ that I recommend not because you shall know C++, only because the concepts that you can learn from it.


Writing solid codeCode completeProgramming Pearls, more programming pearls(recommended)[NEW] Introduction to Algorithms


Refactoring (M. Fowler)Pragmatic ProgrammerClean code[NEW] Software Engineering: A Practitioner's Approach[NEW] The Mythical Man-Month[NEW] The Art of Computer Programming


Applying UML and Patterns (GRASP patterns)C++ coding standards (Sutter, Alexandrescu)The C++ programming language (Stroustrup, Part IV)Object-oriented programming (Peter Coad)P…