My existing application is a .NET 3.0 winform app that talks to a WCF service which in turn updates a MS SQL Server database; I can make changes at all spots (Windows client, WCF, server). I've had an inquiry into the possibility of allowing users to work offline and later sync their data back up. I have concerns about stale data mostly as the data is not "owned" by them and another user could have updated the same record while they were offline. I also use NHibernate for my ORM.

What are your thoughts as to the best implementation to add a sync feature?

3 Answers 3


The actual logistics of this aren't too tricky (last-modified date on each record, and a "disconnected-since" or "last-synced" timestamp stored somewhere), the real issue is probably going to be your conflict resolution technique. What should the system do when two people modify the same field in the same record?

I'd suggest logging all offline changes made to the database, then try to simply replay or apply those changes the next time the user syncs up. That's probably more efficient than simply checking each table to see if it has any changes made since the last sync time. You could also store both the old and new values for any fields that got changed. This would help with conflict resolution, since you could still update any fields that still contained their old values, even if the last-modified date indicated that the record had been changed.

  • "logging all offline changes" - I'm not familiar with methods/technologies that are used for this purpose. Could you elaborate more on this point? Dec 10, 2011 at 22:30
  • Basically, you store every SQL statement you execute while offline, then attempt to replay them when you sync. Your update statements should explicitly reference the previous values of the record (e.g., UPDATE contact SET phone_number = '234-5678' WHERE contact_id = 42 AND phone_number = '123-4567'. That way, if someone else already updated the record's phone number, your update won't overwrite theirs.
    – TMN
    Dec 11, 2011 at 2:36

Your requirements sound ideal for Microsoft's Sync Framework. Except for the NHibernate bit, Sync Framework needs to add a few columns to the data to handle synchronization, and those extra columns "don't play well" with NHibernate. A previous employer had an in-house developed synchronization system, and there are a lot of edge cases you have to worry about, including updates that just keep going (data item X is updated on computer A, synched over to computer B, then is picked up as a change and sent back to A, who then sends it back to B, and so on). If you really have to create your own system, and I strongly advise against it, then I'd recomend taking a look at SyncML. I think volume 4 of Pattern Languages of Program Design has a chapter on a sync pattern.


The change in reality is the timing window associated with updating the data. If the data can be updated by multiple users today; concurrency, locking at the DB level, etc... should already be in place. After that, the time at which a user holds on to the data before updating the database is somewhat irrelevant.

Unless you are going to offer a merging option, the sync should be trivial. DB wins or user wins, allow that choice to be made by the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.