Introducing Concurrency

Ive been reading some articles to introduce Optimistic Concurrency into my application, so if two users are editing a single record they are given options to either lose their changes and reload or save over the current changes so the current users changes is the final save.

Im making this amendment in the UoW class and have a try catch block. Ive added RowVersion to my table and marked it with TimeStamp. During debug i can amend the record and the error occurs.

The issue im finding it how do i bubble up the message to the end user as the try catch block (from the articles i have read) only seems to target the code and not a way to get the user to decide? I want a message to be shown to the end user and let them decide what to do?

Anyone? I was hoping for some idea when using this pattern.

When your server encounters the optimistic concurrency error (because the record has already changed), it could return a special error to the client, for example the HTTP status code 409 Conflict. When the client application receives this error, it could give the user the option to load the updated record or to save over those changes.

I have a UnitOfWork class which has a Complete method (this was listed on the course)

        public int Complete()
        {
        return  _context.SaveChanges();

So do i check this within the Try Catch block and when i returns a value other than 1 (which i think is a successful save) then i assume i return that to the client?

If that is correct how do i send a value back to the Complete method to state to override and save the changes or even reload the record?

You’ll just be doing a whole new save using the latest concurrency timestamp. So either way your client will need to fetch the updated record so that you can either 1. display it to the user or 2. use its concurrency timestamp for the new save.