Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
"logging all offline changes" - I'm not familiar with methods/technologies that are used for this purpose. Could you elaborate more on this point? –  SpectralGhost Dec 10 '11 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 '11 at 2:36
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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