1+ #if WEBAPI
2+ namespace Microsoft . Web . Http . Versioning . Conventions
3+ #else
4+ namespace Microsoft . AspNetCore . Mvc . Versioning . Conventions
5+ #endif
6+ {
7+ using System ;
8+ using System . Collections ;
9+ using System . Collections . Generic ;
10+ using System . Diagnostics . Contracts ;
11+ using System . Linq ;
12+ using System . Reflection ;
13+
14+ /// <summary>
15+ /// Represents a collection of controller action convention builders.
16+ /// </summary>
17+ #if ! WEBAPI
18+ [ CLSCompliant ( false ) ]
19+ #endif
20+ public class ActionApiVersionConventionBuilderCollection : IReadOnlyCollection < ActionApiVersionConventionBuilder >
21+ {
22+ readonly ControllerApiVersionConventionBuilder controllerBuilder ;
23+ readonly IList < ActionBuilderMapping > actionBuilderMappings = new List < ActionBuilderMapping > ( ) ;
24+
25+ /// <summary>
26+ /// Initializes a new instance of the <see cref="ActionApiVersionConventionBuilderCollection"/> class.
27+ /// </summary>
28+ /// <param name="controllerBuilder">The associated <see cref="ControllerApiVersionConventionBuilder">controller convention builder</see>.</param>
29+ public ActionApiVersionConventionBuilderCollection ( ControllerApiVersionConventionBuilder controllerBuilder )
30+ {
31+ Arg . NotNull ( controllerBuilder , nameof ( controllerBuilder ) ) ;
32+ this . controllerBuilder = controllerBuilder ;
33+ }
34+
35+ /// <summary>
36+ /// Gets or adds a controller action convention builder for the specified method.
37+ /// </summary>
38+ /// <param name="actionMethod">The controller action method to get or add the convention builder for.</param>
39+ /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder">controller action convention builder</see>.</returns>
40+ protected internal virtual ActionApiVersionConventionBuilder GetOrAdd ( MethodInfo actionMethod )
41+ {
42+ Arg . NotNull ( actionMethod , nameof ( actionMethod ) ) ;
43+
44+ var mapping = actionBuilderMappings . FirstOrDefault ( m => m . Method == actionMethod ) ;
45+
46+ if ( mapping == null )
47+ {
48+ mapping = new ActionBuilderMapping ( actionMethod , new ActionApiVersionConventionBuilder ( controllerBuilder ) ) ;
49+ actionBuilderMappings . Add ( mapping ) ;
50+ }
51+
52+ return mapping . Builder ;
53+ }
54+
55+ /// <summary>
56+ /// Gets a count of the controller action convention builders in the collection.
57+ /// </summary>
58+ /// <value>The total number of controller action convention builders in the collection.</value>
59+ public virtual int Count => actionBuilderMappings . Count ;
60+
61+ /// <summary>
62+ /// Attempts to retrieve the controller action convention builder for the specified method.
63+ /// </summary>
64+ /// <param name="actionMethod">The controller action method to get the convention builder for.</param>
65+ /// <param name="actionBuilder">The <see cref="ActionApiVersionConventionBuilder">controller action convention builder</see> or <c>null</c>.</param>
66+ /// <returns>True if the <paramref name="actionBuilder">action builder</paramref> is successfully retrieved; otherwise, false.</returns>
67+ public virtual bool TryGetValue ( MethodInfo actionMethod , out ActionApiVersionConventionBuilder actionBuilder )
68+ {
69+ actionBuilder = null ;
70+
71+ if ( actionMethod == null )
72+ {
73+ return false ;
74+ }
75+
76+ var mapping = actionBuilderMappings . FirstOrDefault ( m => m . Method == actionMethod ) ;
77+
78+ return ( actionBuilder = mapping ? . Builder ) != null ;
79+ }
80+
81+ /// <summary>
82+ /// Returns an iterator that enumerates the controller action convention builders in the collection.
83+ /// </summary>
84+ /// <returns>An <see cref="IEnumerator"/> object.</returns>
85+ public virtual IEnumerator < ActionApiVersionConventionBuilder > GetEnumerator ( )
86+ {
87+ foreach ( var mapping in actionBuilderMappings )
88+ {
89+ yield return mapping . Builder ;
90+ }
91+ }
92+
93+ IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
94+
95+ sealed partial class ActionBuilderMapping
96+ {
97+ internal ActionBuilderMapping ( MethodInfo method , ActionApiVersionConventionBuilder builder )
98+ {
99+ Contract . Requires ( method != null ) ;
100+ Contract . Requires ( builder != null ) ;
101+
102+ Method = method ;
103+ Builder = builder ;
104+ }
105+
106+ internal MethodInfo Method { get ; }
107+
108+ internal ActionApiVersionConventionBuilder Builder { get ; }
109+ }
110+ }
111+ }
0 commit comments