-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Policy.HandleSyntax.cs
107 lines (94 loc) · 5.48 KB
/
Policy.HandleSyntax.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
namespace Polly
{
public partial class Policy
{
/// <summary>
/// Specifies the type of exception that this policy can handle.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder Handle<TException>() where TException : Exception
{
ExceptionPredicate predicate = exception => exception is TException;
return new PolicyBuilder(predicate);
}
/// <summary>
/// Specifies the type of exception that this policy can handle with additional filters on this exception type.
/// </summary>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="exceptionPredicate">The exception predicate to filter the type of exception this policy can handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
{
ExceptionPredicate predicate = exception => exception is TException &&
exceptionPredicate((TException)exception);
return new PolicyBuilder(predicate);
}
/// <summary>
/// Specifies the type of return result that this policy can handle with additional filters on the result.
/// </summary>
/// <typeparam name="TResult">The type of return values this policy will handle.</typeparam>
/// <param name="resultPredicate">The predicate to filter results this policy will handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult<TResult>(Func<TResult, bool> resultPredicate)
{
return new PolicyBuilder<TResult>(resultPredicate);
}
/// <summary>
/// Specifies the type of return result that this policy can handle, and a result value which the policy will handle.
/// </summary>
/// <typeparam name="TResult">The type of return values this policy will handle.</typeparam>
/// <param name="result">The TResult value this policy will handle.</param>
/// <remarks>This policy filter matches the <paramref name="result"/> value returned using .Equals(), ideally suited for value types such as int and enum. To match characteristics of class return types, consider the overload taking a result predicate.</remarks>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult<TResult>(TResult result)
{
return HandleResult(new Func<TResult, bool>(r => (r != null && r.Equals(result)) || (r == null && result == null)));
}
}
public partial class Policy<TResult>
{
/// <summary>
/// Specifies the type of exception that this policy can handle.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> Handle<TException>() where TException : Exception
{
ExceptionPredicate predicate = exception => exception is TException;
return new PolicyBuilder<TResult>(predicate);
}
/// <summary>
/// Specifies the type of exception that this policy can handle with additional filters on this exception type.
/// </summary>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="exceptionPredicate">The exception predicate to filter the type of exception this policy can handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
{
ExceptionPredicate predicate = exception => exception is TException &&
exceptionPredicate((TException)exception);
return new PolicyBuilder<TResult>(predicate);
}
/// <summary>
/// Specifies a filter on the return result values that this strongly-typed generic policy will handle.
/// </summary>
/// <param name="resultPredicate">The predicate to filter the results this policy will handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult(Func<TResult, bool> resultPredicate)
{
return new PolicyBuilder<TResult>(resultPredicate);
}
/// <summary>
/// Specifies a return result value which the strongly-typed generic policy will handle.
/// </summary>
/// <param name="result">The TResult value this policy will handle.</param>
/// <remarks>This policy filter matches the <paramref name="result"/> value returned using .Equals(), ideally suited for value types such as int and enum. To match characteristics of class return types, consider the overload taking a result predicate.</remarks>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult(TResult result)
{
return HandleResult(r => (r != null && r.Equals(result)) || (r == null && result == null));
}
}
}