Question

In my project I'm using repository pattern for business-layer. Reading alot about repository pattern I haven't found any information on how to keep business objects synchronized with data.

Lets say I've loaded some data, through mapping created my business objects and saved them to repository. My repository implementation maintains change tracking so I can easily update underlying data layer.

The application is n-tier application with multiple clients, and (possibly) multiple business layer instances on different independent servers.

The problem:
How to maintain data consistency between business objects in repository and respective data in database?

Is there any patterns or best practices on when and how to update business objects who's data was changed in database while they are cached in repository?

Was it helpful?

Solution

When dealing with one Aggregate Root (AR) at a time, things are simple since the Repository should wrap anything realted to that AR in a transaction (or commit). The problem is when dealing with multiple ARs and/ore when the persistence doesn't support transactions.

For that case, the solution is a bit tricky as it involves beign comfortable with the concept of eventual consistency (aka things will be consistent... eventually), message driven architecture, saga and idempotency.

In a nutshell, one Ar is updated which generates one or more events. The other AR subscribe to those events and update their state as well generating other events. The infrastructure, the message bus in this case, will ensure that each event is published at least once. Things can go wrong but the message bus will eventually deliver the messages (but not quite in the same milisecond).

The event handler idempotency ensures that an operation can be repeated without changing the initial result, while the saga manages the flow of events and possbile commands (as a result of an event).

So, all the business objects involved will be in sync eventually, but it will take a bit of time to achieve that (from 1 milisecond to minutes and hours).

It's not easy and straightforward, however it's not THAT hard and it's very usable both in local and disitrbuted scenarios.

For a distributed app you can use NServiceBus

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top