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

Introduce ExposeKeyedServiceAttribute. #18819

Merged
merged 3 commits into from
Jan 22, 2024
Merged

Conversation

maliming
Copy link
Member

@maliming maliming commented Jan 21, 2024

Resolves #18794


ExposeKeyedService Attribute

ExposeKeyedServiceAttribute is used to control which keyed services are provided by the related class. Example:

[ExposeKeyedService<ITaxCalculator>("taxCalculator")]
[ExposeKeyedService<ICalculator>("calculator")]
public class TaxCalculator: ICalculator, ITaxCalculator, ICanCalculate, ITransientDependency
{
}

TaxCalculator class exposes ITaxCalculator interface with the key tax and ICalculator interface with the key calculator. That means you can get keyed services from the IServiceProvider as shown below:

var taxCalculator = ServiceProvider.GetRequiredKeyedService<ITaxCalculator>("taxCalculator");
var calculator = ServiceProvider.GetRequiredKeyedService<ICalculator>("calculator");

Notice that the ExposeKeyedServiceAttribute is only expose keyed services. So, you can not inject ITaxCalculator or ICalculator in your application. If you want to expose both keyed and non-keyed services, you can use ExposeServicesAttribute and ExposeKeyedServiceAttribute together as shown below:

[ExposeKeyedService<ITaxCalculator>("taxCalculator")]
[ExposeKeyedService<ICalculator>("calculator")]
[ExposeServices(typeof(ITaxCalculator), typeof(ICalculator))]
public class TaxCalculator: ICalculator, ITaxCalculator, ICanCalculate, ITransientDependency
{
}

This is a small breaking change because IOnServiceExposingContext has changed.

image

@maliming maliming added this to the 8.1-preview milestone Jan 21, 2024
@maliming maliming marked this pull request as ready for review January 21, 2024 12:11
Copy link

codecov bot commented Jan 21, 2024

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (98c5dec) 51.61% compared to head (2218e9d) 51.61%.
Report is 484 commits behind head on dev.

❗ Current head 2218e9d differs from pull request most recent head 0e0c461. Consider uploading reports for the commit 0e0c461 to get more accurate results

Files Patch % Lines
...bp/DependencyInjection/OnServiceExposingContext.cs 33.33% 4 Missing ⚠️
...p/DependencyInjection/ConventionalRegistrarBase.cs 91.42% 3 Missing ⚠️
...DependencyInjection/ExposeKeyedServiceAttribute.cs 72.72% 2 Missing and 1 partial ⚠️
...ependencyInjection/DefaultConventionalRegistrar.cs 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##              dev   #18819    +/-   ##
========================================
  Coverage   51.61%   51.61%            
========================================
  Files        3077     3083     +6     
  Lines       97266    97789   +523     
  Branches     7745     7806    +61     
========================================
+ Hits        50202    50476   +274     
- Misses      45521    45761   +240     
- Partials     1543     1552     +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maliming maliming changed the title Introduce ExposeKeyedServicesAttribute. Introduce ExposeKeyedServiceAttribute. Jan 22, 2024
@EngincanV EngincanV merged commit 206f1ba into dev Jan 22, 2024
3 checks passed
@EngincanV EngincanV deleted the ExposeKeyedServicesAttribute branch January 22, 2024 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce ExposeKeyedServiceAttribute to automatically register multiple services by keys.
2 participants