-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Using try add for shape attribute provider #12284
Conversation
@@ -29,7 +30,7 @@ public static class ShapeProviderExtensions | |||
{ | |||
public static IServiceCollection AddShapeAttributes<T>(this IServiceCollection services) where T : class, IShapeAttributeProvider | |||
{ | |||
services.AddScoped<T>(); | |||
services.TryAddScoped<T>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any issue with AddScoped()
?
@@ -29,7 +30,7 @@ public static class ShapeProviderExtensions | |||
{ | |||
public static IServiceCollection AddShapeAttributes<T>(this IServiceCollection services) where T : class, IShapeAttributeProvider | |||
{ | |||
services.AddScoped<T>(); | |||
services.TryAddScoped<T>(); | |||
services.AddScoped<IShapeAttributeProvider>(sp => sp.GetService<T>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally in a given module we don't call twice AddShapeAttributes()
for a given shape type.
Otherwise we would need to also use TryAddScoped()
here but it may not work when we pass a factory delegate.
Maybe we could have our own TryAddScoped()
that would be used before line 33 and that would return a bool, so that if it returns false we would not re-register this IShapeAttributeProvider
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it's not issue as it's same class registered more than once and only last one is used. However registration is needed only once.
Useful when having shared shape library not attached to any module , that adds such shapes to service collection, and can be safely called more than once from different modules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant is that we could also prevent to re-register
services.AddScoped<IShapeAttributeProvider>(sp => sp.GetService<T>());
But TryAddScoped()
only checks the service type, here IShapeAttributeProvider
;
So what you could use is TryAddEnumerable()
, was not sure so I looked at the dotnet code (see below), because it also checks the implementation type, anf if it is an implementation factory as here, it also checks the return type of the factory.
services.TryAddEnumerable(
ServiceDescriptor.Scope<IShapeAttributeProvider>(sp => sp.GetRequiredService<T>()));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use TryAddEnumerable
Oops but unit tests are failing
I will check it, maybe just use a cast
|
Okay, should be
I will update it. |
@@ -1,5 +1,6 @@ | |||
using System.Threading.Tasks; | |||
using Microsoft.Extensions.DependencyInjection; | |||
using Microsoft.Extensions.DependencyInjection.Extensions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any perf degradation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If AddShapeAttributes<T>()
is called once per shape type it is as before.
If called multiple times for a given shape type, less registrations, so better for perf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant, does the reflection here makes it slower on an equivalent request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no additional reflection, just a generic extension helper as before.
Using try add for shape attribute provider to add only once