public static Sport GetOrCreate(string name) { try { validateName(name); Sport sport; using (var db = new MyDbContext()) { sport = db.Sport.Where(p => p.Name == name).FirstOrDefault(); if (sport == null) { sport = new Sport(); sport.Name = name; db.Sport.Add(sport); db.SaveChanges(); } return sport; } } catch (Exception e) { throw new DomainException("Could not find or create sport", e); } }
private static void validateSport(Sport sport) { if (sport == null) throw new DomainException("Invalid sport"); validateName(sport.Name); }