示例#1
0
        public EntitySync ForEntity <T>(SynchronizationParameters synchronizationParameters) where T : AbstractEntity, new()
        {
            Type       type       = typeof(T);
            string     typeName   = type.Name;
            EntitySync entitySync = new EntitySync
            {
                Type   = type,
                Getter = sql => DatabaseConnection.Connection.Query <T>(sql).Cast <AbstractEntity>().ToList(),
            };

            if (typeName != "User" && synchronizationParameters.DontUpload.Contains(typeName))
            {
                entitySync.Up = () => entitySync.PostUp();
            }
            else
            {
                entitySync.Up = () => UploadAsync <T>(synchronizationParameters, entitySync.PostUp);
            }

            if (synchronizationParameters.DontDownload.Contains(typeName))
            {
                entitySync.Down = () => entitySync.PostDown();
            }
            else
            {
                entitySync.Down = () => DownloadAsync <T>(synchronizationParameters, entitySync.PostDown);
            }

            entitySync.CreateTable = connection => connection.CreateTable <T>();
            entitySync.DropTable   = connection => connection.DropTable <T>();

            return(entitySync);
        }
示例#2
0
        public void Synchronize(SynchronizationParameters synchronizationParameters)
        {
            NotificationService.Send(NotificationEvent.PreSynchronization);

            List <EntitySync> syncTables = synchronizationParameters.EntitiesInSynchronization = Setup(synchronizationParameters);

            Debug.Assert(syncTables.Count >= 2, "syncEntities.Count >= 2");
            Debug.Assert(syncTables[0].Type == typeof(DeletedRecord), "syncEntities[0].Type == typeof(DeletedRecord)");
            Debug.Assert(syncTables[1].Type == typeof(User), "syncEntities[1].Type == typeof(User)");

            //synchronizationParameters.Notif((syncTables.Count >= 2) + "syncEntities.Count >= 2");
            //synchronizationParameters.Notif((syncTables[0].Type == typeof(DeletedRecord)) + "syncEntities[0].Type == typeof(DeletedRecord)");
            //synchronizationParameters.Notif((syncTables[1].Type == typeof(User)) +  "syncEntities[1].Type == typeof(User)");

            for (int i = 0; i < syncTables.Count; i++)
            {
                EntitySync sync = syncTables[i];

                // Handle each PostXxx Action different for last SyncEntity
                if (sync == syncTables.Last())
                {
                    // After last upload start with first SyncEntity download
                    sync.PostUp = syncTables[0].Down;
                    // After last download invoke finalAction
                    sync.PostDown = () =>
                    {
                        NotificationService.Send(NotificationEvent.Synchronized);
                        synchronizationParameters.FinalAction();
                    };
                }
                else
                {
                    // after up-/download start up-/download of next SyncEntity
                    sync.PostUp   = syncTables[i + 1].Up;
                    sync.PostDown = syncTables[i + 1].Down;
                }
            }

            synchronizationParameters.EntitiesInSynchronization[1].PostUp = synchronizationParameters.EntitiesInSynchronization[0].Down;

            synchronizationParameters.Downloaded                             =
                synchronizationParameters.Uploaded                           =
                    synchronizationParameters.RecordsToDelete                =
                        synchronizationParameters.RecordsDeleted             =
                            synchronizationParameters.RecordsDeletedAtServer = 0;

            synchronizationParameters.EntitiesInSynchronization[0].Up();
        }