다음을 통해 공유


방법: 열거된 변경 단위 필터링

이 항목에서는 사용자 지정 데이터 저장소의 데이터를 동기화하는 Sync Framework 동기화 공급자에 의해 열거된 변경 단위를 관리되는 언어를 사용하여 필터링하는 방법을 설명합니다.

이 항목에서는 기본적인 C# 및 Microsoft .NET Framework 개념에 익숙하다고 가정합니다.

이 항목의 예제에서는 다음과 같은 Sync Framework 클래스 및 멤버를 중점적으로 설명합니다.

변경 단위 필터링 이해

변경 단위 필터는 동기화 데이터를 변경 단위의 하위 집합으로 제한합니다. 예를 들어 나머지 변경 단위는 무시하고 연락처의 이름 및 전화 번호 필드만 동기화할 수 있습니다. 변경 단위 필터는 범위 안의 항목에 대해 정의된 변경 단위의 하위 집합만 복제본에 저장된 경우 유용합니다.

Sync Framework에서는 변경 내용 열거에 포함할 변경 단위 목록을 정의하는 ChangeUnitListFilterInfo 개체를 제공합니다. 이 필터는 응용 프로그램과 공급자 간에 적절한 모든 메커니즘을 사용하여 동기화 응용 프로그램에서 설정하거나 두 공급자 간에 협상될 수 있습니다. 필터 협상에 대한 자세한 내용은 동기화 데이터 필터링을 참조하십시오.

변경 단위 필터가 정의된 경우 Sync Framework에서는 원본 공급자의 LoadChangeData를 호출할 때 필터에 포함된 변경 단위에 대한 데이터만 요청합니다.

SaveChangeWithChangeUnits로 전송된 데이터에 필터에 있는 변경 단위만 포함된다는 것을 제외하면, 대상 공급자는 표준 변경 내용 일괄 처리와 동일한 방식으로 변경 내용을 받아 적용합니다.

빌드 요구 사항

예제

이 항목의 예제 코드에서는 Metadata Storage Service를 사용하여 변경 내용 일괄 처리에 포함된 변경 단위를 필터링하여 변경 내용 일괄 처리를 생성하는 방법을 보여 줍니다. 이 예제의 복제본은 연락처 정보를 쉼표로 구분된 값의 목록으로 저장하는 텍스트 파일입니다. 동기화할 항목은 이 파일에 포함된 연락처입니다. 필터는 연락처의 이름과 전화 번호 필드만 포함하도록 정의됩니다.

필터 설정

원본 공급자는 응용 프로그램에서 변경 내용을 열거할 때 어떤 연락처 필드를 포함할지 정의하는 변경 단위 필터를 설정할 수 있도록 공용 메서드를 구현합니다.

public void SetContactFieldsToInclude(Contact.ChangeUnitFields[] includedFields)
{
    // Translate the array of fields to a list of IDs.
    _includedChangeUnits = new List<SyncId>(includedFields.Length);
    for (int iField = 0; iField < includedFields.Length; iField++)
    {
        _includedChangeUnits.Add(new SyncId((byte)includedFields[iField]));
    }

    _isFiltered = true;
}

동기화 응용 프로그램은 원본 공급자를 로컬 공급자로 만들고 SetContactFieldsToInclude 메서드를 사용하여 변경 내용을 열거할 때 이름 및 전화 번호 필드만 포함되도록 지정합니다. 또한, 이 응용 프로그램은 대상 공급자를 만들고 동기화를 수행합니다.

private void SynchronizeWithChangeUnitFiltering(ContactStore localStore, ContactStore remoteStore, SyncDirectionOrder syncDir)
{
    // Create the local provider and set the change unit filter.
    // The filter is ignored when the provider is the destination provider.
    SyncProvider localProvider = new ContactsProviderChangeUnitFiltering(localStore);
    // Only include name and phone number fields.
    Contact.ChangeUnitFields[] includedFields = new Contact.ChangeUnitFields[2];
    includedFields[0] = Contact.ChangeUnitFields.NameCU;
    includedFields[1] = Contact.ChangeUnitFields.PhoneCU;
    ((ContactsProviderChangeUnitFiltering)localProvider).SetContactFieldsToInclude(includedFields);
    
    // Create the remote provider and do not set a filter.
    SyncProvider remoteProvider = new ContactsProviderChangeUnitFiltering(remoteStore);

    // Create the synchronization orchestrator and set the providers and synchronization direction.
    SyncOrchestrator orchestrator = new SyncOrchestrator();
    orchestrator.LocalProvider = localProvider;
    orchestrator.RemoteProvider = remoteProvider;
    orchestrator.Direction = syncDir;

    string msg;
    try
    {
        // Synchronize data between the two providers.
        SyncOperationStatistics stats = orchestrator.Synchronize();

        // Display statistics for the synchronization operation.
        msg = "Synchronization succeeded!\n\n" +
            stats.DownloadChangesApplied + " download changes applied\n" +
            stats.DownloadChangesFailed + " download changes failed\n" +
            stats.UploadChangesApplied + " upload changes applied\n" +
            stats.UploadChangesFailed + " upload changes failed";
    }
    catch (Exception ex)
    {
        msg = "Synchronization failed! Here's why: \n\n" + ex.Message;
    }
    MessageBox.Show(msg, "Synchronization Results");
}

필터링된 변경 내용 일괄 처리 열거

원본 공급자는 GetChangeBatch를 구현하므로, 필터가 지정된 경우 Metadata Storage Service를 사용하여 필터링된 변경 내용 일괄 처리를 생성합니다. 이 작업은 ChangeUnitListFilterInfo 개체를 만든 다음 포함할 변경 단위의 목록을 지정하여 개체를 초기화하는 방식으로 수행합니다. 필터 정보는 ReplicaMetadata 개체의 GetFilteredChangeBatch 메서드로 전달됩니다. 필터에 포함된 내용을 확인하기 위해 Metadata Storage Service가 공급자를 콜백할 필요가 없으므로 filterCallback 매개 변수에는 null이 지정됩니다.

public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge, out object changeDataRetriever)
{
    // Return this object as the IChangeDataRetriever object that is called to retrieve item data.
    changeDataRetriever = this;

    ChangeBatch retrievedBatch;
    if (_isFiltered)
    {
        // Use the metadata storage service to get a filtered batch of changes.
        ChangeUnitListFilterInfo filterInfo = new ChangeUnitListFilterInfo(IdFormats, _includedChangeUnits, true);
        retrievedBatch = _ContactStore.ContactReplicaMetadata.GetFilteredChangeBatch(batchSize, destinationKnowledge,
            filterInfo, null);
    }
    else
    {
        // Use the metadata storage service to get a batch of changes.
        retrievedBatch = _ContactStore.ContactReplicaMetadata.GetChangeBatch(batchSize, destinationKnowledge);
    }
    
    return retrievedBatch;
}

다음 단계

다음으로, 공급자가 대상 공급자와 통신하여 변경 내용 열거에 사용할 필터를 설정할 수 있도록 필터 협상 기능을 추가할 수 있습니다. 필터를 협상하는 방법에 대한 자세한 내용은 방법: 필터 협상을 참조하십시오.

참고 항목

개념

일반적인 표준 사용자 지정 공급자 태스크 프로그래밍
동기화 데이터 필터링