From 13f2977c35dfd587228ca8d2d2bce3dc93df99c5 Mon Sep 17 00:00:00 2001 From: Mateusz Marciniak Date: Tue, 14 Jan 2025 13:54:56 +0100 Subject: [PATCH] feat: fix AddClasses usages to explicitly pass publicOnly parameter; add support for publicOnly parameter in this library --- .../StartupExtensions.cs | 12 +++++++++--- .../Commands/StartupExtensions.cs | 14 ++++++++++---- .../Queries/StartupExtensions.cs | 11 +++++++---- src/Allegro.Extensions.Cqrs/version.xml | 2 +- .../StartupExtensions.cs | 9 +++++++-- src/Allegro.Extensions.DependencyCall/version.xml | 2 +- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs.FluentValidations/StartupExtensions.cs b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs.FluentValidations/StartupExtensions.cs index 5dc36ab..7379483 100644 --- a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs.FluentValidations/StartupExtensions.cs +++ b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs.FluentValidations/StartupExtensions.cs @@ -16,16 +16,22 @@ public static class StartupExtensions /// /// /// Assembly collection in which IValidators should be looked for. - public static IServiceCollection AddCqrsFluentValidations(this IServiceCollection services, IEnumerable assemblies) + /// Determines whether only public classes should be registered + public static IServiceCollection AddCqrsFluentValidations( + this IServiceCollection services, + IEnumerable assemblies, + bool publicOnly = false) { services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(IValidator<>))) + .AddClasses(c => c.AssignableTo(typeof(IValidator<>)), publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); services .Scan(s => s.FromCallingAssembly() - .AddClasses(c => c.AssignableToAny(typeof(ICommandValidator<>), typeof(IQueryValidator<>))) + .AddClasses( + c => c.AssignableToAny(typeof(ICommandValidator<>), typeof(IQueryValidator<>)), + publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); diff --git a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Commands/StartupExtensions.cs b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Commands/StartupExtensions.cs index 9c16f48..1a4d5b7 100644 --- a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Commands/StartupExtensions.cs +++ b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Commands/StartupExtensions.cs @@ -15,20 +15,26 @@ public static class StartupExtensions /// /// /// Assembly collection in which Command related types should be looked for. - public static IServiceCollection AddCommands(this IServiceCollection services, IEnumerable assemblies) + /// Determines whether only public classes should be registered + public static IServiceCollection AddCommands( + this IServiceCollection services, + IEnumerable assemblies, + bool publicOnly = false) { services .AddSingleton(); services .Scan(s => s.FromAssemblies(assemblies) // TODO: should we remove Scrutor in future? - .AddClasses(c => c.AssignableTo(typeof(ICommandHandler<>)) - .WithoutAttribute()) + .AddClasses( + c => c.AssignableTo(typeof(ICommandHandler<>)) + .WithoutAttribute(), + publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); services .Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(ICommandValidator<>))) + .AddClasses(c => c.AssignableTo(typeof(ICommandValidator<>)), publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); return services; diff --git a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Queries/StartupExtensions.cs b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Queries/StartupExtensions.cs index 1ab1d4f..b0446e2 100644 --- a/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Queries/StartupExtensions.cs +++ b/src/Allegro.Extensions.Cqrs/Allegro.Extensions.Cqrs/Queries/StartupExtensions.cs @@ -15,18 +15,21 @@ public static class StartupExtensions /// /// /// Assembly collection in which Command related types should be looked for. - public static IServiceCollection AddQueries(this IServiceCollection services, IEnumerable assemblies) + /// Determines whether only public classes should be registered + public static IServiceCollection AddQueries(this IServiceCollection services, IEnumerable assemblies, bool publicOnly = false) { services.AddSingleton(); services.Scan(s => s.FromAssemblies(assemblies) // TODO: remove scrutor and register by own util - .AddClasses(c => c.AssignableTo(typeof(IQueryHandler<,>)) - .WithoutAttribute()) + .AddClasses( + c => c.AssignableTo(typeof(IQueryHandler<,>)) + .WithoutAttribute(), + publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); services .Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(IQueryValidator<>))) + .AddClasses(c => c.AssignableTo(typeof(IQueryValidator<>)), publicOnly) .AsImplementedInterfaces() .WithScopedLifetime()); return services; diff --git a/src/Allegro.Extensions.Cqrs/version.xml b/src/Allegro.Extensions.Cqrs/version.xml index 61b1a4c..f64b653 100644 --- a/src/Allegro.Extensions.Cqrs/version.xml +++ b/src/Allegro.Extensions.Cqrs/version.xml @@ -1,5 +1,5 @@ - 2.1.0 + 2.2.0 \ No newline at end of file diff --git a/src/Allegro.Extensions.DependencyCall/Allegro.Extensions.DependencyCall/StartupExtensions.cs b/src/Allegro.Extensions.DependencyCall/Allegro.Extensions.DependencyCall/StartupExtensions.cs index b87ee75..2fcce08 100644 --- a/src/Allegro.Extensions.DependencyCall/Allegro.Extensions.DependencyCall/StartupExtensions.cs +++ b/src/Allegro.Extensions.DependencyCall/Allegro.Extensions.DependencyCall/StartupExtensions.cs @@ -12,10 +12,15 @@ public static class StartupExtensions /// /// Register dependency call abstractions and scan to register usages; /// + /// + /// An optional action to configure the dependency call builder. + /// An optional collection of assemblies in which dependency call types should be looked for. + /// Determines whether only public classes should be registered public static IServiceCollection AddDependencyCall( this IServiceCollection services, Action? configureDependencyCall = null, - IReadOnlyCollection? applicationAssemblies = null) + IReadOnlyCollection? applicationAssemblies = null, + bool publicOnly = false) { var builder = new DependencyCallBuilder(services); configureDependencyCall?.Invoke(builder); @@ -26,7 +31,7 @@ public static IServiceCollection AddDependencyCall( .FromAssemblies( applicationAssemblies ?? AppDomain.CurrentDomain.GetAssemblies()) - .AddClasses(c => c.AssignableTo(typeof(IDependencyCall<,>))) + .AddClasses(c => c.AssignableTo(typeof(IDependencyCall<,>)), publicOnly) .AsImplementedInterfaces()// TODO: remove scrutor and register by own util .WithTransientLifetime()); return services diff --git a/src/Allegro.Extensions.DependencyCall/version.xml b/src/Allegro.Extensions.DependencyCall/version.xml index a715999..8d2acde 100644 --- a/src/Allegro.Extensions.DependencyCall/version.xml +++ b/src/Allegro.Extensions.DependencyCall/version.xml @@ -1,5 +1,5 @@ - 1.1.1 + 1.2.0 \ No newline at end of file