public async Task SaveAsync()
{
if (this.budgetAnalyserDatabase == null)
{
throw new InvalidOperationException("Application Database cannot be null here. Code Bug.");
}
if (!HasUnsavedChanges)
{
return;
}
var messages = new StringBuilder();
if (!ValidateAll(messages))
{
throw new ValidationWarningException(messages.ToString());
}
this.databaseDependents
.Where(service => this.dirtyData[service.DataType])
.ToList()
.ForEach(service => service.SavePreview());
// This clears all the temporary tasks from the collection. Only tasks that have CanDelete=false will be kept and saved.
this.budgetAnalyserDatabase.LedgerReconciliationToDoCollection.Clear();
// Save the main application repository first.
await this.applicationRepository.SaveAsync(this.budgetAnalyserDatabase);
// Save all remaining service's data in parallel.
await this.databaseDependents
.Where(service => this.dirtyData[service.DataType])
.Select(async service => await Task.Run(async () => await service.SaveAsync(this.budgetAnalyserDatabase)))
.ContinueWhenAllTasksComplete();
ClearDirtyDataFlags();
this.monitorableDependencies.NotifyOfDependencyChange<IApplicationDatabaseService>(this);
}