title: How to use language-ext functors either to handle errors in repository API?
body: <p>I am playing with <a href="https://github.com/louthy/language-ext" rel="nofollow noreferrer">language-ext</a> a library that encompass functional patterns for C# (and its smaller <a href="https://github.com/louthy/csharp-monad" rel="nofollow noreferrer">sibling csharp-monad</a>) and I would like to apply some of the patterns to deal with errors in an ASP.NET Core application.</p>
  
  <p>In my applications I often have the following layers:</p>
  
  <ul>
  <li>ASP.NET Actions (the REST API surface) Layer</li>
  <li>Domain Service Layer</li>
  <li>Persistence Layer / Repository</li>
  <li>Database Layer</li>
  </ul>
  
  <p>Example of a simplified code:</p>
  
  <pre><code>[Route("api/v1/[controller]")]
  public class FlightsController : Controller
  {
      private readonly IFlightService _flightService;
      private readonly IDistanceCalculator _distanceCalculator;
  
      // [...]
  
      public FlightsController(IFlightService flightService, IDistanceCalculator distanceCalculator)
      {
          _flightService = flightService;
          _distanceCalculator = distanceCalculator;
      }
  
      [HttpGet]
      public IActionResult Get()
      {
          var result = _flightService.GetFlights();
  
          switch (result)
          {
              case Success&lt;List&lt;FlightDTO&gt;&gt; success:
                  var flights = new List&lt;FlightInformations&gt;();
                  success.Value.ForEach(x =&gt; flights.Add(new FlightInformations(x)));
                  return Ok(flights);
              case Failure&lt;List&lt;FlightDTO&gt;&gt; failure:
                  return StatusCode((int) HttpStatusCode.InternalServerError, failure.Errors);
              default:
                  return StatusCode((int) HttpStatusCode.InternalServerError, "Unexpected error");
          }
      }
  }
  
  public class FlightService : IFlightService
  {
      private readonly IFlightRepository _flightRepository;
  
      public FlightService(IFlightRepository flightRepository)
      {
          _flightRepository = flightRepository;
      }
  
      // [...]
  
      public Result&lt;List&lt;FlightDTO&gt;&gt; GetFlights()
      {
          var res = _flightRepository.SelectFlights();
          switch (res)
          {
              case Success&lt;List&lt;Flight&gt;&gt; success:
                  var flights = new List&lt;FlightDTO&gt;();
                  success.Value.ForEach(x =&gt; flights.Add(new FlightDTO(x)));
                  return Result&lt;List&lt;FlightDTO&gt;&gt;.Ok(flights);
              case Failure&lt;List&lt;Flight&gt;&gt; failure:
                  return Result&lt;List&lt;FlightDTO&gt;&gt;.Fail(failure.Errors);
              default:
                  return Result&lt;List&lt;FlightDTO&gt;&gt;.Fail(new List&lt;string&gt;() {"An error occured"});
          }
      }
  }
  
  public class FlightRepository : IFlightRepository
  {
      private readonly FlightManagerDbContext _context;
  
      public FlightRepository(FlightManagerDbContext context)
      {
          _context = context;
      }
  
      // [...]
  
      public Result&lt;List&lt;Flight&gt;&gt; SelectFlights()
      {
          try
          {
              return Result&lt;List&lt;Flight&gt;&gt;.Ok(_context.Flight.Any()
                  ? _context.Flight.ToList()
                  : new List&lt;Flight&gt;());
          }
          catch (Exception e)
          {
              return Result&lt;List&lt;Flight&gt;&gt;.Fail(new List&lt;string&gt;() {$"An error occured,{e.Message}"});
          }
      }
  }
  </code></pre>
  
  <p>I would like to avoid using <code>try</code> / <code>catch</code> directly and use either instead, how can I achieve that?</p>