1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
using System . Linq ;
5
4
using System . Reflection ;
6
- using System . Text . RegularExpressions ;
7
5
using KY . Core ;
8
- using KY . Core . DataAccess ;
9
6
using KY . Generator . AspDotNet . Configurations ;
10
7
using KY . Generator . Reflection . Language ;
11
8
using KY . Generator . Reflection . Readers ;
@@ -26,116 +23,101 @@ public AspDotNetControllerReader(ReflectionModelReader modelReader)
26
23
public virtual IEnumerable < ITransferObject > Read ( AspDotNetReadConfiguration configuration )
27
24
{
28
25
Logger . Trace ( "Read ASP.net controller..." ) ;
29
- List < Assembly > assemblies = new List < Assembly > ( ) ;
30
- if ( ! string . IsNullOrEmpty ( configuration . Controller . Assembly ) )
26
+ Type type = GeneratorTypeLoader . Get ( configuration , configuration . Controller . Assembly , configuration . Controller . Namespace , configuration . Controller . Name ) ;
27
+ if ( type == null )
31
28
{
32
- //TODO: Support only assembly name too
33
- Assembly assembly = Assembly . LoadFile ( configuration . Controller . Assembly ) ;
34
- assemblies . Add ( assembly ) ;
35
- }
36
- else
37
- {
38
- AppDomain . CurrentDomain . GetAssemblies ( ) . ForEach ( assemblies . Add ) ;
29
+ yield break ;
39
30
}
40
31
41
- foreach ( Assembly assembly in assemblies )
32
+ HttpServiceTransferObject controller = new HttpServiceTransferObject ( ) ;
33
+ controller . Name = type . Name ;
34
+ controller . Language = ReflectionLanguage . Instance ;
35
+
36
+ Attribute routeAttribute = type . GetCustomAttributes ( ) . FirstOrDefault ( ) ;
37
+ controller . Route = routeAttribute ? . GetType ( ) . GetProperty ( "Template" ) ? . GetValue ( routeAttribute ) ? . ToString ( ) ;
38
+
39
+ MethodInfo [ ] methods = type . GetMethods ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . DeclaredOnly ) ;
40
+ foreach ( MethodInfo method in methods )
42
41
{
43
- Type type = assembly . GetType ( string . Join ( "." , configuration . Controller . Namespace , configuration . Controller . Name ) ) ;
44
- if ( type == null )
42
+ foreach ( ModelTransferObject model in this . modelReader . Read ( method . ReturnType ) )
45
43
{
46
- continue ;
44
+ yield return model ;
47
45
}
48
-
49
- HttpServiceTransferObject controller = new HttpServiceTransferObject ( ) ;
50
- controller . Name = type . Name ;
51
- controller . Language = ReflectionLanguage . Instance ;
52
-
53
- Attribute routeAttribute = type . GetCustomAttributes ( ) . FirstOrDefault ( ) ;
54
- controller . Route = routeAttribute ? . GetType ( ) . GetProperty ( "Template" ) ? . GetValue ( routeAttribute ) ? . ToString ( ) ;
55
-
56
- MethodInfo [ ] methods = type . GetMethods ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . DeclaredOnly ) ;
57
- foreach ( MethodInfo method in methods )
46
+ foreach ( Attribute attribute in method . GetCustomAttributes ( ) )
58
47
{
59
- foreach ( ModelTransferObject model in this . modelReader . Read ( method . ReturnType ) )
48
+ Type attributeType = attribute . GetType ( ) ;
49
+ HttpServiceActionTransferObject action = new HttpServiceActionTransferObject ( ) ;
50
+ action . ReturnType = method . ReturnType . ToTransferObject ( ) ;
51
+ action . Route = attributeType . GetProperty ( "Template" ) ? . GetValue ( attribute ) ? . ToString ( ) ;
52
+ int methodNameIndex = 1 ;
53
+ while ( true )
60
54
{
61
- yield return model ;
62
- }
63
- foreach ( Attribute attribute in method . GetCustomAttributes ( ) )
64
- {
65
- Type attributeType = attribute . GetType ( ) ;
66
- HttpServiceActionTransferObject action = new HttpServiceActionTransferObject ( ) ;
67
- action . ReturnType = method . ReturnType . ToTransferObject ( ) ;
68
- action . Route = attributeType . GetProperty ( "Template" ) ? . GetValue ( attribute ) ? . ToString ( ) ;
69
- int methodNameIndex = 1 ;
70
- while ( true )
55
+ string actionName = $ "{ method . Name } { ( methodNameIndex > 1 ? methodNameIndex . ToString ( ) : "" ) } ";
56
+ if ( controller . Actions . All ( x => ! x . Name . Equals ( actionName ) ) )
71
57
{
72
- string actionName = $ "{ method . Name } { ( methodNameIndex > 1 ? methodNameIndex . ToString ( ) : "" ) } ";
73
- if ( controller . Actions . All ( x => ! x . Name . Equals ( actionName ) ) )
74
- {
75
- action . Name = actionName ;
76
- break ;
77
- }
78
- methodNameIndex ++ ;
58
+ action . Name = actionName ;
59
+ break ;
79
60
}
80
- ParameterInfo [ ] parameters = method . GetParameters ( ) ;
81
- foreach ( ParameterInfo parameter in parameters )
61
+ methodNameIndex ++ ;
62
+ }
63
+ ParameterInfo [ ] parameters = method . GetParameters ( ) ;
64
+ foreach ( ParameterInfo parameter in parameters )
65
+ {
66
+ foreach ( ModelTransferObject model in this . modelReader . Read ( parameter . ParameterType ) )
82
67
{
83
- foreach ( ModelTransferObject model in this . modelReader . Read ( parameter . ParameterType ) )
84
- {
85
- yield return model ;
86
- }
68
+ yield return model ;
87
69
}
88
- switch ( attributeType . Name )
70
+ }
71
+ switch ( attributeType . Name )
72
+ {
73
+ case "HttpGetAttribute" :
74
+ action . Type = HttpServiceActionTypeTransferObject . Get ;
75
+ break ;
76
+ case "HttpPostAttribute" :
77
+ action . Type = HttpServiceActionTypeTransferObject . Post ;
78
+ break ;
79
+ case "HttpPatchAttribute" :
80
+ action . Type = HttpServiceActionTypeTransferObject . Patch ;
81
+ break ;
82
+ case "HttpPutAttribute" :
83
+ action . Type = HttpServiceActionTypeTransferObject . Put ;
84
+ break ;
85
+ case "HttpDeleteAttribute" :
86
+ action . Type = HttpServiceActionTypeTransferObject . Delete ;
87
+ break ;
88
+ default :
89
+ Logger . Warning ( $ "Unknown controller action attribute { attributeType . Name } ") ;
90
+ continue ;
91
+ }
92
+ action . RequireBodyParameter = action . Type . IsBodyParameterRequired ( ) ;
93
+ foreach ( ParameterInfo parameter in parameters )
94
+ {
95
+ HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject ( ) ;
96
+ actionParameter . Name = parameter . Name ;
97
+ actionParameter . Type = parameter . ParameterType . ToTransferObject ( ) ;
98
+ actionParameter . FromBody = action . RequireBodyParameter && parameter . GetCustomAttributes ( ) . Any ( parameterAttribute => parameterAttribute . GetType ( ) . Name == "FromBodyAttribute" ) ;
99
+ action . Parameters . Add ( actionParameter ) ;
100
+ }
101
+ if ( action . RequireBodyParameter )
102
+ {
103
+ if ( action . Parameters . Count == 0 )
89
104
{
90
- case "HttpGetAttribute" :
91
- action . Type = HttpServiceActionTypeTransferObject . Get ;
92
- break ;
93
- case "HttpPostAttribute" :
94
- action . Type = HttpServiceActionTypeTransferObject . Post ;
95
- break ;
96
- case "HttpPatchAttribute" :
97
- action . Type = HttpServiceActionTypeTransferObject . Patch ;
98
- break ;
99
- case "HttpPutAttribute" :
100
- action . Type = HttpServiceActionTypeTransferObject . Put ;
101
- break ;
102
- case "HttpDeleteAttribute" :
103
- action . Type = HttpServiceActionTypeTransferObject . Delete ;
104
- break ;
105
- default :
106
- Logger . Warning ( $ "Unknown controller action attribute { attributeType . Name } ") ;
107
- continue ;
105
+ throw new InvalidOperationException ( $ "Can not write { method . Name } . { action . Type } requires at least one parameter, but no parameter found.") ;
108
106
}
109
- action . RequireBodyParameter = action . Type . IsBodyParameterRequired ( ) ;
110
- foreach ( ParameterInfo parameter in parameters )
107
+ if ( action . Parameters . Count == 1 )
111
108
{
112
- HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject ( ) ;
113
- actionParameter . Name = parameter . Name ;
114
- actionParameter . Type = parameter . ParameterType . ToTransferObject ( ) ;
115
- actionParameter . FromBody = action . RequireBodyParameter && parameter . GetCustomAttributes ( ) . Any ( parameterAttribute => parameterAttribute . GetType ( ) . Name == "FromBodyAttribute" ) ;
116
- action . Parameters . Add ( actionParameter ) ;
109
+ action . Parameters . Single ( ) . FromBody = true ;
117
110
}
118
- if ( action . RequireBodyParameter )
111
+ else if ( action . Parameters . All ( x => ! x . FromBody ) )
119
112
{
120
- if ( action . Parameters . Count == 0 )
121
- {
122
- throw new InvalidOperationException ( $ "Can not write { method . Name } . { action . Type } requires at least one parameter, but no parameter found.") ;
123
- }
124
- if ( action . Parameters . Count == 1 )
125
- {
126
- action . Parameters . Single ( ) . FromBody = true ;
127
- }
128
- else if ( action . Parameters . All ( x => ! x . FromBody ) )
129
- {
130
- throw new InvalidOperationException ( $ "Can not write { method . Name } . { action . Type } requires at least one parameter marked with [FromBody] or can have only one parameter") ;
131
- }
113
+ throw new InvalidOperationException ( $ "Can not write { method . Name } . { action . Type } requires at least one parameter marked with [FromBody] or can have only one parameter") ;
132
114
}
133
- controller . Actions . Add ( action ) ;
134
115
}
116
+ controller . Actions . Add ( action ) ;
135
117
}
136
-
137
- yield return controller ;
138
118
}
119
+
120
+ yield return controller ;
139
121
}
140
122
}
141
123
}
0 commit comments