0

I'm having to implement operations on a repository-like class that needs to maintain consistency between a local database and external(third party) APIs. Some of those API may only offer partial success/partial failure semantics(i.e. non atomic) for some operations. However, I'd like to make sure the operations I implement do have atomic/transactional semantics, such that e.g. I only update my database if the request to the API succeeded, and whatever ends up in that external service is also replicated in the database.

Are there any useful patterns to deal with that, or other pointers on how to solve that kind of problem?

Thanks!

Charles Langlois
  • 4,198
  • 4
  • 16
  • 25
  • It seems to me that you want to perform some operation after the completion of another operation. So, it looks like it will handled by using "State Design Pattern". You can elaborate by using diagram for better understand. – Gul Ershad Mar 02 '18 at 08:42

1 Answers1

1

The Command pattern seems like a good way to encapsulate operations that can be rolled back, and offer a uniform interface for those operations.

Then, the Unit of Work pattern can be used to define transactions involving multiple commands, where if any of them fails, each command is rolled back.

Charles Langlois
  • 4,198
  • 4
  • 16
  • 25