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

support decorating with a decorator interface #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Scrutor/ServiceCollectionExtensions.Decoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Owner

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 always true here?

Copy link
Owner

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/

Copy link
Author

@akayyali akayyali Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsGenericType

not necessary, i`m taking into consideration here the interface could be either generic (IServiceDecorator < T > ) or not

Copy link
Author

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/

totally, agree :)
thanks for pointing out


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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and always false here? 🤔

Copy link
Author

Choose a reason for hiding this comment

The 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
"The default value for reference and nullable types is null."


return services.DecorateDescriptors(typeof(TService), x => x.Decorate(decoratorDescriptor.ImplementationType));
}

}

/// <summary>
Expand Down