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

Added open generic support for decorator func #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Kamil-Zakiev
Copy link

Hi Kristian!
I have added support for decorating open generic type by decorator func.
This change allows me to initialize component after being resolved by ServiceProvider.
I added test CanDecorateOpenGenericTypeBasedOnInterfaceByDecoratorFunc that may clarify my intensions.

It would be great if this could be added to your library. Thank you!

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.

Sorry for taking so long to respond 😞 I think the change looks pretty good. My only concern is the recent change in logic (introduced in #76) and reusing code between the different paths. See comment below.

@@ -261,6 +276,27 @@ bool TryDecorate(Type[] typeArguments)
return arguments.Aggregate(true, (result, args) => result && TryDecorate(args));
}

private static bool TryDecorateOpenGeneric(this IServiceCollection services, Type serviceType, Func<object, IServiceProvider, object> decorator)
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm... I'm not a big fan of the duplication here. I just merge #76, which made a change to the argument filter below:

- .Where(descriptor => descriptor.ServiceType.IsAssignableTo(serviceType))
+ .Where(descriptor => IsSameGenericType(descriptor.ServiceType, serviceType))

Ideally, this should be changed here as well. Are you able to find a way to share more of this code?

Copy link
Author

Choose a reason for hiding this comment

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

Removed duplication by refining existed TryDecorateOpenGeneric method.
Take a look, please.

Copy link
Author

Choose a reason for hiding this comment

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

Refactored my implementation, simplified TryDecorateOpenGeneric.
Should look good now.

P.S. locally I have successful build, but AppVeyor fails with "unable to access 'https://github.com/khellang/Scrutor.git/': Could not resolve host: github.com"

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.

None yet

2 participants