-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoResult.cs
More file actions
84 lines (72 loc) · 2.25 KB
/
Copy pathDoResult.cs
File metadata and controls
84 lines (72 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace TedToolkit.Fluent;
/// <summary>
/// Do Result
/// </summary>
/// <param name="fluent"></param>
/// <typeparam name="TValue"></typeparam>
public abstract class DoResultBase<TValue>(Fluent<TValue> fluent)
{
private protected Fluent<TValue> Fluent { get; } = fluent;
/// <summary>
/// Did it run the method.
/// </summary>
public abstract bool DidIt { get; }
/// <inheritdoc cref="Fluent{TTarget}.Result" />
public TValue Result => Continue.Result;
/// <summary>
/// Continue to do sth.
/// </summary>
public Fluent<TValue> Continue => Fluent;
/// <summary>
/// Convert it to bool.
/// </summary>
/// <param name="doResult"></param>
/// <returns></returns>
public static implicit operator bool(DoResultBase<TValue> doResult)
{
return doResult.DidIt;
}
}
/// <inheritdoc />
public class DoResult<TValue>(Fluent<TValue> fluent, Lazy<bool> lazy) : DoResultBase<TValue>(fluent)
{
public DoResult(Fluent<TValue> fluent) : this(fluent, new Lazy<bool>(() => true))
{
}
/// <inheritdoc />
public override bool DidIt => lazy.Value;
}
/// <inheritdoc />
public sealed class DoResult<TValue, TResult>(Fluent<TValue> fluent, Lazy<(bool, TResult)> lazy)
: DoResultBase<TValue>(fluent)
{
public DoResult(Fluent<TValue> fluent, TResult result)
: this(fluent, new Lazy<(bool, TResult)>(() => (true, result)))
{
}
/// <inheritdoc />
public override bool DidIt => lazy.Value.Item1;
/// <summary>
/// The return value
/// </summary>
public TResult ReturnValue => lazy.Value.Item2;
/// <summary>
/// Continue when can continue.
/// </summary>
/// <param name="canContinue"></param>
/// <param name="type"></param>
/// <returns>fluent type</returns>
public Fluent<TValue> ContinueWhen(Predicate<TResult> canContinue, FluentType? type = null)
{
return Fluent.AddCondition(() => canContinue(lazy.Value.Item2), type);
}
/// <summary>
/// Convert to the result.
/// </summary>
/// <param name="doResult"></param>
/// <returns></returns>
public static implicit operator TResult(DoResult<TValue, TResult> doResult)
{
return doResult.ReturnValue;
}
}