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
Modify interface to support HttpClientFactory users #494
Modify interface to support HttpClientFactory users #494
Conversation
…used in an HttpFactory approach while maintaining the advantages of the cache.
@@ -63,7 +62,7 @@ namespace {{Namespace}} | |||
{ | |||
{{#IsRefitMethod}} | |||
var arguments = new object[] { {{ArgumentList}} }; | |||
var func = methodImpls.GetOrAdd({{#MethodTypeParameterNames}}${{/MethodTypeParameterNames}}"{{Name}}{{#MethodTypeParameterNames}}<{{.}}>{{/MethodTypeParameterNames}}({{ArgumentListWithTypes}})", _ => requestBuilder.BuildRestResultFuncForMethod("{{Name}}", new Type[] { {{ArgumentTypesList}} }{{#MethodTypeParameterList}}, new Type[] { {{.}} }{{/MethodTypeParameterList}})); | |||
var func = requestBuilder.BuildRestResultFuncForMethod("{{Name}}", new Type[] { {{ArgumentTypesList}} }{{#MethodTypeParameterList}}, new Type[] { {{.}} }{{/MethodTypeParameterList}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do this....this means we're running potentially expensive reflection code on every method invocation and not caching the result.
I'd need to see a benchmark comparing the two before removing this cache.
I may have spoken a little too soon in my code review comment as I was going top-down and didn't see the new cache. Given the structural changes, can you please describe what you changed and how it's working & caching things? |
Basically, I've made the following changes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me other than a few minor readability things.
return ""; | ||
} | ||
|
||
return parameterTypes.Select(t => t.Name).Aggregate((s1, s2) => s1 + ", " + s2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string.Join(", ", parameterTypes.Select(t => t.Name)
would be a little clearer here. There's another one like this in the next method too.
@@ -27,7 +27,7 @@ partial class RequestBuilderImplementation : IRequestBuilder | |||
readonly ConcurrentDictionary<CloseGenericMethodKey, RestMethodInfo> interfaceGenericHttpMethods; | |||
readonly JsonSerializer serializer; | |||
readonly RefitSettings settings; | |||
readonly Type targetType; | |||
public Type targetType { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a property, change to PascalCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot. Fixing now
Haven't forgotten about this. Will try to get this reviewed and merged in the next few days. |
@thecontrarycat This looks good, thanks! One thing that would be helpful though is a doc update that shows how to use this with |
@onovotny I've added a note to the documentation showing how you can use it with HttpClientFactory |
@thecontrarycat Thanks, that makes sense. In light of the pattern in the docs, what would we need to expose a single extension method (or two with overloads) to add Refit? Something like |
I think it would be complicated to do a single I did start thinking that we could make Refactoring the IRequestHandler was a larger job than I wanted to do unless you think it makes sense. Alternatively, we could static cache the |
…alents, allowing us to provide a generic extension method for HttpClientFactory users.
I did that refactor. |
Refit/HttpClientFactoryExtensions.cs
Outdated
{ | ||
public static class HttpClientFactoryExtensions | ||
{ | ||
public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection services) where T : class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need an overload that can specify the refit settings. Can either be as another parameter or as an Action<RefitSettings>
where the method news it up and passes it to the user for setting?
This looks really great, I love having an easy one-liner to add in the startup that "does the right thing" |
Added the overload for |
Thank for this! |
@thecontrarycat I know I already merged it, but any chance you could add a test or two that covers what the extension method is supposed to do and any tests that cover the new behavior? (new PR) |
PR for #491