共用方式為


管理簡單提供者的中繼資料

簡單提供者會要求您將中繼資料儲存在中繼資料儲存服務中,該服務是在 SqlMetadataStore (適用於 Managed 程式碼) 和 ISqlSyncMetadataStore (適用於 Unmanaged 程式碼) 中實作。

中繼資料存放區包含以下類型的中繼資料,提供者或應用程式將會與這些中繼資料互動:

  • 複寫識別碼。可識別使用特定存放區的複寫。

  • 項目識別碼和版本。可識別已經從項目存放區列舉的每一個項目以及該項目的目前版本。

  • 變更單位識別碼和版本。可識別需要追蹤之項目的部分以及每一個部分的目前版本。例如,連絡人資料庫中的連絡人可以是項目,而且電話號碼欄位可以是其中一個變更單位。

識別項目存放區和中繼資料存放區中的項目

若要同步處理某個項目,Sync Framework 必須能夠在項目存放區中識別此項目,並將該識別對應到中繼資料存放區中的內部識別碼。它也必須能夠判斷,項目版本自從上次同步處理工作階段之後是否已變更。如果此版本已變更而且目的地複寫尚未包含該項目版本,應該要同步處理此項目。如果變更是在變更單位層級而非項目層級同步處理,Sync Framework 就必須能夠識別此變更單位和它的版本。變更單位代表子項目變更,例如代表連絡人之項目中的電話號碼欄位。這個範例不會使用變更單位。

指定中繼資料存放區識別碼的格式

下列程式碼會定義 MyFullEnumerationSimpleSyncProvider 的建構函式和 IdFormats 屬性。如此可讓 Sync Framework 執行階段判斷中繼資料存放區用於識別碼的格式。如果未使用彈性識別碼,Sync Framework 會使用固定格式來識別複寫、項目和變更單位。如果使用了彈性識別碼,則會使用 ISimpleSyncProviderIdGenerator 方法來產生識別碼。

public MyFullEnumerationSimpleSyncProvider(string name, MySimpleDataStore store)
{
    _name = name;
    _store = store;

    // Create a file to store metadata for all items and a file to store 
    // the replica ID.
    _replicaMetadataFile = Environment.CurrentDirectory + "\\" + _name.ToString() + ".Metadata";
    _replicaIdFile = Environment.CurrentDirectory + "\\" + _name.ToString() + ".Replicaid";

    // Set ReplicaIdFormat to use a GUID as an ID, and ItemIdFormat to use a GUID plus
    // an 8-byte prefix.
    _idFormats = new SyncIdFormatGroup();
    _idFormats.ItemIdFormat.IsVariableLength = false;
    _idFormats.ItemIdFormat.Length = 24;
    _idFormats.ReplicaIdFormat.IsVariableLength = false;
    _idFormats.ReplicaIdFormat.Length = 16;

    this.ItemConstraint += new EventHandler<SimpleSyncItemConstraintEventArgs>(OnItemConstraint);
    this.ItemConflicting += new EventHandler<SimpleSyncItemConflictingEventArgs>(OnItemConflicting);
}
public SyncId ReplicaId
{
    get 
    {
        if (_replicaId == null)
        {
            _replicaId = GetReplicaIdFromFile( _replicaIdFile);
        }

        return _replicaId; 
    }
}

public override SyncIdFormatGroup IdFormats
{
    get { return _idFormats; }
}
Public Sub New(ByVal name As String, ByVal store As MySimpleDataStore)
    _name = name
    _store = store

    ' Create a file to store metadata for all items and a file to store 
    ' the replica ID. 
    _replicaMetadataFile = (Environment.CurrentDirectory & "\") + _name.ToString() & ".Metadata"
    _replicaIdFile = (Environment.CurrentDirectory & "\") + _name.ToString() & ".Replicaid"

    ' Set ReplicaIdFormat to use a GUID as an ID, and ItemIdFormat to use a GUID plus 
    ' an 8-byte prefix. 
    _idFormats = New SyncIdFormatGroup()
    _idFormats.ItemIdFormat.IsVariableLength = False
    _idFormats.ItemIdFormat.Length = 24
    _idFormats.ReplicaIdFormat.IsVariableLength = False
    _idFormats.ReplicaIdFormat.Length = 16

    AddHandler Me.ItemConstraint, AddressOf HandleItemConstraint
    AddHandler Me.ItemConflicting, AddressOf HandleItemConflicting
End Sub
Public ReadOnly Property ReplicaId() As SyncId
    Get
        If _replicaId Is Nothing Then
            _replicaId = GetReplicaIdFromFile(_replicaIdFile)
        End If

        Return _replicaId
    End Get
End Property

Public Overrides ReadOnly Property IdFormats() As SyncIdFormatGroup
    Get
        Return _idFormats
    End Get
End Property

指定項目欄位和中繼資料結構描述

Sync Framework 會將項目存放區資料或是您建立的其他中繼資料對應到內部中繼資料存放區識別碼和版本,其方式是使用 MetadataSchema 屬性所公開的 ItemMetadataSchema 物件。下列程式碼範例提供 ItemMetadataSchema 物件的輸入。範例程式碼中的常數會定義項目存放區中每一個資料行的整數值。這些值是在建立自訂欄位定義和 ItemMetadataSchema 物件的識別規則時使用。

public const uint CUSTOM_FIELD_ID = 1;
public const uint CUSTOM_FIELD_TIMESTAMP = 2;
public override ItemMetadataSchema MetadataSchema
{
    get
    {
        CustomFieldDefinition[] customFields = new CustomFieldDefinition[2];
        customFields[0] = new CustomFieldDefinition(CUSTOM_FIELD_ID, typeof(ulong));
        customFields[1] = new CustomFieldDefinition(CUSTOM_FIELD_TIMESTAMP, typeof(ulong));

        IdentityRule[] identityRule = new IdentityRule[1];
        identityRule[0] = new IdentityRule(new uint[] { CUSTOM_FIELD_ID });

        return new ItemMetadataSchema(customFields, identityRule);
    }
}
Public Const CUSTOM_FIELD_ID As UInteger = 1
Public Const CUSTOM_FIELD_TIMESTAMP As UInteger = 2
Public Overrides ReadOnly Property MetadataSchema() As ItemMetadataSchema
    Get
        Dim customFields As CustomFieldDefinition() = New CustomFieldDefinition(1) {}
        customFields(0) = New CustomFieldDefinition(CUSTOM_FIELD_ID, GetType(ULong))
        customFields(1) = New CustomFieldDefinition(CUSTOM_FIELD_TIMESTAMP, GetType(ULong))

        Dim identityRule As IdentityRule() = New IdentityRule(0) {}
        identityRule(0) = New IdentityRule(New UInteger() {CUSTOM_FIELD_ID})

        Return New ItemMetadataSchema(customFields, identityRule)
    End Get
End Property

ItemMetadataSchema 物件會公開三項屬性:

  • CustomFields

    自訂欄位是在中繼資料存放區中由整數所識別的欄位。如果應用程式需要一個或多個欄位的易記名稱,它應該將此整數對應到名稱。針對兩個理由所定義的自訂欄位:為了識別項目,並提供有關這些項目的版本資訊。版本欄位可讓 Sync Framework 判斷某個項目或變更單位是否已變更。在此範例中,版本欄位包含項目存放區中的實際資料,所以此項目存放區中的每一個欄位都會有一個欄位。這個一對一的對應並不是必要的,也不會有效率。更實際的解決方案是取得項目欄位的雜湊,並將其儲存在單一自訂欄位中。

  • IdentityRules

    識別規則會指定應該使用哪一個或哪些自訂欄位來識別某個項目。在此情況下,將會使用 CUSTOM_FIELD_ID 欄位 (欄位 0)。

  • ChangeUnitVersionDefinitions (不用於這個範例)

    如果使用了變更單位,您必須針對變更單位定義版本欄位。變更單位與版本資訊之間不一定要有一對一的對應,也不一定要儲存實際資料。變更單位也可以跨越多個欄位。例如,這個應用程式可以指定 Zip Phone 為變更單位,而且 Guid 是另一個變更單位。對於 Guid 而言,您可使用實際資料;對於其他變更單位而言,您可使用 Zip Phone 欄位的雜湊或某個其他機制來判斷版本。

搭配項目存放區資料使用的某些方法 (例如 InsertItem) 需要代表每個欄位的 ItemField 物件集合。這些方法之參數的 ItemFieldDictionary 物件與 CustomFieldDefinition 物件內指定的參數具有相同索引值。

請參閱

概念

實作簡單的自訂提供者
HOW TO:建立 Managed 簡單提供者