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?

Comments

  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.

    ReplyDelete
    Replies
    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.

      Delete

Post a Comment

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

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 http://go.microsoft.com/fwlink/?LinkId=260882 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.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...