Scrutor icon indicating copy to clipboard operation
Scrutor copied to clipboard

Update to .NET 8 and leverage keyed services for decoration

Open khellang opened this issue 2 years ago • 18 comments

What do you think @DanHarltey?

Closes #171 Closes #208 Closes #65 Closes #200

khellang avatar Aug 18 '23 12:08 khellang

Codecov Report

Attention: 36 lines in your changes are missing coverage. Please review.

Comparison is base (a634e58) 64.71% compared to head (7fcedf7) 66.15%.

Files Patch % Lines
src/Scrutor/TypeSourceSelector.cs 17.64% 14 Missing :warning:
src/Scrutor/ServiceDescriptorExtensions.cs 35.71% 7 Missing and 2 partials :warning:
.../Scrutor/ServiceCollectionExtensions.Decoration.cs 69.56% 5 Missing and 2 partials :warning:
src/Scrutor/LifetimeSelector.cs 0.00% 3 Missing :warning:
src/Scrutor/ClosedTypeDecorationStrategy.cs 83.33% 1 Missing :warning:
src/Scrutor/DecorationStrategy.cs 94.11% 0 Missing and 1 partial :warning:
src/Scrutor/OpenGenericDecorationStrategy.cs 75.00% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #209      +/-   ##
==========================================
+ Coverage   64.71%   66.15%   +1.44%     
==========================================
  Files          25       24       -1     
  Lines        1278     1232      -46     
  Branches        0      112     +112     
==========================================
- Hits          827      815      -12     
+ Misses        451      387      -64     
- Partials        0       30      +30     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 18 '23 12:08 codecov[bot]

Thanks for the review, @DanHarltey! All good feedback 👍🏻

khellang avatar Sep 01 '23 07:09 khellang

Hi @khellang When this will be available at the nuget package? We really need this. 😃

Please, please, please :)

PS We are doing future project but as for now we are using NET8 RC2. Before our release we are switching to full NET8 ofcoz. 😄

dario-l avatar Oct 11 '23 11:10 dario-l

I can probably target RC2 and publish a prerelease version if anyone's eager to test it out.

khellang avatar Oct 11 '23 11:10 khellang

I can probably target RC2 and publish a prerelease version if anyone's eager to test it out.

@khellang Yes please 🙏🏻

dario-l avatar Oct 11 '23 14:10 dario-l

Hi @khellang 😃 I'm just cloned branch for net8 and adopted source code into my project. 😄

Now I can wait for released version of net8 and newest scrutor 😃 I hope that Scan will have appropriate extensions for keyed services also. 👍🏻 Thanks for help. ❤️

dario-l avatar Oct 12 '23 11:10 dario-l

I hope that Scan will have appropriate extensions for keyed services also. 👍🏻

What does that mean? How do you envision using keyed services with Scrutor?

khellang avatar Oct 12 '23 11:10 khellang

I hope that Scan will have appropriate extensions for keyed services also. 👍🏻

What does that mean? How do you envision using keyed services with Scrutor?

I would love to register services from given assembly as keyed with specified key,

For example. I have modular monolith. Each module is a separated project/assembly. I execute services.Scan(...) to register all ICommandHandler from that assembly. I want to register them as keyed services. Thanks to that I can resolve only those services in particular module.

For now I am using this hacky solution 😄

        var tempServices = new ServiceCollection();

        tempServices.Scan(  );
        
        foreach (var service in tempServices)
        {
                // register service as keyed
        }

It would be great to do this something like:

new ServiceCollection()
            .Scan(s => s.FromAssembliesOf(module.GetType())
                .AddClasses(c =>
                {
                    c.AssignableToAny(
                            typeof(ICommandHandler<,>),
                            typeof(ICommandHandler<>),
                            typeof(IQueryHandler<,>),
                            typeof(IEventHandler<>),
                            typeof(IDomainEventHandler<>))
                        .WithoutAttribute<DecoratorAttribute>();
                }, false)
                .AsKeyed(moduleName) //  <========
                .AsImplementedInterfaces()
                .WithScopedLifetime());

dario-l avatar Oct 12 '23 12:10 dario-l

If it's allowed to register multiple services against the same key, it could work. I'll have to verify it.

khellang avatar Oct 12 '23 12:10 khellang

Hi @khellang :) How about this? Will be any chance for this? 😃

dario-l avatar Oct 19 '23 07:10 dario-l

Hi @khellang ?

How about now, after NET8 premiere? 😄

dario-l avatar Nov 15 '23 14:11 dario-l

Still waiting :)

rekosko avatar Jan 12 '24 09:01 rekosko

@rekosko What exactly are you waiting for? The latest version on NuGet works fine on .NET 8, right?

khellang avatar Jan 12 '24 09:01 khellang

@khellang yes I confirm it works on .NET 8 :) But decoration for keyed services is not yet implemented or I missed the purpose of this PR?

rekosko avatar Jan 12 '24 09:01 rekosko

That's not the purpose of this PR, no. Although I want to add that as well before shipping a new version. This PR replaces an old hack to avoid StackOverflowExceptions when decorating services with keyed services under the covers. It also files a few other long-standing issues and pending breaking changes.

khellang avatar Jan 12 '24 09:01 khellang

BTW, if anyone wants to contribute support for decorating keyed services to get a release out sooner, that would be cool.

khellang avatar Jan 12 '24 09:01 khellang

@khellang Can this be merged into master or it's not complete or it needs more testing? Or it's better to base all the tests and work on feature/net8.0 branch?

savornicesei avatar Feb 08 '24 04:02 savornicesei

It's not totally complete and needs more tests. It's better to base work off this branch for now. Once this goes into master, I will release a new major version.

khellang avatar Feb 08 '24 06:02 khellang