Closed fuine6 closed 5 years ago
If for certain use cases you expect concurrent calls whose simultaneous write breaks business rules, you can use one more method besides the methods you mentioned - repeat command processing.
Then validation on the command handler / domain model will detect broken rule (because side effect of another command is already persisted) and the application will return an appropriate message.
The important thing is to have always all validation on the application side and treat database as "last line of defense" against such situations.
You can use the Polly library to implement this behavior
The important thing is to have always all validation on the application side and treat database as "last line of defense" against such situations.
I think this is a good idea. It helped me a lot.
Sorry, my English is poor.
If the customer's email has unique constraint. When call RegisterCustomer api with duplicate email, the method in UnitOfWork.CommitAsync will throw DbUpdateException, how to handle this and return friendly message to frontend, like this
the errorCode is unique which defined by application, and has a api document for frontend, like this
frontend use errorCode to show user friendly message.
I found most solution is,
and use a ExceptionFilter to catch EmailAlreadyExistException.
But this can't handle concurrent insert, if two requests send at same time with same email, two requests will pass EmailAlreadyExist, the method in UnitOfWork.CommitAsync will throw DbUpdateException, this solution don't solve problem.
Maybe this happen rarely, I can return a response with 500(Internal Server Error), and let user try again.
Maybe I can use a lock in database, but most suggest don't do this, because of performance.
Maybe I can use Optimistic Concurrency Control, so I need write my code like this
But it may has many change in a database transaction, I can't know who throw the DbUpdateException.
If I has a situation, user can update same record, and it happen frequently, user may complain.
How to handle this gracefully.