Skip to content

Commit

Permalink
Merge pull request #41 from allegro/feature/Scrutor-PublicOnlyParameter
Browse files Browse the repository at this point in the history
Fix AddClasses usages to explicitly pass publicOnly parameter
  • Loading branch information
cugoszi authored Jan 14, 2025
2 parents d7b79b9 + a7e01c7 commit beb4072
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ public static class StartupExtensions
/// </summary>
/// <param name="services"></param>
/// <param name="assemblies">Assembly collection in which IValidators should be looked for.</param>
public static IServiceCollection AddCqrsFluentValidations(this IServiceCollection services, IEnumerable<Assembly> assemblies)
/// <param name="publicOnly">Determines whether only public classes should be registered</param>
public static IServiceCollection AddCqrsFluentValidations(
this IServiceCollection services,
IEnumerable<Assembly> 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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ public static class StartupExtensions
/// </summary>
/// <param name="services"></param>
/// <param name="assemblies">Assembly collection in which Command related types should be looked for.</param>
public static IServiceCollection AddCommands(this IServiceCollection services, IEnumerable<Assembly> assemblies)
/// <param name="publicOnly">Determines whether only public classes should be registered</param>
public static IServiceCollection AddCommands(
this IServiceCollection services,
IEnumerable<Assembly> assemblies,
bool publicOnly = false)
{
services
.AddSingleton<ICommandDispatcher, CommandDispatcher>();
services
.Scan(s => s.FromAssemblies(assemblies) // TODO: should we remove Scrutor in future?
.AddClasses(c => c.AssignableTo(typeof(ICommandHandler<>))
.WithoutAttribute<DecoratorAttribute>())
.AddClasses(
c => c.AssignableTo(typeof(ICommandHandler<>))
.WithoutAttribute<DecoratorAttribute>(),
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ public static class StartupExtensions
/// </summary>
/// <param name="services"></param>
/// <param name="assemblies">Assembly collection in which Command related types should be looked for.</param>
public static IServiceCollection AddQueries(this IServiceCollection services, IEnumerable<Assembly> assemblies)
/// <param name="publicOnly">Determines whether only public classes should be registered</param>
public static IServiceCollection AddQueries(this IServiceCollection services, IEnumerable<Assembly> assemblies, bool publicOnly = false)
{
services.AddSingleton<IQueryDispatcher, QueryDispatcher>();
services.Scan(s => s.FromAssemblies(assemblies) // TODO: remove scrutor and register by own util
.AddClasses(c => c.AssignableTo(typeof(IQueryHandler<,>))
.WithoutAttribute<DecoratorAttribute>())
.AddClasses(
c => c.AssignableTo(typeof(IQueryHandler<,>))
.WithoutAttribute<DecoratorAttribute>(),
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;
Expand Down
8 changes: 8 additions & 0 deletions src/Allegro.Extensions.Cqrs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2025-01-14
### Allegro.Extensions.Cqrs
- Added publicOnly parameter to AddQueries and AddCommands methods
- Unified commands/queries registration behavior across different Scrutor versions
### Allegro.Extensions.Cqrs.FluentValidations
- Added publicOnly parameter to AddCqrsFluentValidations method
- Unified validators registration behavior across different Scrutor versions

## [2.1.0] - 2023-01-26

### Allegro.Extensions.Cqrs
Expand Down
2 changes: 1 addition & 1 deletion src/Allegro.Extensions.Cqrs/version.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionPrefix>2.2.0</VersionPrefix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ public static class StartupExtensions
/// <summary>
/// Register dependency call abstractions and scan to register usages;
/// </summary>
/// <param name="services"></param>
/// <param name="configureDependencyCall">An optional action to configure the dependency call builder.</param>
/// <param name="applicationAssemblies">An optional collection of assemblies in which dependency call types should be looked for.</param>
/// <param name="publicOnly">Determines whether only public classes should be registered</param>
public static IServiceCollection AddDependencyCall(
this IServiceCollection services,
Action<DependencyCallBuilder>? configureDependencyCall = null,
IReadOnlyCollection<Assembly>? applicationAssemblies = null)
IReadOnlyCollection<Assembly>? applicationAssemblies = null,
bool publicOnly = false)
{
var builder = new DependencyCallBuilder(services);
configureDependencyCall?.Invoke(builder);
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Allegro.Extensions.DependencyCall/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2025-01-14

### Added
* Added publicOnly parameter to AddDependencyCall method
* Unified DependencyCall registration behavior across different Scrutor versions

## [1.1.0] - 2024-01-31

Expand Down
2 changes: 1 addition & 1 deletion src/Allegro.Extensions.DependencyCall/version.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.1.1</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
</PropertyGroup>
</Project>

0 comments on commit beb4072

Please sign in to comment.