support decorating with a decorator interface#140
support decorating with a decorator interface#140akayyali wants to merge 1 commit intokhellang:masterfrom
Conversation
khellang
left a comment
There was a problem hiding this comment.
This looks small enough that I'm willing to pull it into the library, but it's missing tests. Would you be able to add some?
| { | ||
| 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)); |
There was a problem hiding this comment.
Isn't typeof(TDecorator).IsGenericType always true here?
There was a problem hiding this comment.
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.
IsGenericType
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.
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
| { | ||
| 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.
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."
support decorating with a decorator interface instead of concrete decorator, this will allow changing decorator concrete class without affecting business services implementation.
Example :