@@ -7,112 +7,96 @@ namespace Asp.Versioning;
77/// </summary>
88public static class IPolicyManagerExtensions
99{
10- /// <summary>
11- /// Returns the policy for the specified API and version.
12- /// </summary>
10+ /// <typeparam name="T">The type of policy.</typeparam>
1311 /// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
14- /// <param name="apiVersion">The API version to get the policy for.</param>
15- /// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
16- /// <typeparam name="TPolicy">The type of policy.</typeparam>
17- /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
18- public static bool TryGetPolicy < TPolicy > (
19- this IPolicyManager < TPolicy > policyManager ,
20- ApiVersion apiVersion ,
21- [ MaybeNullWhen ( false ) ] out TPolicy policy )
12+ extension < T > ( IPolicyManager < T> policyManager )
2213 {
23- ArgumentNullException . ThrowIfNull ( policyManager ) ;
24- return policyManager . TryGetPolicy ( default , apiVersion , out policy ) ;
25- }
26-
27- /// <summary>
28- /// Returns the policy for the specified API and version.
29- /// </summary>
30- /// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
31- /// <param name="name">The name of the API.</param>
32- /// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
33- /// <typeparam name="TPolicy">The type of policy.</typeparam>
34- /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
35- public static bool TryGetPolicy < TPolicy > (
36- this IPolicyManager < TPolicy > policyManager ,
37- string name ,
38- [ MaybeNullWhen ( false ) ] out TPolicy policy )
39- {
40- ArgumentNullException . ThrowIfNull ( policyManager ) ;
41- return policyManager . TryGetPolicy ( name , default , out policy ) ;
42- }
43-
44- /// <summary>
45- /// Attempts to resolve a policy for the specified name and API version combination.
46- /// </summary>
47- /// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
48- /// <param name="name">The name of the API.</param>
49- /// <param name="apiVersion">The API version to get the policy for.</param>
50- /// <typeparam name="TPolicy">The type of policy.</typeparam>
51- /// <returns>The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</returns>
52- /// <remarks>The resolution order is as follows:
53- /// <list type="bullet">
54- /// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
55- /// <item><paramref name="name"/> only</item>
56- /// <item><paramref name="apiVersion"/> only</item>
57- /// </list>
58- /// </remarks>
59- public static TPolicy ? ResolvePolicyOrDefault < TPolicy > (
60- this IPolicyManager < TPolicy > policyManager ,
61- string ? name ,
62- ApiVersion ? apiVersion )
63- {
64- ArgumentNullException . ThrowIfNull ( policyManager ) ;
14+ /// <summary>
15+ /// Returns the policy for the specified API and version.
16+ /// </summary>
17+ /// <param name="apiVersion">The API version to get the policy for.</param>
18+ /// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
19+ /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
20+ public bool TryGetPolicy ( ApiVersion apiVersion , [ MaybeNullWhen ( false ) ] out T policy )
21+ {
22+ ArgumentNullException . ThrowIfNull ( policyManager ) ;
23+ return policyManager . TryGetPolicy ( default , apiVersion , out policy ) ;
24+ }
6525
66- if ( policyManager . TryResolvePolicy ( name , apiVersion , out var policy ) )
26+ /// <summary>
27+ /// Returns the policy for the specified API and version.
28+ /// </summary>
29+ /// <param name="name">The name of the API.</param>
30+ /// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
31+ /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
32+ public bool TryGetPolicy ( string name , [ MaybeNullWhen ( false ) ] out T policy )
6733 {
68- return policy ;
34+ ArgumentNullException . ThrowIfNull ( policyManager ) ;
35+ return policyManager . TryGetPolicy ( name , default , out policy ) ;
6936 }
7037
71- return default ;
72- }
38+ /// <summary>
39+ /// Attempts to resolve a policy for the specified name and API version combination.
40+ /// </summary>
41+ /// <param name="name">The name of the API.</param>
42+ /// <param name="apiVersion">The API version to get the policy for.</param>
43+ /// <returns>The applicable <typeparamref name="T">policy</typeparamref>, if any.</returns>
44+ /// <remarks>The resolution order is as follows:
45+ /// <list type="bullet">
46+ /// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
47+ /// <item><paramref name="name"/> only</item>
48+ /// <item><paramref name="apiVersion"/> only</item>
49+ /// </list>
50+ /// </remarks>
51+ public T ? ResolvePolicyOrDefault ( string ? name , ApiVersion ? apiVersion )
52+ {
53+ ArgumentNullException . ThrowIfNull ( policyManager ) ;
7354
74- /// <summary>
75- /// Attempts to resolve a policy for the specified name and API version combination.
76- /// </summary>
77- /// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
78- /// <param name="name">The name of the API.</param>
79- /// <param name="apiVersion">The API version to get the policy for.</param>
80- /// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
81- /// <typeparam name="TPolicy">The type of policy.</typeparam>
82- /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
83- /// <remarks>The resolution order is as follows:
84- /// <list type="bullet">
85- /// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
86- /// <item><paramref name="name"/> only</item>
87- /// <item><paramref name="apiVersion"/> only</item>
88- /// </list>
89- /// </remarks>
90- public static bool TryResolvePolicy < TPolicy > (
91- this IPolicyManager < TPolicy > policyManager ,
92- string ? name ,
93- ApiVersion ? apiVersion ,
94- [ MaybeNullWhen ( false ) ] out TPolicy policy )
95- {
96- ArgumentNullException . ThrowIfNull ( policyManager ) ;
55+ if ( policyManager . TryResolvePolicy ( name , apiVersion , out var policy ) )
56+ {
57+ return policy ;
58+ }
59+
60+ return default ;
61+ }
9762
98- if ( ! string . IsNullOrEmpty ( name ) )
63+ /// <summary>
64+ /// Attempts to resolve a policy for the specified name and API version combination.
65+ /// </summary>
66+ /// <param name="name">The name of the API.</param>
67+ /// <param name="apiVersion">The API version to get the policy for.</param>
68+ /// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
69+ /// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
70+ /// <remarks>The resolution order is as follows:
71+ /// <list type="bullet">
72+ /// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
73+ /// <item><paramref name="name"/> only</item>
74+ /// <item><paramref name="apiVersion"/> only</item>
75+ /// </list>
76+ /// </remarks>
77+ public bool TryResolvePolicy ( string ? name , ApiVersion ? apiVersion , [ MaybeNullWhen ( false ) ] out T policy )
9978 {
100- if ( apiVersion != null && policyManager . TryGetPolicy ( name , apiVersion , out policy ) )
79+ ArgumentNullException . ThrowIfNull ( policyManager ) ;
80+
81+ if ( ! string . IsNullOrEmpty ( name ) )
10182 {
102- return true ;
83+ if ( apiVersion != null && policyManager . TryGetPolicy ( name , apiVersion , out policy ) )
84+ {
85+ return true ;
86+ }
87+ else if ( policyManager . TryGetPolicy ( name ! , out policy ) )
88+ {
89+ return true ;
90+ }
10391 }
104- else if ( policyManager . TryGetPolicy ( name ! , out policy ) )
92+
93+ if ( apiVersion != null && policyManager . TryGetPolicy ( apiVersion , out policy ) )
10594 {
10695 return true ;
10796 }
108- }
10997
110- if ( apiVersion != null && policyManager . TryGetPolicy ( apiVersion , out policy ) )
111- {
112- return true ;
98+ policy = default ! ;
99+ return false ;
113100 }
114-
115- policy = default ! ;
116- return false ;
117101 }
118102}
0 commit comments