Skip to content

Commit 06fb4f7

Browse files
authored
Ensure there are async result extensions for all result methods. (#9)
1 parent 5d0036a commit 06fb4f7

File tree

2 files changed

+248
-48
lines changed

2 files changed

+248
-48
lines changed

src/Winton.DomainModelling.Abstractions/AsyncResultExtensions.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,115 @@ namespace Winton.DomainModelling
1212
/// </summary>
1313
public static class AsyncResultExtensions
1414
{
15+
/// <summary>
16+
/// Combines this result with another.
17+
/// If both are successful then <paramref>combineData</paramref> is invoked;
18+
/// else if either is a failure then <paramref>combineErrors</paramref> is invoked.
19+
/// </summary>
20+
/// <typeparam name="TData">
21+
/// The type of data encapsulated by the result.
22+
/// </typeparam>
23+
/// <typeparam name="TOtherData">
24+
/// The type of data in the other result.
25+
/// </typeparam>
26+
/// <typeparam name="TNewData">
27+
/// The type of data in the combined result.
28+
/// </typeparam>
29+
/// <param name="resultTask">
30+
/// The asynchronous result that this extension method is invoked on.
31+
/// </param>
32+
/// <param name="other">
33+
/// The other result to be combined with this one.
34+
/// </param>
35+
/// <param name="combineData">
36+
/// The function that is invoked to combine the data when both of the results are successful.
37+
/// </param>
38+
/// <param name="combineErrors">
39+
/// The function that is invoked to combine the errors when either of the results is a failure.
40+
/// </param>
41+
/// <returns>
42+
/// A new <see cref="Result{TNewData}" />.
43+
/// </returns>
44+
public static async Task<Result<TNewData>> Combine<TData, TOtherData, TNewData>(
45+
this Task<Result<TData>> resultTask,
46+
Result<TOtherData> other,
47+
Func<TData, TOtherData, TNewData> combineData,
48+
Func<Error, Error, Error> combineErrors)
49+
{
50+
Result<TData> result = await resultTask;
51+
return result.Combine(other, combineData, combineErrors);
52+
}
53+
54+
/// <summary>
55+
/// Combines this result with another.
56+
/// If both are successful then <paramref>combineData</paramref> is invoked;
57+
/// else if either is a failure then <paramref>combineErrors</paramref> is invoked.
58+
/// </summary>
59+
/// <typeparam name="TData">
60+
/// The type of data encapsulated by the result.
61+
/// </typeparam>
62+
/// <typeparam name="TOtherData">
63+
/// The type of data in the other result.
64+
/// </typeparam>
65+
/// <typeparam name="TNewData">
66+
/// The type of data in the combined result.
67+
/// </typeparam>
68+
/// <param name="resultTask">
69+
/// The asynchronous result that this extension method is invoked on.
70+
/// </param>
71+
/// <param name="otherResultTask">
72+
/// The other asynchronous result to be combined with this one.
73+
/// </param>
74+
/// <param name="combineData">
75+
/// The function that is invoked to combine the data when both of the results are successful.
76+
/// </param>
77+
/// <param name="combineErrors">
78+
/// The function that is invoked to combine the errors when either of the results is a failure.
79+
/// </param>
80+
/// <returns>
81+
/// A new <see cref="Result{TNewData}" />.
82+
/// </returns>
83+
public static async Task<Result<TNewData>> Combine<TData, TOtherData, TNewData>(
84+
this Task<Result<TData>> resultTask,
85+
Task<Result<TOtherData>> otherResultTask,
86+
Func<TData, TOtherData, TNewData> combineData,
87+
Func<Error, Error, Error> combineErrors)
88+
{
89+
Result<TData> result = await resultTask;
90+
Result<TOtherData> otherResult = await otherResultTask;
91+
return result.Combine(otherResult, combineData, combineErrors);
92+
}
93+
94+
/// <summary>
95+
/// Used to match on whether this result is a success or a failure.
96+
/// </summary>
97+
/// <typeparam name="TData">
98+
/// The type of data encapsulated by the result.
99+
/// </typeparam>
100+
/// <typeparam name="T">
101+
/// The type that is returned.
102+
/// </typeparam>
103+
/// <param name="resultTask">
104+
/// The asynchronous result that this extension method is invoked on.
105+
/// </param>
106+
/// <param name="onSuccess">
107+
/// The function that is invoked if this result represents a success.
108+
/// </param>
109+
/// <param name="onFailure">
110+
/// The function that is invoked if this result represents a failure.
111+
/// </param>
112+
/// <returns>
113+
/// A value that is mapped from either the data or the error.
114+
/// </returns>
115+
public static async Task<T> Match<TData, T>(
116+
this Task<Result<TData>> resultTask,
117+
Func<TData, T> onSuccess,
118+
Func<Error, T> onFailure)
119+
{
120+
Result<TData> result = await resultTask;
121+
return result.Match(onSuccess, onFailure);
122+
}
123+
15124
/// <summary>
16125
/// Invokes the specified action if the result was successful and returns the original result.
17126
/// </summary>

0 commit comments

Comments
 (0)