I am designning project structure follow DDD artchitecture & C# technologies. I am facing an issue with out date DTO mapping to Domain Model and persit the out date data to database.
I have 2 distributed clients (Desktop) getting data for the Student has ID=1
Server return DTO for Student has ID=1 with bellow information to Client 1 & 2 like that: Student:
The Client 1 getting the DTO and Update phone number of this Student to: 56789 and permit the save action ---> Database have persited the new phone value to 56789.
The Client 2 getting the DTO and Update Full name of this Student to: Peter and permit the save action ---> Database have persited the new Full name: Peter and the old phone number: 01234 (Un-expected behavior)
Root cause: - When perform Saving action, the client return the modified DTO to Server and mapping to Domain Model. The DTO keeping the old value (Phone number: 01234) and mapping this value to Domain Model.
Would you please advise me a way or a pattern or a technology for preventing this issue? This point is very important because the application can have multiple person modified the same application model at the same time.
Thank you so much
It's called a concurrency problem. There are many ways to deal with it. RDMS products like SQL Server or Oracle, for example, have built-in concurrency handling features that can help (check your database vendor's documentation). Study and utilize them first, then add your preferred programmatic approach. I've seen the following done in some of the applications I supported, each with their own problems so you'll have to test what works best for your applications and how your users operate.
There are many other ways, each with their own problems as well. They can be simple to sophisticated. Named-patterns can include OCC, event sourcing and messaging, for example. Regardless, the assumption is that at least one request can win "first come, first serve" and all other requests handled to react accordingly.
Hope this helps!