About Unit Of Work Pattern :
Unit of Work design pattern does two important things: first it maintains in-memory updates and second it sends these in-memory updates as one transaction to the database
Assume that I need to use a web service and update a DB table in same unit of work. Conventionally, I perform these steps:
- Open a connection.
- Call DB
Insert
method (there is no risk if it is performed, but it is not FLUSHED at that moment in UOW ). - Call an external web service (an any service from other company).
If web service results is 200 (OK) then call commit, else rollback.
using (var unitOfWork = _unitOfWorkManager.Begin()) { _personRepository.Insert(person); externalWebService.IncrementPeopleCount(); unitOfWork.Complete(); }
unitOfWork
has one method: Complete()
.If web service gets error, it's not problem.Because I can throw exception, so complete() method doesn't execute. But I get error in complete() I must reverse web service?
It would work fine if Insert() method transactional is performed in DB (not commit yet).
How can I do this scenario in Unit of Work pattern? Or is it absolutely necessary to have a reverse web method DecrementPeopleCount
?