Sync101 with Custom Filtering

Shows how to track custom filters and how to use a standard custom provider to send changes from a filter-tracking replica to two different filtered replicas.

C# (58.2 KB)
 
 
 
 
 
(0)
1,322 times
Add to favorites
5/13/2011
E-mail Twitter del.icio.us Digg Facebook
using System;
using Microsoft.Synchronization;
using System.Text;
using System.Collections.Generic;



namespace CustomFilterSyncSample
{
    public class SyncableItem<T>:ICloneable where T:ICloneable
    {
        public SyncableItem(T item)
            : this(new SyncId(Guid.NewGuid()), item)
        {
        }

        public SyncableItem(
            SyncId globalId, T item)
        {
            _globalId = globalId;
            _item = item;
            _changeUnitVersions = new SyncVersion[4];
            _filterChanges = new SortedList<uint, FilterChange>();
        }        

        public SyncId GlobalId
        {
            get
            {
                return _globalId;
            }
        }

        public T Item
        {
            get
            {
                return _item;
            }

            set
            {
                _item = value;
            }
        }

        public SyncVersion CreationVersion
        {
            get
            {
                return _creationVersion;
            }

            set
            {
                if (_creationVersion != null && !_creationVersion.Equals(value))
                {
                    throw new ArgumentException("creation version should match");
                }
                _creationVersion = value;
            }
        }

        public SortedList<uint, FilterChange> FilterChanges
        {
            get
            {
                return _filterChanges;
            }

            set
            {
                _filterChanges = value;
            }
        }
        
        public SyncVersion CurrentVersion
        {
            get
            {
                return _currentVersion;
            }

            set
            {
                _currentVersion = value;
            }
        }

        public ChangeKind ChangeKind
        {
            get
            {
                return _changeKind;
            }
            set
            {
                _changeKind = value;
            }
        }

        public bool IsDeleted
        {
            get
            {
                return _changeKind == ChangeKind.Deleted;
            }
        }

        public bool IsGhost
        {
            get
            {
                return _changeKind == ChangeKind.Ghost;
            }
        }

        public void DeleteItem(
            SyncVersion deletionVersion)
        {
            _item               = default(T);
            _changeUnitVersions = null;
            _currentVersion    = deletionVersion;
            _changeKind = ChangeKind.Deleted;
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();

            buffer.AppendFormat("SyncId: {0}, CreateVersion: {1}, CurrentVersion: {2}, ChangeKind: {3}", GlobalId.ToString().Substring(0,3), 
                                                                                                         CreationVersion, CurrentVersion, ChangeKind);
            if (!IsDeleted)
            {
                foreach (SyncVersion cuVersion in _changeUnitVersions)
                {
                    buffer.AppendFormat("\r\n\t\tCU Id: {0} CurVer: {0}", cuVersion);
                }
            }

            foreach (uint filterKey in FilterChanges.Keys)
            {
                buffer.AppendFormat("\r\nFltrKey: {0} IsMoveIn: {1} MoveVersion: {2} ", filterKey, FilterChanges[filterKey].IsMoveIn, FilterChanges[filterKey].MoveVersion);
            }

            buffer.Append("\r\n");

            return buffer.ToString();
        }

        SyncId          _globalId;
        SyncVersion     _creationVersion;
        SyncVersion     _currentVersion;
        SyncVersion[]   _changeUnitVersions; 
        SortedList<uint, FilterChange>  _filterChanges;
        ChangeKind      _changeKind = ChangeKind.Update;        
        T               _item;

        #region ICloneable Members

        internal SyncableItem(SyncId globalId,
            SyncVersion creationVersion,
            SyncVersion currentVersion,
            SortedList<uint, FilterChange> filterChanges,
            ChangeKind changeKind,
            object item)
        {
            _globalId = globalId;
            _creationVersion = creationVersion;
            _currentVersion = currentVersion;
            _filterChanges = filterChanges;
            _changeKind = changeKind;
            _item = (T)item;

        }
        public object Clone()
        {

            return new SyncableItem<T>(_globalId,
                                        new SyncVersion(_creationVersion.ReplicaKey, _creationVersion.TickCount),
                                        new SyncVersion(_currentVersion.ReplicaKey, _currentVersion.TickCount),
                                        _filterChanges,
                                        _changeKind,
                                        _item.Clone());
        }

        #endregion
    }
}