Ejemplo n.º 1
0
        /// <summary>
        /// Saves all unsaved objects asynchronously, then marks them as saved.
        /// </summary>
        /// <param name="connection">
        /// </param>
        /// <returns>
        /// </returns>
        public static async Task SaveAllAsync(Connection connection)
        {
            IEnumerable <SaveableObject>  unsavedFileObjects = SaveableObjects.Items.Where(x => !x.IsSaved && !(x is IDatabaseObject));
            IEnumerable <IDatabaseObject> unsavedDbObjects   = SaveableObjects.Items.Where(x => !x.IsSaved && x is IDatabaseObject).Cast <IDatabaseObject>();

            foreach (SaveableObject unsavedFileObject in unsavedFileObjects)
            {
                await unsavedFileObject.SaveAsync(connection);
            }
            if (unsavedDbObjects.Count() > 0)
            {
                using (DatabaseTransaction <QuartzContext> transaction = new DatabaseTransaction <QuartzContext>(connection))
                {
                    foreach (IDatabaseObject unsavedDbObject in unsavedDbObjects)
                    {
                        await transaction.AddOrUpdateAsync(unsavedDbObject);
                    }
                    await transaction.SaveChangesAsync();
                }
                foreach (SaveableObject unsavedDbObject in SaveableObjects.Items.Where(x => !x.IsSaved && x is IDatabaseObject))
                {
                    unsavedDbObject.IsSaved = true;
                }
            }
        }