In my previous post I gave a small heads up regarding Microsoft’s Sync Framework and one of the issues which I faced while using SQLCE as a client. To get the latest version of Sync Framework which at this point of time is the v2.0 CTP1 visit this link.
If you’ve read up on the Sync Framework then you should know that it can target a variety of stores. One of the server side stores that can be used is SQL Server 2008. SQL Server 2008 provides change tracking features that enable you to easily sync your data without losing changes.
However to use the powerful (read: troublesome) change tracking features you’d require a bit of ADO.NET code to perform some of the following tasks:
This bunch of ADO.NET code can be put into stored procedures and executed on the ‘08 Server. There is one small problem though: Points 3,4 and 5 above have to be repeated for each of the tables participating in synchronization. Well, not quite every point listed above for every table but almost... Let me explain below:
Upload Only Scenario
For a table on the server to work successfully with the upload only scenario requires that it has to have the following stored procedures:
Download Only Scenario
In the download scenario a table has to be able to send back incremental inserts, updates and deletes that happened on the server back to the client. This means:
Bidirectional Scenario
In the bidirectional scenario a table has to fulfill both scenarios above as well as be able to detect conflicts when rows are simultaneously updated or deleted on server and client.
In the case of your application having a handful of tables (hardly likely), you can manually go ahead and write these stored procedures. However you’d soon realize the repetitiveness in such a mundane task. Enter T4.
T4 is an acronym standing for text templating transformation toolkit. It’s basically a code generator hidden inside Visual Studio that you can use to wreak havoc :). The basic concept is that you create a file like [AnyName].tt (notice the extension tt) and then run the transformation and it turns into [AnyName].cs or whatever extension you want it to have. You can interweave code and logic inside the .tt file and the result will be code generated in your output file. It’s a slightly tricky concept to get your head around at the first go, but persevere and you’ll find you can do amazing things with T4.
For a (yawn) intro to T4 visit the msdn site but to actually get productive with T4 and to see some sample code in action visit this post on Oleg Sych’s blog. Also go through the various articles on his blog, he’s done some pretty cool stuff with T4.
Although Visual Studio provides support for T4 they don’t make it really obvious, there’s no file-> new item template or Intellisense that will help you code on T4. You can download either Clarius T4 editor (look for the free edition) or Tangible Modelling Tools plus T4 editor.
Clarius does not have intellisense when you write code inside code blocks, this is present inside the Tangible Modelling tools download. If you’re ok with getting a UML modelling tool along with the T4 editor go for Tangible. (Note: Only some of the .NET class libraries are supported in the free edition of Tangible).
Coming back to this post, I’ve written a T4 template that contains the templates for all the stored procedures mentioned above. To use it perform the following steps:
The script that I’ve provided here basically creates the structure of the stored procs for a sample database and sample tables which have to participate in synchroniztion. To actually work with your own database you’ll have to modify some code in the .tt file to generate the stored procedures for all your tables. If you’re upto it go ahead, if not, mail me sometime and I can provide a utility that you point to a database and it will generate all the stored procs for you.
I love T4!!!