Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 1.51 KB

File metadata and controls

26 lines (20 loc) · 1.51 KB

Add information to Result instances

All types of Result implementations contain a Statement property which encapsulates collections of Fact and Warning classes.

See various WithFact and WithWarning extension methods to add fact and warning information to result instances.

Default Failure implementation used in Result and Result<TValue> objects, has a Type property holding FailureType and also contains a collection of Errors.

See various static methods of Result objects to create a Failed Result containing Error information. Errors can only be attached to a Failed Result instance during Result instance creation and cannot be added or removed afterwards.

public async Task<Result<GetBookByIdResponse>> GetBookById(GetBookByIdRequest req, CancellationToken ct)
{
    var entity = await db.Books.FirstOrDefaultAsync(b => b.Id == req.Id, ct);

    return entity is null ?
      Result<GetBookByIdResponse>.NotFound($"Book with id: {0} not found.", req.Id)
        .WithFact($"dbContextId: {db.ContextId}") :
      Result.Ok(new GetBookByIdResponse(
        Id: entity.Id,
        Title: entity.Title,
        Author: entity.Author,
        Price: entity.Price))
      .WithFact($"dbContextId: {db.ContextId}");
}