public MappingModel AddMapping(MappingModel model) { if (model == null) throw new ArgumentNullException("model"); Mapping mapToReturn; using (var unitOfWork = new UnitOfWork(_connectionString)) { bool userAuthorised = UserAuthorizedToAccessSuite(unitOfWork, model.UserId, model.SuiteId, new[] {RoleType.Admin}); if (!userAuthorised) throw new UnauthorizedUserException( "User does not have access or sufficient privileges for this action to suite: " + model.SuiteId); if ( !unitOfWork.Context.Parameters.Any( x => x.ParameterId == model.ParameterId && x.SuiteId == model.SuiteId)) throw new InvalidOperationException( "Mismatch between parameter id and suite id. Can not add mapping!"); if (!unitOfWork.Context.Servers.Any(x => x.ServerId == model.ServerId && x.SuiteId == model.SuiteId)) throw new InvalidOperationException("Mismatch between server id and suite id. Can not add mapping!"); if (!unitOfWork.Context.Regions.Any(x => x.RegionId == model.RegionId && x.SuiteId == model.SuiteId)) throw new InvalidOperationException("Mismatch between region id and suite id. Can not add mapping!"); if ( !unitOfWork.Context.Environments.Any( x => x.EnvironmentId == model.EnvironmentId && x.SuiteId == model.SuiteId)) throw new InvalidOperationException( "Mismatch between environment id and suite id. Can not add mapping!"); if ( !unitOfWork.Context.Applications.Any( x => x.ApplicationId == model.ApplicationId && x.SuiteId == model.SuiteId)) throw new InvalidOperationException( "Mismatch between application id and suite id. Can not add mapping!"); mapToReturn = model.ToNewDbObject(); unitOfWork.Add(mapToReturn); } if (mapToReturn != null) return mapToReturn.ToModel(); return null; }