Allow Blazor components injection
See original GitHub issueI think we should be able to abstract components into interfaces and inject them into other components using DI, the same way we inject other dependencies. Something like this:
TextBox.razor
@inject ILabel Label
@inject IInput Input
<div class="textbox">
<Label Text="..."/>
<Input Value="..." />
</div>
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILabel, MyLabelImplementation>();
services.AddSingleton<IInput, MyInputImplementation>();
}
I know that it’s already possible to render the injected instance of IComponent
using RenderTreeBuilder
, but the syntax required to do that is incredibly verbose. It would be so much cooler if we could simply use the injected components with the standard syntax we use for any other component.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:11 (6 by maintainers)
Top Results From Across the Web
ASP.NET Core Blazor dependency injection
This article explains how Blazor apps can inject services into components. Dependency injection (DI) is a technique for accessing services ...
Read more >Injecting dependencies into Blazor components
Dependencies are injected after the Blazor component instance has been created and before the OnInitialized or OnInitializedAsync lifecycle events are executed.
Read more >Dependency Injection In Blazor Component file
I am trying to use DI in my app , for example IHostingEnvironment. Code give no compile time error here but when i...
Read more >How to Inject HTML in a Blazor Component
The HTML code we're going to inject is an iFrame that will play a video from Twitch.tv. Let's get to work! Create a...
Read more >How do you add dependency injection in Blazor?
Use the @inject directive to inject the service into components. @inject has two parameters (type and name). Type represents the service type and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We’ve discussed this within the team and we don’t think we’ll implement this in the core framework, given that it has issues with linking and that this can be built on top of the framework if needed.
@javiercn imagine that over time you start building your own library of reusable components, and some of these components internally use other components. For instance: you build a
LoginForm
component that internally uses a customInput
component. SuchLoginForm
component will be used many times across different projects, and in some occasions you might want to customize the way the internalInput
component looks and behaves without having to re-write the entireLoginForm
component.Now, in my opinion, the most obvious way to do so would be to treat the
Input
component as a dependency ofLoginForm
, and to inject such dependency in the DI container. That way, when you want to swap their implementation to achieve a different result, you would simply do:Since abstraction is a basic design principle when it comes to services, I don’t see why we shouldn’t use it for UI components… And again, what I’m asking can already be achieved, the only issue is that the syntax is incredibly verbose:
So I’m saying, instead of resorting to using
RenderTreeBuilder
, it would be great if this was an accepted Blazor syntax: