Can we talk about constructor injection now that partial classes are here? #18088
Comments
Thanks for your issue report. We'll consider whether to address this during our next milestone planning. |
Thanks for that |
This is a must have. Property based injection prevents us from consuming them within the constructor there by preventing us from making some (other) fields read only. |
Comment from a duplicate issue I opened: Currently blazor only allows injecting services into components using property injection via the However this has a number of disadvantages over constructor injection
ProposalIf there is a single constructor, use it for dependency injection. It is an error if any of the parameters cannot be resolved. It is an error if there are multiple constructors, none of which are parameterless, unless one of them is marked with |
|
Any updates on this? @pranavkm |
Moving to backlog given this now be achieved using |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
@mkArtakMSFT how can it be achieved using IComponentActivator? Could you please give an example? |
As I can see, So, you can just implement the Then you need to register your components to the DI. |
The implementation can be like that: using System;
using Microsoft.AspNetCore.Components;
namespace MyProject
{
public class ServiceProviderComponentActivator : IComponentActivator
{
public IServiceProvider ServiceProvider { get; }
public ServiceProviderComponentActivator(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public IComponent CreateInstance(Type componentType)
{
var instance = ServiceProvider.GetService(componentType);
if (instance == null)
{
instance = Activator.CreateInstance(componentType);
}
if (!(instance is IComponent component))
{
throw new ArgumentException($"The type {componentType.FullName} does not implement {nameof(IComponent)}.", nameof(componentType));
}
return component;
}
}
} Then replace the services.Replace(ServiceDescriptor.Transient<IComponentActivator, ServiceProviderComponentActivator>()); @mkArtakMSFT is there any problem with this implementation? Does it cause a memory leak (I don't know how the IServiceProvider scope is managed for this case)? |
Keep in mind that this only works in .net 5. I wish this was the done by default. But this will do it for me. |
Constructor DI should be default in Blazor and it can be a very good feature for 6.0.0 |
I have created a library called Blazor Server: using TanvirArjel.Blazor.DependencyInjection;
services.AddComponents(); Blazor Web Assembly: using TanvirArjel.Blazor.DependencyInjection;
builder.Services.AddComponents(); Install the PMC:
.NET CLI:
|
Is your feature request related to a problem? Please describe.
Constructor injection in Components was ruled out previously as "we don't plan to do this" but now that we have partials is there any change in this attitude?
see #15779 , #5497
Describe the solution you'd like
If Blazor could be modified to optionally use DI to source components, then constructor injection would be possible,
If you would be open to at least reviewing a PR, I'm sure the community would do the groundwork to make it happen.
The text was updated successfully, but these errors were encountered: