Skip to content

Commit 0cebeea

Browse files
authored
Improve usability of ResilienceStrategy<T> (#1428)
1 parent 59e461a commit 0cebeea

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

src/Polly.Core/PublicAPI.Unshipped.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,18 @@ Polly.ResilienceStrategy.ExecuteAsync<TState>(System.Func<Polly.ResilienceContex
248248
Polly.ResilienceStrategy.ExecuteAsync<TState>(System.Func<TState, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask
249249
Polly.ResilienceStrategy.ExecuteOutcomeAsync<TResult, TState>(System.Func<Polly.ResilienceContext!, TState, System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
250250
Polly.ResilienceStrategy.ResilienceStrategy() -> void
251-
Polly.ResilienceStrategy<TResult>
252-
Polly.ResilienceStrategy<TResult>.Execute(System.Func<Polly.ResilienceContext!, TResult>! callback, Polly.ResilienceContext! context) -> TResult
253-
Polly.ResilienceStrategy<TResult>.Execute(System.Func<System.Threading.CancellationToken, TResult>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult
254-
Polly.ResilienceStrategy<TResult>.Execute(System.Func<TResult>! callback) -> TResult
255-
Polly.ResilienceStrategy<TResult>.Execute<TState>(System.Func<Polly.ResilienceContext!, TState, TResult>! callback, Polly.ResilienceContext! context, TState state) -> TResult
256-
Polly.ResilienceStrategy<TResult>.Execute<TState>(System.Func<TState, System.Threading.CancellationToken, TResult>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult
257-
Polly.ResilienceStrategy<TResult>.Execute<TState>(System.Func<TState, TResult>! callback, TState state) -> TResult
258-
Polly.ResilienceStrategy<TResult>.ExecuteAsync(System.Func<Polly.ResilienceContext!, System.Threading.Tasks.ValueTask<TResult>>! callback, Polly.ResilienceContext! context) -> System.Threading.Tasks.ValueTask<TResult>
259-
Polly.ResilienceStrategy<TResult>.ExecuteAsync(System.Func<System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<TResult>>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<TResult>
260-
Polly.ResilienceStrategy<TResult>.ExecuteAsync<TState>(System.Func<Polly.ResilienceContext!, TState, System.Threading.Tasks.ValueTask<TResult>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask<TResult>
261-
Polly.ResilienceStrategy<TResult>.ExecuteAsync<TState>(System.Func<TState, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<TResult>>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<TResult>
262-
Polly.ResilienceStrategy<TResult>.ExecuteOutcomeAsync<TState>(System.Func<Polly.ResilienceContext!, TState, System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
251+
Polly.ResilienceStrategy<T>
252+
Polly.ResilienceStrategy<T>.Execute<TResult, TState>(System.Func<Polly.ResilienceContext!, TState, TResult>! callback, Polly.ResilienceContext! context, TState state) -> TResult
253+
Polly.ResilienceStrategy<T>.Execute<TResult, TState>(System.Func<TState, System.Threading.CancellationToken, TResult>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult
254+
Polly.ResilienceStrategy<T>.Execute<TResult, TState>(System.Func<TState, TResult>! callback, TState state) -> TResult
255+
Polly.ResilienceStrategy<T>.Execute<TResult>(System.Func<Polly.ResilienceContext!, TResult>! callback, Polly.ResilienceContext! context) -> TResult
256+
Polly.ResilienceStrategy<T>.Execute<TResult>(System.Func<System.Threading.CancellationToken, TResult>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult
257+
Polly.ResilienceStrategy<T>.Execute<TResult>(System.Func<TResult>! callback) -> TResult
258+
Polly.ResilienceStrategy<T>.ExecuteAsync<TResult, TState>(System.Func<Polly.ResilienceContext!, TState, System.Threading.Tasks.ValueTask<TResult>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask<TResult>
259+
Polly.ResilienceStrategy<T>.ExecuteAsync<TResult, TState>(System.Func<TState, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<TResult>>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<TResult>
260+
Polly.ResilienceStrategy<T>.ExecuteAsync<TResult>(System.Func<Polly.ResilienceContext!, System.Threading.Tasks.ValueTask<TResult>>! callback, Polly.ResilienceContext! context) -> System.Threading.Tasks.ValueTask<TResult>
261+
Polly.ResilienceStrategy<T>.ExecuteAsync<TResult>(System.Func<System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<TResult>>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<TResult>
262+
Polly.ResilienceStrategy<T>.ExecuteOutcomeAsync<TResult, TState>(System.Func<Polly.ResilienceContext!, TState, System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
263263
Polly.ResilienceStrategyBuilder
264264
Polly.ResilienceStrategyBuilder.Build() -> Polly.ResilienceStrategy!
265265
Polly.ResilienceStrategyBuilder.ResilienceStrategyBuilder() -> void

src/Polly.Core/ResilienceStrategy.TResult.Async.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace Polly;
77
/// <summary>
88
/// Resilience strategy is used to execute the user-provided callbacks.
99
/// </summary>
10-
/// <typeparam name="TResult">The type of result this strategy supports.</typeparam>
10+
/// <typeparam name="T">The type of result this strategy supports.</typeparam>
1111
/// <remarks>
12-
/// Resilience strategy supports various types of callbacks of <typeparamref name="TResult"/> result type
12+
/// Resilience strategy supports various types of callbacks of <typeparamref name="T"/> result type
1313
/// and provides a unified way to execute them. This includes overloads for synchronous and asynchronous callbacks.
1414
/// </remarks>
15-
public partial class ResilienceStrategy<TResult>
15+
public partial class ResilienceStrategy<T>
1616
{
1717
internal ResilienceStrategy(ResilienceStrategy strategy) => Strategy = strategy;
1818

@@ -21,16 +21,18 @@ public partial class ResilienceStrategy<TResult>
2121
/// <summary>
2222
/// Executes the specified callback.
2323
/// </summary>
24+
/// <typeparam name="TResult">The type of the result.</typeparam>
2425
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
2526
/// <param name="callback">The user-provided callback.</param>
2627
/// <param name="context">The context associated with the callback.</param>
2728
/// <param name="state">The state associated with the callback.</param>
2829
/// <returns>The instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
2930
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
30-
public ValueTask<TResult> ExecuteAsync<TState>(
31+
public ValueTask<TResult> ExecuteAsync<TResult, TState>(
3132
Func<ResilienceContext, TState, ValueTask<TResult>> callback,
3233
ResilienceContext context,
3334
TState state)
35+
where TResult : T
3436
{
3537
Guard.NotNull(callback);
3638
Guard.NotNull(context);
@@ -41,13 +43,15 @@ public ValueTask<TResult> ExecuteAsync<TState>(
4143
/// <summary>
4244
/// Executes the specified callback.
4345
/// </summary>
46+
/// <typeparam name="TResult">The type of the result.</typeparam>
4447
/// <param name="callback">The user-provided callback.</param>
4548
/// <param name="context">The context associated with the callback.</param>
4649
/// <returns>The instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
4750
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
48-
public ValueTask<TResult> ExecuteAsync(
51+
public ValueTask<TResult> ExecuteAsync<TResult>(
4952
Func<ResilienceContext, ValueTask<TResult>> callback,
5053
ResilienceContext context)
54+
where TResult : T
5155
{
5256
Guard.NotNull(callback);
5357
Guard.NotNull(context);
@@ -58,16 +62,18 @@ public ValueTask<TResult> ExecuteAsync(
5862
/// <summary>
5963
/// Executes the specified callback.
6064
/// </summary>
65+
/// <typeparam name="TResult">The type of the result.</typeparam>
6166
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
6267
/// <param name="callback">The user-provided callback.</param>
6368
/// <param name="state">The state associated with the callback.</param>
6469
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the callback.</param>
6570
/// <returns>The instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
6671
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
67-
public ValueTask<TResult> ExecuteAsync<TState>(
72+
public ValueTask<TResult> ExecuteAsync<TResult, TState>(
6873
Func<TState, CancellationToken, ValueTask<TResult>> callback,
6974
TState state,
7075
CancellationToken cancellationToken = default)
76+
where TResult : T
7177
{
7278
Guard.NotNull(callback);
7379

@@ -77,11 +83,12 @@ public ValueTask<TResult> ExecuteAsync<TState>(
7783
/// <summary>
7884
/// Executes the specified callback.
7985
/// </summary>
86+
/// <typeparam name="TResult">The type of the result.</typeparam>
8087
/// <param name="callback">The user-provided callback.</param>
8188
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the callback.</param>
8289
/// <returns>The instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
8390
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
84-
public ValueTask<TResult> ExecuteAsync(
91+
public ValueTask<TResult> ExecuteAsync<TResult>(
8592
Func<CancellationToken, ValueTask<TResult>> callback,
8693
CancellationToken cancellationToken = default)
8794
{
@@ -93,6 +100,7 @@ public ValueTask<TResult> ExecuteAsync(
93100
/// <summary>
94101
/// Executes the specified outcome-based callback.
95102
/// </summary>
103+
/// <typeparam name="TResult">The type of the result.</typeparam>
96104
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
97105
/// <param name="callback">The user-provided callback.</param>
98106
/// <param name="context">The context associated with the callback.</param>
@@ -103,10 +111,11 @@ public ValueTask<TResult> ExecuteAsync(
103111
/// This method is for advanced and high performance scenarios. The caller must make sure that the <paramref name="callback"/>
104112
/// does not throw any exceptions. Instead, it converts them to <see cref="Outcome{TResult}"/>.
105113
/// </remarks>
106-
public ValueTask<Outcome<TResult>> ExecuteOutcomeAsync<TState>(
114+
public ValueTask<Outcome<TResult>> ExecuteOutcomeAsync<TResult, TState>(
107115
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
108116
ResilienceContext context,
109117
TState state)
118+
where TResult : T
110119
{
111120
Guard.NotNull(callback);
112121
Guard.NotNull(context);

src/Polly.Core/ResilienceStrategy.TResult.Sync.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ namespace Polly;
22

33
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
44

5-
public partial class ResilienceStrategy<TResult>
5+
public partial class ResilienceStrategy<T>
66
{
77
/// <summary>
88
/// Executes the specified callback.
99
/// </summary>
10+
/// <typeparam name="TResult">The type of the result.</typeparam>
1011
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
1112
/// <param name="callback">The user-provided callback.</param>
1213
/// <param name="context">The context associated with the callback.</param>
1314
/// <param name="state">The state associated with the callback.</param>
1415
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
1516
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
16-
public TResult Execute<TState>(
17+
public TResult Execute<TResult, TState>(
1718
Func<ResilienceContext, TState, TResult> callback,
1819
ResilienceContext context,
1920
TState state)
21+
where TResult : T
2022
{
2123
Guard.NotNull(callback);
2224
Guard.NotNull(context);
@@ -27,13 +29,15 @@ public TResult Execute<TState>(
2729
/// <summary>
2830
/// Executes the specified callback.
2931
/// </summary>
32+
/// <typeparam name="TResult">The type of the result.</typeparam>
3033
/// <param name="callback">The user-provided callback.</param>
3134
/// <param name="context">The context associated with the callback.</param>
3235
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
3336
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
34-
public TResult Execute(
37+
public TResult Execute<TResult>(
3538
Func<ResilienceContext, TResult> callback,
3639
ResilienceContext context)
40+
where TResult : T
3741
{
3842
Guard.NotNull(callback);
3943
Guard.NotNull(context);
@@ -44,13 +48,15 @@ public TResult Execute(
4448
/// <summary>
4549
/// Executes the specified callback.
4650
/// </summary>
51+
/// <typeparam name="TResult">The type of the result.</typeparam>
4752
/// <param name="callback">The user-provided callback.</param>
4853
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the callback.</param>
4954
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
5055
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
51-
public TResult Execute(
56+
public TResult Execute<TResult>(
5257
Func<CancellationToken, TResult> callback,
5358
CancellationToken cancellationToken = default)
59+
where TResult : T
5460
{
5561
Guard.NotNull(callback);
5662

@@ -60,10 +66,12 @@ public TResult Execute(
6066
/// <summary>
6167
/// Executes the specified callback.
6268
/// </summary>
69+
/// <typeparam name="TResult">The type of the result.</typeparam>
6370
/// <param name="callback">The user-provided callback.</param>
6471
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
6572
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
66-
public TResult Execute(Func<TResult> callback)
73+
public TResult Execute<TResult>(Func<TResult> callback)
74+
where TResult : T
6775
{
6876
Guard.NotNull(callback);
6977

@@ -73,12 +81,14 @@ public TResult Execute(Func<TResult> callback)
7381
/// <summary>
7482
/// Executes the specified callback.
7583
/// </summary>
84+
/// <typeparam name="TResult">The type of the result.</typeparam>
7685
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
7786
/// <param name="callback">The user-provided callback.</param>
7887
/// <param name="state">The state associated with the callback.</param>
7988
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
8089
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
81-
public TResult Execute<TState>(Func<TState, TResult> callback, TState state)
90+
public TResult Execute<TResult, TState>(Func<TState, TResult> callback, TState state)
91+
where TResult : T
8292
{
8393
Guard.NotNull(callback);
8494

@@ -88,16 +98,18 @@ public TResult Execute<TState>(Func<TState, TResult> callback, TState state)
8898
/// <summary>
8999
/// Executes the specified callback.
90100
/// </summary>
101+
/// <typeparam name="TResult">The type of the result.</typeparam>
91102
/// <typeparam name="TState">The type of state associated with the callback.</typeparam>
92103
/// <param name="callback">The user-provided callback.</param>
93104
/// <param name="state">The state associated with the callback.</param>
94105
/// <param name="cancellationToken">The <see cref="CancellationToken"/> associated with the callback.</param>
95106
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
96107
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> is <see langword="null"/>.</exception>
97-
public TResult Execute<TState>(
108+
public TResult Execute<TResult, TState>(
98109
Func<TState, CancellationToken, TResult> callback,
99110
TState state,
100111
CancellationToken cancellationToken = default)
112+
where TResult : T
101113
{
102114
Guard.NotNull(callback);
103115

0 commit comments

Comments
 (0)