Skip to content

Commit dfa786e

Browse files
committed
2 parents 1b4e81f + b0fbfe3 commit dfa786e

File tree

6 files changed

+1190
-12
lines changed

6 files changed

+1190
-12
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Result.Ok<int>(5).ToResult<float>(v => v);
327327
Result.Fail<int>("Failed").ToResult<float>();
328328

329329
// converting a result to a result from type Result
330-
Result.Ok<int>().ToResult();
330+
Result.Ok<int>().ToResult();
331331
```
332332

333333
### Implicit conversion from T to success result ```Result<T>```
@@ -337,6 +337,41 @@ string myString = "hello world";
337337
Result<T> result = myString;
338338
```
339339

340+
### Bind the result to another result
341+
342+
Binding is a transformation that returns a `Result` | `Result<T>`.
343+
It only evaluates the transformation if the original result is successful.
344+
The reasons of both `Result` will be merged into a new flattened `Result`.
345+
346+
```csharp
347+
// converting a result to a result which may fail
348+
Result<string> r = Result.Ok(8)
349+
.Bind(v => v == 5 ? "five" : Result.Fail<string>("It is not five"));
350+
351+
// converting a failed result to a result, which can also fail,
352+
// returns a result with the errors of the first result only,
353+
// the transformation is not evaluated because the value of the first result is not available
354+
Result<string> r = Result.Fail<int>("Not available")
355+
.Bind(v => v == 5 ? "five" : Result.Fail<string>("It is not five"));
356+
357+
// converting a result with value to a Result via a transformation which may fail
358+
Result.Ok(5).Bind(x => Result.OkIf(x == 6, "Number is not 6"));
359+
360+
// converting a result without value into a Result
361+
Result.Ok().Bind(() => Result.Ok(5));
362+
363+
// just running an action if the original result is sucessful.
364+
Result r = Result.Ok().Bind(() => Result.Ok());
365+
```
366+
367+
The `Bind` has asynchronous overloads.
368+
369+
```csharp
370+
var result = await Result.Ok(5)
371+
.Bind(int n => Task.FromResult(Result.Ok(n + 1).WithSuccess("Added one")))
372+
.Bind(int n => /* next continuation */);
373+
```
374+
340375
### Set global factories for ISuccess/IError/IExceptionalError
341376

342377
Within the FluentResults library in some scenarios an ISuccess, IError or IExceptionalError object is created. For example if the method ```Result.Fail("My Error")``` is called then internally an IError object is created. If you need to overwrite this behavior and create in this scenario a custom error class then you can set the error factory via the settings. The same extension points are also available for ISuccess and IExceptionalError.

src/FluentResults.Test/Playground.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using FluentResults.Extensions;
35

46
namespace FluentResults.Test
57
{
@@ -21,31 +23,30 @@ public void Simple()
2123
var voidResult = Result.Ok();
2224

2325
voidResult = Result.Ok()
24-
.WithSuccess("This is a success")
25-
.WithSuccess("This is a second success");
26+
.WithSuccess("This is a success")
27+
.WithSuccess("This is a second success");
2628

2729
voidResult = Result.Fail("First error");
2830

2931
voidResult = Result.Fail(new Error("First error"));
3032

3133
voidResult = Result.Fail("First error")
32-
.WithError("second error")
33-
.WithError(new CustomError().CausedBy(new InvalidCastException()));
34-
34+
.WithError("second error")
35+
.WithError(new CustomError().CausedBy(new InvalidCastException()));
3536

3637
var valueResult = Result.Ok<int>(default)
37-
.WithSuccess("first success")
38-
.WithValue(3);
38+
.WithSuccess("first success")
39+
.WithValue(3);
3940

4041
valueResult = Result.Ok(3);
4142

4243
valueResult = Result.Ok<int>(default)
43-
.WithValue(3);
44+
.WithValue(3);
4445

4546
valueResult = Result.Fail<int>("First error");
4647

4748
valueResult = Result.Fail<int>(new Error("first error"))
48-
.WithError("second error");
49+
.WithError("second error");
4950

5051
IEnumerable<Result> results = new List<Result>();
5152
Result mergedResult = results.Merge();
@@ -89,5 +90,26 @@ public void LogTest()
8990
var result1 = Result.Ok();
9091
result1 = result1.Log();
9192
}
93+
94+
public async Task BindTests()
95+
{
96+
var r = Result.Ok(1);
97+
98+
var r1 = await r.Bind(v => GetVResult())
99+
.Bind(v => GetVResultAsync())
100+
.Bind(v => GetVResult())
101+
.Bind(v => GetVResultAsync())
102+
;
103+
104+
var r2 = await r.Bind(v => GetVResultAsync());
105+
}
106+
107+
private Result GetResult() => Result.Ok();
108+
109+
private Result<int> GetVResult() => Result.Ok(1);
110+
111+
private Task<Result> GetResultAsync() => Task.FromResult(GetResult());
112+
113+
private Task<Result<int>> GetVResultAsync() => Task.FromResult(GetVResult());
92114
}
93115
}

0 commit comments

Comments
 (0)