-
Notifications
You must be signed in to change notification settings - Fork 709
Consider to add options.Conventions.Controller(Type controllerType) method #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Reasonable request and something I considered in the original design. I'm willing to discuss how this would work. The biggest challenge I see is defining how to map API version versions to specific actions. This, more than anything, is the reason why just providing type information was not included as an option. For example, the compile-time typed version looks like: var controller = options.Conventions.Controller<ValuesController>();
controller.HasApiVersion( 1, 0 )
.HasApiVersion( 2, 0 )
.Action( c => c.GetV2() ).MapToApiVersion( 2, 0 ); Using the run-time types, how would this work? var controller = options.Conventions.Controller( typeof( ValuesController ) );
controller.HasApiVersion( 1, 0 )
.HasApiVersion( 2, 0 );
// TODO: how can be define a lambda here since the type is not known at compile-time?
// .Action( c => c.GetV2() ).MapToApiVersion( 2, 0 );
// Maybe this would work?
// .Action( "GetV2" ).MapToApiVersion( 2, 0 ); If you can articulate a use case about how you would use this, that might help drive the feature. While limiting, perhaps version interleaving cannot (and should not) be supported by this method of convention. |
Actions will not be type safe in that case. We can just pass MethodInfo (I suppose it's already possible in the code).
|
Sounds reasonable. I'll re-evaluate the design and see if it's not possible to make this work. The scenario you described is one of the reasons to support conventions in the first place (since you might not be able to apply attributes - say if it were 3rd party). I'll bounce some things off of you once I have something to look at. |
Thanks a lot for your consideration. For now, I'm using reflection, something like that: foreach (var controllerType in setting.ControllerTypes)
{
var controllerBuilder = typeof(ApiVersionConventionBuilder)
.GetMethod(nameof(ApiVersionConventionBuilder.Controller), BindingFlags.Instance | BindingFlags.Public)
.MakeGenericMethod(controllerType)
.Invoke(options.Conventions, null);
typeof(ControllerApiVersionConventionBuilder<>)
.MakeGenericType(controllerType)
.GetMethod("HasApiVersion")
.Invoke(controllerBuilder, new object[] { setting.ApiVersion });
} |
@hikalkan have a look at PR #199 whenever you have a minute. This should be what you're looking for. Per our previous discussion, the expanded support will look like: var controller = options.Conventions.Controller( typeof( ValuesController ) );
controller.HasApiVersion( 1, 0 )
.HasApiVersion( 2, 0 );
.Action( "GetV2" ).MapToApiVersion( 2, 0 ); Action method binding is really the only tricky and loosey-goosey part. The matching rules will be as follows:
As you mentioned, there is already a way to map actions using MethodInfo as that is how the underlying mapping is performed. In the compile-time builder path, this method is intentionally hidden from Intellisense (but callable). In the new run-time builder path, this method will be visible as expected. One final design note is the builders are not substitutable. This means that you cannot use both I'll probably commit and potentially publish by day's end tomorrow. If you don't get a chance to review, no big deal. A second pair of eyes is always nice though. If there's any other relevant feedback or suggestions, I'll consider integrating them. |
Thank you very much @commonsensesoftware for your great work. |
Consider to add
options.Conventions.Controller(Type controllerType)
method forApiVersionConventionBuilder
. Thus, we can define conventions for controllers where we can only resolve their type on runtime.Surely, we can use reflection (I will do it now), but such an option would be better.
The text was updated successfully, but these errors were encountered: