1+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+ namespace Asp . Versioning ;
4+
5+ /// <summary>
6+ /// Provides extension methods for the <see cref="IPolicyManager{T}"/> interface.
7+ /// </summary>
8+ public static class IPolicyManagerExtensions
9+ {
10+ /// <summary>
11+ /// Returns the policy for the specified API and version.
12+ /// </summary>
13+ /// <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 )
22+ {
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 ) ;
65+
66+ if ( policyManager . TryResolvePolicy ( name , apiVersion , out var policy ) )
67+ {
68+ return policy ;
69+ }
70+
71+ return default ;
72+ }
73+
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 ) ;
97+
98+ if ( ! string . IsNullOrEmpty ( name ) )
99+ {
100+ if ( apiVersion != null && policyManager . TryGetPolicy ( name , apiVersion , out policy ) )
101+ {
102+ return true ;
103+ }
104+ else if ( policyManager . TryGetPolicy ( name ! , out policy ) )
105+ {
106+ return true ;
107+ }
108+ }
109+
110+ if ( apiVersion != null && policyManager . TryGetPolicy ( apiVersion , out policy ) )
111+ {
112+ return true ;
113+ }
114+
115+ policy = default ! ;
116+ return false ;
117+ }
118+ }
0 commit comments