Monthly Archives: May 2008

Exploring the Microsoft Sync Framework

Earlier this week i’ve been experimenting with the Microsoft Sync Framework. In a typical n-tier architecture the client can’t access the remote database directly but uses a proxy instead. The available ServerSyncProviderProxy has only one constructor which accepts an object. With reflector i found out that the proxy simply uses reflection to call some methods. Here are a couple of helper classes that help you prevent runtime errors due to this approach:

[ServiceContract(Namespace = "http://www.timvw.be/Synchronization")]
public interface IServerSyncProvider
{
 [OperationContract]
 SyncServerInfo GetServerInfo(SyncSession syncSession);

 [OperationContract]
 SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession);

 [OperationContract]
 SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession);

 [OperationContract]
 SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession);
}

public class MyServerSyncProviderProxy : ServerSyncProviderProxy
{
 public MyServerSyncProviderProxy(IServerSyncProvider serverSyncProvider)
  : base(serverSyncProvider)
 {
 }
}

And now you can easily consume a ServerSyncProviderProxy as following:

SyncAgent syncAgent = new SyncAgent();

EndpointAddress address = new EndpointAddress("http://somewhere/Be.Timvw.Demo.Host/ServerSyncProvider.svc");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<iserverSyncProvider> factory = new ChannelFactory<iserverSyncProvider>(binding, address);
IServerSyncProvider serverSyncProvider = factory.CreateChannel();
syncAgent.RemoteProvider = new SafeServerSyncProviderProxy(serverSyncProvider);

Presenting a generic EffectivityManager

I’ve already presented a Generic Effectivity. Offcourse, managing all these effectivities (versions of data) can be handled in a generic way too. A bit of experience mixed with Patterns for things that change with time allowed me to come up with the following interface:

public interface IEffectivityManager<t> : IList<ieffectivity<t>>
{
 IEffectivity<t> Add(T t, DateTime begin);
 IEffectivity<t> GetSnapshot(DateTime validityDate);
 bool TryGetSnapshot(DateTime validityDate, out IEffectivity<t> effectivity);
}

Feel free to download IEffectivityManager.txt, EffectivityManager.txt and EffectivityManagerTester.txt.

Presenting a generic Effectivity

Very often we have to manage objects and their changes over time. Usually we implement this by adding a Range<DateTime> to the data. Martin Fowler has a name for this pattern: Effectivity and i have an implementation for the following interface:

public interface IEffectivity<t> : IComparable<ieffectivity<t>>
{
 T Element { get; }
 IRange<dateTime> ValidityPeriod { get; }
 bool IsEffectiveOn(DateTime validityDate);
}

Feel free to download IEffectivity.txt, Effectivity.txt and EffectivityTester.txt.

Presenting a generic Range

Quite often i’m writing code that compares one value against a range of other values. Most implementations compare the value against the boundaries (smallest and largest in the collection of other values). Having written this sort of code way too much i’ve decided to generalize the problem and distill an interface:

public interface IRange<t>
{
 T Begin { get; set; }
 T End { get; set; }
 bool Includes(T t);
 bool Includes(IRange<t> range);
 bool Overlaps(IRange<t> range);
}

Offcourse, i’ve also written implementation ;) Feel free to download IRange.txt, Range.txt and RangeTester.txt