Skip to content

Conversation

@zms9110750
Copy link

What kind of change does this PR introduce?

  • Feature enhancement: Add extension methods for IHttpClientBuilder to simplify Refit client configuration
  • Code refactoring: Extract duplicated extension method implementations into 4 core methods
  • Tool improvement: Use T4 template to auto-generate all 16 overload variants

What is the current behavior?
Currently, when configuring Refit clients, developers must manually specify the HTTP client configuration name as a string, which is error-prone and difficult to maintain. For example:

var builder = services.AddHttpClient("MyApiClient");
// ... other configurations ...
services.AddRefitClient<IMyApi>(settings, "MyApiClient");  // Need to repeat string parameter

Additionally, existing extension method implementations have significant code duplication, making maintenance difficult.

What is the new behavior?

  1. Add IHttpClientBuilder extension methods that automatically retrieve the HttpClient name from builder.Name
  2. Cleaner and safer usage:
services.AddHttpClient("MyApiClient")
    .ConfigurePrimaryHttpMessageHandler(...)
    .AddPolicyHandler(...)
    .AddRefitClient<IMyApi>(settings);  // Automatically uses "MyApiClient"
  1. Generate 16 complete API variants via T4 template
  2. Extract duplicate logic into 4 core private methods

What might this PR break?
This change is API-compatible but binary-incompatible:

  • API compatible: All existing code continues to work without modification

  • Binary incompatible: Applications referencing this library need recompilation due to some required parameters becoming optional

  • Parameter changes:

    // Before: required parameters
    AddRefitClient(Type refitInterfaceType, RefitSettings settings)
    // After: optional parameters
    AddRefitClient(Type refitInterfaceType, RefitSettings? settings = null)
  • Impact on existing code:

    • ✅ Source compatible: No code changes needed
    • ⚠️ Recompilation required: Due to method signature changes
    • ✅ Functionality unchanged: All features remain identical

Other information:

Technical implementation:

  1. Added 4 core private methods:

    • AddRefitClientCore (non-generic, non-keyed)
    • AddKeyedRefitClientCore (non-generic, keyed)
    • AddRefitClientCore<T> (generic, non-keyed)
    • AddKeyedRefitClientCore<T> (generic, keyed)
  2. T4 template generates 16 variants covering:

    • Keyed/Non-Keyed
    • Generic/Non-Generic
    • Builder/ServiceCollection
    • Settings/SettingsAction
  3. Improved error handling:

    • Unified parameter validation in core methods
    • Clearer error messages

- Extract duplicated extension method implementations into 4 core methods
- Use T4 template to auto-generate all 16 overload variants

Note: This change is API-compatible but binary-incompatible:
- All method parameters now use optional parameter syntax
- No code changes required for existing usage
- But applications referencing this library need to be recompiled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant