Repository navigation
Generate DI registrations using implemented interfaces - #84
Open
Denny09310 wants to merge 3 commits into
Open
Denny09310 wants to merge 3 commits into
Denny09310 wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First of all, big thanks to for creating and maintaining this project. This change builds on that foundation to make interface-based service registration a little more convenient.
Motivation
Blazor Web Apps with interactive render modes, particularly Auto and WebAssembly, can require additional boilerplate when a service has different implementations depending on where it executes.
During SSR, the service executes on the server, while in WebAssembly it executes on the client. When using an interface as the abstraction for the service, the server implementation currently has to be registered separately using the appropriate interface-to-implementation mapping.
This change allows that registration to be generated automatically when the endpoint handler implements an interface.
What this enables
Given an interface:
and a server-side implementation:
the generator can now produce:
instead of requiring the concrete implementation to be registered separately:
The generated endpoint handler also resolves the handler through the interface:
This allows the same abstraction to be used by application code while avoiding additional manual DI registration boilerplate on the server.
This is complementary to approaches such as Refit, where a client-side implementation can be generated from an interface. The purpose of this change is not to provide a shared-contract mechanism itself, but to reduce the boilerplate required when an interface-backed service is used in this kind of architecture.
Implementation
The generator now detects whether the endpoint method is implemented through an interface.
When an applicable interface is found, the generated DI registration becomes:
and the generated endpoint handler resolves the service through that interface.
When no applicable interface exists, the existing concrete-type registration behavior is preserved:
Result
This keeps endpoint and DI registration automatic while reducing the amount of manual service-registration boilerplate required for interface-based services in Blazor Web Apps with different render modes.