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 …

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.