Beispiel #1
0
        private void Synchronize <T>(List <T> modelsFromAPI, List <T> modelsFromDatabase, T model) where T : class, BaseModel, new()
        {
            BaseService <T> service = new BaseService <T>();

            // Todo: make batch functions for a better performance
            #region Local to Global

            // New items added
            List <T> addedItemsLocally = modelsFromDatabase.Where(m => m.LastSynchronized == null).ToList();
            foreach (T item in addedItemsLocally)
            {
                T modelToAdd = item;
                this.UpdateModel <T>(ref modelToAdd);
            }

            // Items updated
            List <T> updatedItemsLocally = modelsFromDatabase.Where(m => m.ModifiedAt > m.LastSynchronized && m.Active).ToList();
            foreach (T item in updatedItemsLocally)
            {
                T modelToUpdate = item;
                this.UpdateModel <T>(ref modelToUpdate);
            }

            // Items deleted
            List <T> deletedItemsLocally = modelsFromDatabase.Where(m => m.ModifiedAt > m.LastSynchronized && !m.Active).ToList();
            foreach (T item in deletedItemsLocally)
            {
                this.Delete <T>(item.Id);
            }

            #endregion

            #region Global to Local

            // New items added
            List <Guid> addedIdsGlobally   = modelsFromAPI.Select(m => m.Id).Except(modelsFromDatabase.Select(m => m.Id)).ToList();
            List <T>    addedItemsGlocally = modelsFromAPI.Where(m => addedIdsGlobally.Contains(m.Id)).ToList();

            foreach (T item in addedItemsGlocally)
            {
                T modelToUpdate = item;
                service.AddOrUpdate(ref modelToUpdate);
            }

            // Items updated

            // Items deleted


            #endregion

            // BatchUpdate LastSynchronized
            if (modelsFromDatabase.Any())
            {
                service.BatchUpdateLastSynchronized <T>(modelsFromDatabase.Select(m => m.Id).ToList(), ref model);
            }
        }