@@ -13,59 +13,74 @@ public sealed partial class ControllerHandler : IHandler, IServiceMethodProvider
1313{
1414 private static readonly Regex HyphenMatcher = CreateHyphenMatcher ( ) ;
1515
16+ private MethodCollection ? _Methods ;
17+
1618 #region Get-/Setters
1719
18- public MethodCollection Methods { get ; }
20+ private Type Type { get ; }
1921
20- private ResponseProvider ResponseProvider { get ; }
22+ private Func < IRequest , ValueTask < object > > InstanceProvider { get ; }
2123
2224 private MethodRegistry Registry { get ; }
2325
24- private object Instance { get ; }
25-
2626 #endregion
2727
2828 #region Initialization
2929
30- public ControllerHandler ( object instance , MethodRegistry registry )
30+ public ControllerHandler ( Type type , Func < IRequest , ValueTask < object > > instanceProvider , MethodRegistry registry )
3131 {
32+ Type = type ;
33+ InstanceProvider = instanceProvider ;
3234 Registry = registry ;
35+ }
36+
37+ #endregion
3338
34- Instance = instance ;
39+ #region Functionality
3540
36- ResponseProvider = new ResponseProvider ( registry ) ;
41+ public ValueTask PrepareAsync ( ) => ValueTask . CompletedTask ;
3742
38- Methods = new MethodCollection ( AnalyzeMethods ( instance . GetType ( ) , registry ) ) ;
39- }
43+ public async ValueTask < IResponse ? > HandleAsync ( IRequest request ) => await ( await GetMethodsAsync ( request ) ) . HandleAsync ( request ) ;
4044
41- private IEnumerable < MethodHandler > AnalyzeMethods ( Type type , MethodRegistry registry )
45+ public async ValueTask < MethodCollection > GetMethodsAsync ( IRequest request )
4246 {
43- foreach ( var method in type . GetMethods ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
47+ if ( _Methods != null ) return _Methods ;
48+
49+ var found = new List < MethodHandler > ( ) ;
50+
51+
52+ foreach ( var method in Type . GetMethods ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
4453 {
4554 var annotation = method . GetCustomAttribute < ControllerActionAttribute > ( true ) ?? new MethodAttribute ( ) ;
4655
4756 var arguments = FindPathArguments ( method ) ;
4857
49- var operation = CreateOperation ( method , arguments ) ;
58+ var operation = CreateOperation ( request , method , arguments , Registry ) ;
5059
51- yield return new MethodHandler ( operation , Instance , annotation , registry ) ;
60+ found . Add ( new MethodHandler ( operation , InstanceProvider , annotation , Registry ) ) ;
5261 }
62+
63+ var result = new MethodCollection ( found ) ;
64+
65+ await result . PrepareAsync ( ) ;
66+
67+ return _Methods = result ;
5368 }
5469
55- private Operation CreateOperation ( MethodInfo method , List < string > arguments )
70+ private static Operation CreateOperation ( IRequest request , MethodInfo method , List < string > arguments , MethodRegistry registry )
5671 {
5772 var pathArguments = string . Join ( '/' , arguments . Select ( a => $ ":{ a } ") ) ;
5873
5974 if ( method . Name == "Index" )
6075 {
61- return OperationBuilder . Create ( pathArguments . Length > 0 ? $ "/{ pathArguments } /" : null , method , Registry , true ) ;
76+ return OperationBuilder . Create ( request , pathArguments . Length > 0 ? $ "/{ pathArguments } /" : null , method , registry , true ) ;
6277 }
6378
6479 var name = HypenCase ( method . Name ) ;
6580
6681 var path = $ "/{ name } ";
6782
68- return OperationBuilder . Create ( pathArguments . Length > 0 ? $ "{ path } /{ pathArguments } /" : $ "{ path } /", method , Registry , true ) ;
83+ return OperationBuilder . Create ( request , pathArguments . Length > 0 ? $ "{ path } /{ pathArguments } /" : $ "{ path } /", method , registry , true ) ;
6984 }
7085
7186 private static List < string > FindPathArguments ( MethodInfo method )
@@ -93,12 +108,4 @@ private static List<string> FindPathArguments(MethodInfo method)
93108
94109 #endregion
95110
96- #region Functionality
97-
98- public ValueTask PrepareAsync ( ) => Methods . PrepareAsync ( ) ;
99-
100- public ValueTask < IResponse ? > HandleAsync ( IRequest request ) => Methods . HandleAsync ( request ) ;
101-
102- #endregion
103-
104111}
0 commit comments