Skip to content

Refactor HttpClientFactoryExtensions to simplify code structure#2042

Merged
ChrisPulman merged 2 commits intoreactiveui:mainfrom
zms9110750:zms9110750
Jan 31, 2026
Merged

Refactor HttpClientFactoryExtensions to simplify code structure#2042
ChrisPulman merged 2 commits intoreactiveui:mainfrom
zms9110750:zms9110750

Conversation

@zms9110750
Copy link
Contributor

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

zms9110750 and others added 2 commits January 7, 2026 05:50
- 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
@ChrisPulman ChrisPulman merged commit af972dd into reactiveui:main Jan 31, 2026
4 checks passed
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants