I've inherited a dotNet Core project that heavily uses the C# Functional extensions nuget package, and especially its Result class. This goes as far as returning a Result when retrieving records from the database. I see methods looking like
public async Task<Result<IEnumerable<MyEntity>>> GetAll(CancellationToken token)
{
var variableForDebugging = await _dbContext.MyEntity.ToListAsync(token);
return await _dbContext.MyEntity.ToListAsync(token);
}
This snippet is a fair representation of the real code, I have only changed the name of the entity, but have not omitted any try-catch clauses. I have two questions with respect to the code snippet above:
(1) Does it make sense to return a Result when reading records from the database? If I deliberately ensure that the application cannot connect to the database, I get an exception and not a Result.Failure, and I cannot think of a scenario in which the code snippet would return a Failure result.
(2) When I check the type of the result handed to me by EF, e.g. upon inspecting the type of variableForDebugging, it is of type List<MyEntity>. However, the return type of the method is Task<Result<IEnumerable<MyEntity>>>, so how does the compiler figure out that it can wrap List<MyEntity> in a Result and return Task<Result<IEnumerable<MyEntity>>>?