Exemplo n.º 1
0
        public override bool Save(SyncConfiguration entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var db = this.GetDb(true))
            {
                var result = true;
                var pm     = AutoMapper.Mapper.Map <SyncConfigurationPM>(entity);

                // Insert the main sync config object
                result &= base.SavePM(pm);
                if (!result)
                {
                    return(false); // Should be rolled back by the open transaction
                }

                // Insert the backup source
                if (pm.Source != null)
                {
                    var backupSourceRepo = new BackupSourceRepository(db);
                    result &= backupSourceRepo.Save(pm.Source);
                    if (!result)
                    {
                        return(false); // Should be rolled back by the open transaction
                    }
                }

                // Insert each destination record
                var backupDestinationRepo = new BackupDestinationRepository(db);
                foreach (var destination in pm.Destinations)
                {
                    result &= backupDestinationRepo.Save(destination);
                }

                // Commit or rollback open transaction
                if (result)
                {
                    db.Commit();
                }
                // Else it will rollback automatically

                // Return the hydrated domain entity
                AutoMapper.Mapper.Map(pm, entity);

                return(result);
            }
        }
Exemplo n.º 2
0
        public override bool Delete(Guid id)
        {
            var syncConfig = this.Get(id);

            if (syncConfig == null)
            {
                return(false);
            }

            // Start a new transaction
            using (var db = this.GetDb(true))
            {
                var success = true;

                // Cleanup source
                if (syncConfig.Source?.Id.HasValue ?? false)
                {
                    var backupSourceRepo = new BackupSourceRepository(db);
                    success &= backupSourceRepo.Delete(syncConfig.Source.Id.Value);
                }

                // Cleanup destinations
                var backupDestionationRepo = new BackupDestinationRepository(db);
                foreach (var dest in syncConfig.Destinations)
                {
                    if (dest?.Id.HasValue ?? false)
                    {
                        success &= backupDestionationRepo.Delete(dest.Id.Value);
                    }
                }

                // Delete the aggregate root record and commit all the changes if successful
                if (success && base.Delete(id))
                {
                    db.Commit();
                    return(true);
                }

                return(false);
            }
        }