Skip to content

Conversation

@akayyali
Copy link

@akayyali akayyali commented Feb 22, 2021

support decorating with a decorator interface instead of concrete decorator, this will allow changing decorator concrete class without affecting business services implementation.
Example :

serviceCollection
    .AddTransient<ICommandHandler<CreateCityCommand>, CreateCityCommandHandler>()
    .AddTransient(typeof(ILogCommandHandler<>), typeof(LogCommandHandler<>))
    .AddTransient(typeof(IAuditCommandHandler<>), typeof(PrimaryAuditCommandHandler<>))

    .AddTransient(typeof(IService), typeof(MyService))
    .AddTransient(typeof(IMyServiceDecorator), typeof(AnotherServiceDecorator));

serviceCollection.Decorate<ICommandHandler<CreateCityCommand>, ILogCommandHandler<CreateCityCommand>>();
serviceCollection.Decorate<ICommandHandler<CreateCityCommand>, IAuditCommandHandler<CreateCityCommand>>();
serviceCollection.Decorate<IService, IMyServiceDecorator>();

Copy link
Owner

@khellang khellang left a comment

Choose a reason for hiding this comment

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

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));
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

{
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."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants