Exemple #1
0
        public async Task SynchronizeAsync()
        {
            _logger?.Info("Cleaning the database...");
            await CleanDatabase();

            _logger?.Info("Database is cleaned.");

            _logger?.Info("Creating synchronization services...");
            var client       = new PipeDriveClient(_pipedriveApiKey);
            var syncServices = CreateSyncServices();

            _logger?.Info("Synchronization services are created.");

            _logger?.Info("Loading dynamic data types...");
            var dynamicConfigurations = new List <DynamicEntityConfiguration>();

            foreach (var syncService in syncServices.OfType <IDynamicSyncService>())
            {
                dynamicConfigurations.Add(await syncService.ConstructType(client));
            }
            _logger?.Info("Dynamic data types are loaded.");

            _logger?.Info("Loading data from API...");
            foreach (var syncService in syncServices)
            {
                await syncService.Fetch(client);
            }
            _logger?.Info("API data is loaded.");

            _logger?.Info("Synchronizing data with the database...");
            using (var context = new PipeDriveDbContext(_sqlConnectionString, dynamicConfigurations))
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                foreach (var syncService in syncServices)
                {
                    syncService.Sync(context);
                }
                context.Configuration.AutoDetectChangesEnabled = true;

                _logger?.Info("Saving to the database...");
                await context.SaveChangesAsync();
            }
            _logger?.Info("Data is synchronized.");
            _logger?.Info("Success.");
        }