-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
support decorating with a decorator interface #140
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,35 @@ public static partial class ServiceCollectionExtensions | |
{ | ||
Preconditions.NotNull(services, nameof(services)); | ||
|
||
return services.DecorateDescriptors(typeof(TService), x => x.Decorate(typeof(TDecorator))); | ||
if (typeof(TDecorator).IsInterface) | ||
{ | ||
return DecorateUsingInterface<TService, TDecorator>(services); | ||
} | ||
else | ||
{ | ||
return services.DecorateDescriptors(typeof(TService), x => x.Decorate(typeof(TDecorator))); | ||
} | ||
} | ||
|
||
private static IServiceCollection DecorateUsingInterface<TService, TDecorator>(IServiceCollection services) where TDecorator : TService | ||
{ | ||
if (typeof(TDecorator).IsGenericType) | ||
{ | ||
var decoratorDescriptor = services.Where(service => HasSameTypeDefinition(service.ServiceType, typeof(TDecorator))).FirstOrDefault(); | ||
if (decoratorDescriptor == null) | ||
throw new MissingTypeRegistrationException(typeof(TDecorator).IsGenericType ? typeof(TDecorator).GetGenericTypeDefinition() : typeof(TDecorator)); | ||
|
||
return services.DecorateDescriptors(typeof(TService), x => x.Decorate(decoratorDescriptor.ImplementationType.MakeGenericType(typeof(TDecorator).GetGenericArguments().First()))); | ||
} | ||
else | ||
{ | ||
var decoratorDescriptor = services.Where(service => service.ServiceType == typeof(TDecorator)).FirstOrDefault(); | ||
if (decoratorDescriptor == null) | ||
throw new MissingTypeRegistrationException(typeof(TDecorator).IsGenericType ? typeof(TDecorator).GetGenericTypeDefinition() : typeof(TDecorator)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...and always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per : https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=net-5.0 |
||
|
||
return services.DecorateDescriptors(typeof(TService), x => x.Decorate(decoratorDescriptor.ImplementationType)); | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
|
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.
Isn't
typeof(TDecorator).IsGenericType
alwaystrue
here?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.
Could you add some braces while you're at it? I like to always use braces to avoid GOTO FAIL-type bugs 😉 https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
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.
not necessary, i`m taking into consideration here the interface could be either generic (IServiceDecorator < T > ) or not
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.
totally, agree :)
thanks for pointing out