Two list synchronization

ε祈祈猫儿з 提交于 2019-12-06 16:08:21

The problem is not to implement a delete synchronization, but to recognize, whether the event has been deleted in one list or added to the other list.

I recommend not to delete events from the DB.Events list, but to mark them as deleted.

Therefore, you need a flag

bool IsDeleted = false;

in your type myEvent.

In Google list (I assume you use https://developers.google.com/google-apps/calendar/v3/reference/events) you don't have such a flag. So your algorithm needs to be a bit tricky.

So you need the first synchronize from the Google list

foreach (Event item in myGoogleCalendar.Items)
{
    if (DB.Events.Where(o => o.GoogleID == item.Id).Count() == 0)
    {
        // create new DB event from Google event, because since it is 
        // not in the DB.Events list, it has not been deleted there earlier, 
        // else there would be an event with IsDeleted=true
    }
    else
    {
        //This event exists in both
        myEvent dbEvent = DB.Events.Where(o => o.GoogleID == item.Id).First();
        if (dbEvent.IsDeleted)
        {
           // delete Google event, since the flag in DB.Events list shows that 
           // it existed earlier and has been deleted in DB.Events list.
        }
        else
        {
            if(item.Updated.Value == dbEvent.UpdateTime) continue;//Same

            if (item.Updated.Value > dbEvent.UpdateTime)
            {
                // update DB event
            }
            else
            {
                // update Google event
            }
        }
    }
}

Then your synchronize from your DB.Events list

foreach (myEvent item in DB.Events)
{
    if (!item.IsDeleted && string.IsNullOrEmpty(item.GoogleID))
    {
        // Create new Google Event from DB event
    }
    else if (!item.IsDeleted && !string.IsNullOrEmpty(item.GoogleID))
    {
        // Mark DB event as deleted
        item.IsDeleted = true;
    }
}

This works in the scenarios

  • You create an event in DB
  • You create an event in Google
  • You delete an event in DB
  • You delete an event in Google

I created a Gist: https://gist.github.com/ricjac/947991ba96488c640bfe

        // Prepare DTO to delta
        var mergeDict = DeltaExtensions.MergeDictionary(
            sourceIEnum,        //Source
            destinationIEnum,   //Destination
            s => s.SomeIdKey,
            d => d.SomeIdKey);

        // Get the delta between the two
        var mergeDelta = DeltaExtensions.GetMergeDelta(mergeDict);

        // You now have the delta of the two:
        mergeDelta.InsertedOnly
        mergeDelta.DeletedOnly
        mergeDelta.Inserted
        mergeDelta.Updated
        mergeDelta.Deleted
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!