Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @inject to support keyed services #9286

Open
javiercn opened this issue Sep 14, 2023 · 4 comments
Open

Update @inject to support keyed services #9286

javiercn opened this issue Sep 14, 2023 · 4 comments
Labels
area-compiler Umbrella for all compiler issues feature request Large improvement request
Milestone

Comments

@javiercn
Copy link
Member

We added support for keyed services on the runtime as part of dotnet/aspnetcore#50561. We should consider what syntax we want to use when this is exposed through the @inject directive.

Some options are:

  • @inject Key Type PropertyName.
  • @inject Type Key PropertyName.
  • @inject Type(Key) PropertyName.

This would be for Blazor and Razor pages, since both added support for it.

@javiercn
Copy link
Member Author

/cc @SteveSanderson @MackinnonBuck

@phil-allen-msft phil-allen-msft added the area-compiler Umbrella for all compiler issues label Sep 21, 2023
@ebekker
Copy link

ebekker commented Oct 10, 2023

@inject Type("Key") PropertyName
@* or *@
@inject Type["Key"] PropertyName

But either way I think the key needs to be quoted to distinguish a literal string from a reference to a const, e.g.:

@inject Type(Global.ServiceKey) PropertyName
@* or *@
@inject Type[Global.ServiceKey] PropertyName
static class Global
{
    public const string ServiceKey = "Key";
}

@chsienki chsienki added the feature request Large improvement request label Oct 31, 2023
@chsienki chsienki added this to the Backlog milestone Oct 31, 2023
@ghost ghost removed the untriaged label Oct 31, 2023
@burkenyo
Copy link

burkenyo commented Nov 3, 2023

Hi there, will this feature make it into .NET 8?

@david-at-solve
Copy link

Or maybe something like

@inject(Key = "MyKey") MyType MyName

which alignes more to the traditional

[Inject(Key = "MyKey")] private MyType MyName { get; set; }

?

Or as a general alternative it could be helpful to attach all kinds of attributes to the generated property, which could also be non-key-related ones. This could then look like this:
@inject [Key("MyKey"), OtherAttribute] MyType MyPropName

Of course you would have to rethink the name of the "key identifying attribute".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues feature request Large improvement request
Projects
None yet
Development

No branches or pull requests

6 participants