Repository to update flights

I have the Repository below, but I have noticed that there is a recurrent pattern try / catch which does not seem to be that clean, is there a better way to achieve or to extract that sort logic elsewhere?

public class FlightRepository : IFlightRepository
{

public FlightRepository(FlightManagerDbContext context)
{
_context = context;
}

public Result CreateFlight(Flight flight)
{
try
{
var tmp = _context.Flight.FirstOrDefault(f => f.Code == flight.Code);
if (tmp != null)
return Result.Conflict($"Flight {flight.Code} already exists"); _context.Flight.Add(flight); _context.SaveChanges(); return Result.Ok("Flight successfully saved"); } catch (Exception e) { return Result.Fail(new List<string>() {$"An error occured, {e.Message}"});
}
}

public Result UpdateFlight(Flight flight)
{
try
{
var tmp = _context.Flight.FirstOrDefault(f => f.Code == flight.Code);
if (tmp == null)
return Result.NotFound($"Flight { flight.Code} not found"); var toUpdate = _context.Flight.First(f => f.Code == flight.Code); toUpdate.Distance = flight.Distance; toUpdate.DepartureLatitude = flight.DepartureLatitude; toUpdate.DepartureLongitude = flight.DepartureLongitude; toUpdate.DepartureName = flight.DepartureName; toUpdate.DepartureTime = flight.DepartureTime; toUpdate.ArrivalLatitude = flight.ArrivalLatitude; toUpdate.ArrivalLongitude = flight.ArrivalLongitude; toUpdate.ArrivalName = flight.ArrivalName; toUpdate.ArrivalTime = flight.ArrivalTime; toUpdate.ConsumptionPerKm = flight.ConsumptionPerKm; toUpdate.TakeOffEffort = flight.TakeOffEffort; toUpdate.FlightTime = flight.FlightTime; toUpdate.FuelNeeded = flight.FuelNeeded; _context.SaveChanges(); return Result.Ok($"Flight {flight.Code} successfully updated");
}
catch (Exception e)
{
return Result.Fail(new List<string>() {$"An error occured, {e.Message}"}); } } public Result<Flight> SelectFlight(string code) { try { var flight = _context.Flight.FirstOrDefault(f => f.Code == code); if (flight == null) return Result<Flight>.NotFound($"Flight {code} not found");
return  Result<Flight>.Ok(flight);
}
catch (Exception e)
{
return Result<Flight>.Fail(new List<string>(){$"An error occured, {e.Message}"}); } } public Result<List<Flight>> SelectFlights() { try { return Result<List<Flight>>.Ok(_context.Flight.Any() ? _context.Flight.ToList() : new List<Flight>()); } catch (Exception e) { return Result<List<Flight>>.Fail(new List<string>() {$"An error occured,{e.Message}"});
}
}
}


The repository does not need to take responsibility for the errors - remove them from here. The flight repository should add CRUD operations to Flight Dto and not return a result - remove the result. Validation should be done before calling the methods. You are not an automapper, use a tool. You do not need to invent a bicycle every time you need a new repository, use generics. Additionally, you do not need to repeat Flight in every method name, they exist in the Flight repository you can infer that from context. All error messages should be unique.

For example, implement an interface:

public interface IRepository<T> where T : EntityBase
{
T GetById(int id);
IEnumerable<T> List();
void Delete(T entity);
void Edit(T entity);
}

public abstract class EntityBase
{
public int Id { get; protected set; }
}


• not sure then, what kind of layer is taking care of the error then? For now the chain is something like: DB => Repository => Service => Controller => UI (React / Angular). – Ehouarn Perret Jan 19 at 19:04