From ed38a76c7918777c34a36026adaf81d19b318d50 Mon Sep 17 00:00:00 2001 From: adimiko Date: Mon, 10 Mar 2025 20:31:49 +0100 Subject: [PATCH 1/3] Moved from interfaces to abstract class for handlers and changed depejdency injection --- .../Commands/ErrorCommandHandler.cs | 4 +- .../Commands/SampleCommandHandler.cs | 4 +- .../SampleCommandWithResultHandler.cs | 4 +- .../Events/SampleEventHandler1.cs | 4 +- .../Events/SampleEventHandler2.cs | 4 +- samples/EasyWay.Samples/Program.cs | 3 + .../Queries/SampleQueryHandler.cs | 6 +- .../{ICommandHandler.cs => CommandHandler.cs} | 8 +- ...nEventHandler.cs => DomainEventHandler.cs} | 4 +- .../Internals/Commands/CommandExecutor.cs | 2 +- .../CommandWithOperationResultExecutor.cs | 2 +- .../EasyWay/Internals/Commands/Extensions.cs | 10 +- .../DomainEvents/DomainEventPublisher.cs | 4 +- .../Internals/DomainEvents/Extensions.cs | 5 +- .../Internals/DomainServices/Extensions.cs | 7 +- source/EasyWay/Internals/Extensions.cs | 112 ++++++++++++++++++ .../EasyWay/Internals/Factories/Extensions.cs | 7 +- .../Internals/Initializers/Extensions.cs | 5 +- .../EasyWay/Internals/Policies/Extensions.cs | 7 ++ .../EasyWay/Internals/Queries/Extensions.cs | 8 +- .../Internals/Queries/QueryExecutor.cs | 2 +- .../Internals/Repositories/Extensions.cs | 5 +- .../{IQueryHandler.cs => QueryHandler.cs} | 8 +- 23 files changed, 159 insertions(+), 66 deletions(-) rename source/EasyWay/{ICommandHandler.cs => CommandHandler.cs} (62%) rename source/EasyWay/{IDomainEventHandler.cs => DomainEventHandler.cs} (74%) rename source/EasyWay/{IQueryHandler.cs => QueryHandler.cs} (68%) diff --git a/samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs b/samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs index d216c52..ec32cb6 100644 --- a/samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs +++ b/samples/EasyWay.Samples/Commands/ErrorCommandHandler.cs @@ -2,9 +2,9 @@ namespace EasyWay.Samples.Commands { - public class ErrorCommandHandler : ICommandHandler + public sealed class ErrorCommandHandler : CommandHandler { - public Task Handle(ErrorCommand command) + public sealed override Task Handle(ErrorCommand command) { var x = new SampleAggregateRoot(); diff --git a/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs b/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs index c0037fa..fce0fe8 100644 --- a/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs +++ b/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs @@ -3,7 +3,7 @@ namespace EasyWay.Samples.Commands { - internal sealed class SampleCommandHandler : ICommandHandler + internal sealed class SampleCommandHandler : CommandHandler { private readonly ICancellationContext _cancellationContext; @@ -37,7 +37,7 @@ public SampleCommandHandler( _userContext = userContext; } - public async Task Handle(SampleCommand command) + public sealed override async Task Handle(SampleCommand command) { var userId = _userContext.UserId; diff --git a/samples/EasyWay.Samples/Commands/WithResult/SampleCommandWithResultHandler.cs b/samples/EasyWay.Samples/Commands/WithResult/SampleCommandWithResultHandler.cs index daf653e..8a2d87d 100644 --- a/samples/EasyWay.Samples/Commands/WithResult/SampleCommandWithResultHandler.cs +++ b/samples/EasyWay.Samples/Commands/WithResult/SampleCommandWithResultHandler.cs @@ -1,9 +1,9 @@  namespace EasyWay.Samples.Commands.WithResult { - public sealed class SampleCommandWithResultHandler : ICommandHandler + public sealed class SampleCommandWithResultHandler : CommandHandler { - public Task> Handle(SampleCommandWithResult command) + public sealed override Task> Handle(SampleCommandWithResult command) { return Task.FromResult(CommandResult.Ok(new SampleCommandResult())); } diff --git a/samples/EasyWay.Samples/Events/SampleEventHandler1.cs b/samples/EasyWay.Samples/Events/SampleEventHandler1.cs index 8d8a037..f32b46e 100644 --- a/samples/EasyWay.Samples/Events/SampleEventHandler1.cs +++ b/samples/EasyWay.Samples/Events/SampleEventHandler1.cs @@ -2,9 +2,9 @@ namespace EasyWay.Samples.Events { - public class SampleEventHandler1 : IDomainEventHandler + public sealed class SampleEventHandler1 : DomainEventHandler { - public Task Handle(CreatedSampleAggragete @event) + public sealed override Task Handle(CreatedSampleAggragete domainEvent) { return Task.CompletedTask; } diff --git a/samples/EasyWay.Samples/Events/SampleEventHandler2.cs b/samples/EasyWay.Samples/Events/SampleEventHandler2.cs index 7ce21da..df29dab 100644 --- a/samples/EasyWay.Samples/Events/SampleEventHandler2.cs +++ b/samples/EasyWay.Samples/Events/SampleEventHandler2.cs @@ -2,9 +2,9 @@ namespace EasyWay.Samples.Events { - public class SampleEventHandler2 : IDomainEventHandler + public sealed class SampleEventHandler2 : DomainEventHandler { - public Task Handle(CreatedSampleAggragete @event) + public sealed override Task Handle(CreatedSampleAggragete domainEvent) { return Task.CompletedTask; } diff --git a/samples/EasyWay.Samples/Program.cs b/samples/EasyWay.Samples/Program.cs index 568cd7c..e8d1e97 100644 --- a/samples/EasyWay.Samples/Program.cs +++ b/samples/EasyWay.Samples/Program.cs @@ -1,6 +1,7 @@ using EasyWay.Samples; using EasyWay.Samples.Commands; using EasyWay.Samples.Commands.WithResult; +using EasyWay.Samples.Domain.Policies; using EasyWay.Samples.Queries; using Microsoft.AspNetCore.Mvc; @@ -40,6 +41,8 @@ await kernel app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor executor, IWebApiResultMapper mapper) => { + + return await executor.Command(command); }); diff --git a/samples/EasyWay.Samples/Queries/SampleQueryHandler.cs b/samples/EasyWay.Samples/Queries/SampleQueryHandler.cs index 51a0fe1..094b43c 100644 --- a/samples/EasyWay.Samples/Queries/SampleQueryHandler.cs +++ b/samples/EasyWay.Samples/Queries/SampleQueryHandler.cs @@ -1,11 +1,9 @@ namespace EasyWay.Samples.Queries { - public class SampleQueryHandler : IQueryHandler + internal sealed class SampleQueryHandler : QueryHandler { - public Task> Handle(SampleQuery query) + public sealed override Task> Handle(SampleQuery query) { - //return Task.FromResult(QueryResult.Forbidden); - return Task.FromResult(QueryResult.Ok(new SampleQueryResult())); } } diff --git a/source/EasyWay/ICommandHandler.cs b/source/EasyWay/CommandHandler.cs similarity index 62% rename from source/EasyWay/ICommandHandler.cs rename to source/EasyWay/CommandHandler.cs index 28a345f..16d9231 100644 --- a/source/EasyWay/ICommandHandler.cs +++ b/source/EasyWay/CommandHandler.cs @@ -4,20 +4,20 @@ /// Defines a handler for a command /// /// The type of command being handled - public interface ICommandHandler + public abstract class CommandHandler where TCommand : Command { /// /// Handles a command /// /// Command - Task Handle(TCommand command); + public abstract Task Handle(TCommand command); } - public interface ICommandHandler + public abstract class CommandHandler where TCommand : Command where TOperationResult : OperationResult { - Task> Handle(TCommand command); + public abstract Task> Handle(TCommand command); } } diff --git a/source/EasyWay/IDomainEventHandler.cs b/source/EasyWay/DomainEventHandler.cs similarity index 74% rename from source/EasyWay/IDomainEventHandler.cs rename to source/EasyWay/DomainEventHandler.cs index 8557623..a61f751 100644 --- a/source/EasyWay/IDomainEventHandler.cs +++ b/source/EasyWay/DomainEventHandler.cs @@ -4,13 +4,13 @@ /// Defines a handler for an event /// /// The type of event being handled - public interface IDomainEventHandler + public abstract class DomainEventHandler where TDomainEvent : DomainEvent { /// /// Handles an event /// /// Event - Task Handle(TDomainEvent domainEvent); + public abstract Task Handle(TDomainEvent domainEvent); } } diff --git a/source/EasyWay/Internals/Commands/CommandExecutor.cs b/source/EasyWay/Internals/Commands/CommandExecutor.cs index f73be7f..61cff50 100644 --- a/source/EasyWay/Internals/Commands/CommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandExecutor.cs @@ -41,7 +41,7 @@ public async Task Execute(TCommand command, Cancellatio } var commandResult = await _serviceProvider - .GetRequiredService>() + .GetRequiredService>() .Handle(command); await _unitOfWorkCommandHandler.Handle(); diff --git a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs index 1213a45..cd5d6de 100644 --- a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs @@ -41,7 +41,7 @@ public async Task> Command>(); + var commandHandler = _serviceProvider.GetRequiredService>(); var commandResult = await commandHandler.Handle(command); diff --git a/source/EasyWay/Internals/Commands/Extensions.cs b/source/EasyWay/Internals/Commands/Extensions.cs index eda3c67..c0ec9d1 100644 --- a/source/EasyWay/Internals/Commands/Extensions.cs +++ b/source/EasyWay/Internals/Commands/Extensions.cs @@ -13,15 +13,9 @@ internal static IServiceCollection AddCommands( services.AddScoped(typeof(ICommandExecutor<>).MakeGenericType(moduleType), typeof(CommandExecutor<>).MakeGenericType(moduleType)); services.AddScoped(typeof(ICommandWithOperationResultExecutor<>).MakeGenericType(moduleType), typeof(CommandWithOperationResultExecutor<>).MakeGenericType(moduleType)); - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(ICommandHandler<>))) - .AsImplementedInterfaces() - .WithScopedLifetime()); + services.AddAsBasedType(typeof(CommandHandler<>), ServiceLifetime.Scoped, assemblies); - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(ICommandHandler<,>))) - .AsImplementedInterfaces() - .WithScopedLifetime()); + services.AddAsBasedType(typeof(CommandHandler<,>), ServiceLifetime.Scoped, assemblies); services.AddScoped(); diff --git a/source/EasyWay/Internals/DomainEvents/DomainEventPublisher.cs b/source/EasyWay/Internals/DomainEvents/DomainEventPublisher.cs index 4e20702..3e360f9 100644 --- a/source/EasyWay/Internals/DomainEvents/DomainEventPublisher.cs +++ b/source/EasyWay/Internals/DomainEvents/DomainEventPublisher.cs @@ -14,14 +14,14 @@ public DomainEventPublisher(IServiceProvider serviceProvider) public async Task Publish(TEvent @event) where TEvent : DomainEvent { - var handlerType = typeof(IDomainEventHandler<>).MakeGenericType(@event.GetType()); + var handlerType = typeof(DomainEventHandler<>).MakeGenericType(@event.GetType()); var eventHandlers = _serviceProvider.GetServices(handlerType); foreach (var eventHandler in eventHandlers) { await (Task)handlerType - .GetMethod(nameof(IDomainEventHandler.Handle))? + .GetMethod(nameof(DomainEventHandler.Handle))? .Invoke(eventHandler, new object[] { @event }); } } diff --git a/source/EasyWay/Internals/DomainEvents/Extensions.cs b/source/EasyWay/Internals/DomainEvents/Extensions.cs index 93129e2..95defe3 100644 --- a/source/EasyWay/Internals/DomainEvents/Extensions.cs +++ b/source/EasyWay/Internals/DomainEvents/Extensions.cs @@ -11,10 +11,7 @@ internal static IServiceCollection AddDomainEvents(this IServiceCollection servi services.AddScoped(); services.AddScoped(); - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(IDomainEventHandler<>))) - .AsImplementedInterfaces() - .WithScopedLifetime()); + services.AddAsBasedType(typeof(DomainEventHandler<>), ServiceLifetime.Scoped, assemblies); return services; } diff --git a/source/EasyWay/Internals/DomainServices/Extensions.cs b/source/EasyWay/Internals/DomainServices/Extensions.cs index 2a93293..72dd54f 100644 --- a/source/EasyWay/Internals/DomainServices/Extensions.cs +++ b/source/EasyWay/Internals/DomainServices/Extensions.cs @@ -5,16 +5,11 @@ namespace EasyWay.Internals.DomainServices { internal static class Extensions { - private static Type _domainServiceType = typeof(DomainService); - internal static IServiceCollection AddDomainServices( this IServiceCollection services, IEnumerable assemblies) { - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.Where(x => x.IsSubclassOf(_domainServiceType))) - .AsSelf() - .WithTransientLifetime()); + services.AddSelfOnBasedType(typeof(DomainService), ServiceLifetime.Transient, assemblies); return services; } diff --git a/source/EasyWay/Internals/Extensions.cs b/source/EasyWay/Internals/Extensions.cs index a5a9ee8..ece3355 100644 --- a/source/EasyWay/Internals/Extensions.cs +++ b/source/EasyWay/Internals/Extensions.cs @@ -32,5 +32,117 @@ internal static void AddEasyWay( .AddFactories(assemblies) .AddInitializers(assemblies); } + + internal static IServiceCollection AddAsBasedType( + this IServiceCollection services, + Type baseType, + ServiceLifetime serviceLifetime, + IEnumerable assemblies) + { + var typesFromAsseblies = assemblies.SelectMany(x => x.GetTypes()); + + IEnumerable expectedTypes; + + if (baseType.IsGenericType) + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.BaseType != null && x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == baseType); + } + else + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.BaseType != null && x.BaseType == baseType); + } + + expectedTypes = expectedTypes.Distinct(); + + foreach (var type in expectedTypes) + { + services.Add(new ServiceDescriptor(type.BaseType, type, serviceLifetime)); + } + + return services; + } + + internal static IServiceCollection AddSelfOnBasedType( + this IServiceCollection services, + Type baseType, + ServiceLifetime serviceLifetime, + IEnumerable assemblies) + { + var typesFromAsseblies = assemblies.SelectMany(x => x.GetTypes()); + + IEnumerable expectedTypes; + + if (baseType.IsGenericType) + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.BaseType != null && x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == baseType); + } + else + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.BaseType != null && x.BaseType == baseType); + } + + expectedTypes = expectedTypes.Distinct(); + + foreach (var type in expectedTypes) + { + services.Add(new ServiceDescriptor(type, type, serviceLifetime)); + } + + return services; + } + + internal static IServiceCollection AddAsImplementedInterfaces( + this IServiceCollection services, + Type interfaceType, + ServiceLifetime serviceLifetime, + IEnumerable assemblies) + { + var typesFromAsseblies = assemblies.SelectMany(x => x.GetTypes()); + + IEnumerable expectedTypes; + + if (interfaceType.IsGenericType) + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType)) + .ToList(); + } + else + { + expectedTypes = typesFromAsseblies + .Where(x => x.IsClass && x.GetInterfaces().Any(x => x == interfaceType)); + } + + expectedTypes = expectedTypes.Distinct(); + + foreach (var type in expectedTypes) + { + + if (interfaceType.IsGenericType) + { + var interfaces = type.GetInterfaces().Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == interfaceType); + + foreach(var @interface in interfaces) + { + services.Add(new ServiceDescriptor(@interface, type, serviceLifetime)); + } + } + else + { + var interfaces = type.GetInterfaces(); + + foreach (var @interface in interfaces) + { + services.Add(new ServiceDescriptor(@interface, type, serviceLifetime)); + } + } + } + + return services; + } } } diff --git a/source/EasyWay/Internals/Factories/Extensions.cs b/source/EasyWay/Internals/Factories/Extensions.cs index 66f7a56..379ec00 100644 --- a/source/EasyWay/Internals/Factories/Extensions.cs +++ b/source/EasyWay/Internals/Factories/Extensions.cs @@ -5,16 +5,11 @@ namespace EasyWay.Internals.Factories { internal static class Extensions { - private static Type _factoryType = typeof(Factory); - internal static IServiceCollection AddFactories( this IServiceCollection services, IEnumerable assemblies) { - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.Where(x => x.IsSubclassOf(_factoryType))) - .AsSelf() - .WithTransientLifetime()); + services.AddSelfOnBasedType(typeof(Factory), ServiceLifetime.Transient, assemblies); return services; } diff --git a/source/EasyWay/Internals/Initializers/Extensions.cs b/source/EasyWay/Internals/Initializers/Extensions.cs index 24fbfd3..2d71b24 100644 --- a/source/EasyWay/Internals/Initializers/Extensions.cs +++ b/source/EasyWay/Internals/Initializers/Extensions.cs @@ -9,10 +9,7 @@ internal static IServiceCollection AddInitializers( this IServiceCollection services, IEnumerable assemblies) { - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(IInitializer))) - .AsImplementedInterfaces() - .WithTransientLifetime()); + services.AddAsImplementedInterfaces(typeof(IInitializer), ServiceLifetime.Transient, assemblies); return services; } diff --git a/source/EasyWay/Internals/Policies/Extensions.cs b/source/EasyWay/Internals/Policies/Extensions.cs index 94d04bc..5030935 100644 --- a/source/EasyWay/Internals/Policies/Extensions.cs +++ b/source/EasyWay/Internals/Policies/Extensions.cs @@ -9,6 +9,7 @@ internal static IServiceCollection AddPolicies( this IServiceCollection services, IEnumerable assemblies) { + /* services.Scan(s => s.FromAssemblies(assemblies) .AddClasses(c => c.AssignableTo(typeof(IPolicy<,>))) .AsImplementedInterfaces() @@ -18,6 +19,12 @@ internal static IServiceCollection AddPolicies( .AddClasses(c => c.AssignableTo(typeof(IPolicy<,,>))) .AsImplementedInterfaces() .WithTransientLifetime()); + */ + + services.AddAsImplementedInterfaces(typeof(IPolicy<,>), ServiceLifetime.Transient, assemblies); + + services.AddAsImplementedInterfaces(typeof(IPolicy<,,>), ServiceLifetime.Transient, assemblies); + return services; } diff --git a/source/EasyWay/Internals/Queries/Extensions.cs b/source/EasyWay/Internals/Queries/Extensions.cs index 0a1715a..02ba17d 100644 --- a/source/EasyWay/Internals/Queries/Extensions.cs +++ b/source/EasyWay/Internals/Queries/Extensions.cs @@ -12,12 +12,12 @@ internal static IServiceCollection AddQueries( { services.AddScoped(typeof(IQueryExecutor<>).MakeGenericType(moduleType), typeof(QueryExecutor<>).MakeGenericType(moduleType)); - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.AssignableTo(typeof(IQueryHandler<,>))) - .AsImplementedInterfaces() - .WithScopedLifetime()); + services.AddAsBasedType(typeof(QueryHandler<,>), ServiceLifetime.Scoped, assemblies); return services; } + + + } } diff --git a/source/EasyWay/Internals/Queries/QueryExecutor.cs b/source/EasyWay/Internals/Queries/QueryExecutor.cs index 86f295e..8b58bbf 100644 --- a/source/EasyWay/Internals/Queries/QueryExecutor.cs +++ b/source/EasyWay/Internals/Queries/QueryExecutor.cs @@ -41,7 +41,7 @@ public async Task> Execute(TQuery qu } } - var queryHandler = _serviceProvider.GetRequiredService>(); + var queryHandler = _serviceProvider.GetRequiredService>(); var queryResult = await queryHandler.Handle(query); diff --git a/source/EasyWay/Internals/Repositories/Extensions.cs b/source/EasyWay/Internals/Repositories/Extensions.cs index c811fa5..715a055 100644 --- a/source/EasyWay/Internals/Repositories/Extensions.cs +++ b/source/EasyWay/Internals/Repositories/Extensions.cs @@ -9,10 +9,7 @@ internal static class Extensions internal static IServiceCollection AddRepositories(this IServiceCollection services, IEnumerable assemblies) { - services.Scan(s => s.FromAssemblies(assemblies) - .AddClasses(c => c.Where(x => x.IsAssignableTo(_repositoryType))) - .AsImplementedInterfaces() - .WithScopedLifetime()); + services.AddAsImplementedInterfaces(typeof(IRepository), ServiceLifetime.Scoped, assemblies); return services; } diff --git a/source/EasyWay/IQueryHandler.cs b/source/EasyWay/QueryHandler.cs similarity index 68% rename from source/EasyWay/IQueryHandler.cs rename to source/EasyWay/QueryHandler.cs index 0098732..c90e239 100644 --- a/source/EasyWay/IQueryHandler.cs +++ b/source/EasyWay/QueryHandler.cs @@ -1,12 +1,10 @@ -using System.Collections.Generic; - -namespace EasyWay +namespace EasyWay { /// /// Defines a handler for a query /// /// The type of query being handled - public interface IQueryHandler + public abstract class QueryHandler where TQuery : Query where TReadModel : ReadModel { @@ -14,6 +12,6 @@ public interface IQueryHandler /// Handles a query /// /// Query - Task> Handle(TQuery query); + public abstract Task> Handle(TQuery query); } } From 332d2cdba1b24ad79fccd8af3ebbc1aa37584e9f Mon Sep 17 00:00:00 2001 From: adimiko Date: Mon, 10 Mar 2025 20:40:02 +0100 Subject: [PATCH 2/3] Changed CancellationContext from interfaces to sealed class --- samples/EasyWay.Samples/Commands/SampleCommandHandler.cs | 4 ++-- samples/EasyWay.Samples/Program.cs | 4 ++-- source/EasyWay/CancellationContext.cs | 9 +++++++++ source/EasyWay/ICancellationContext.cs | 7 ------- source/EasyWay/Internals/Commands/CommandExecutor.cs | 8 ++++---- .../Commands/CommandWithOperationResultExecutor.cs | 8 ++++---- source/EasyWay/Internals/Contexts/CancellationContext.cs | 9 --------- source/EasyWay/Internals/Contexts/Extensions.cs | 3 +-- .../Contexts/ICancellationContextConstructor.cs | 7 ------- source/EasyWay/Internals/Queries/QueryExecutor.cs | 8 ++++---- 10 files changed, 26 insertions(+), 41 deletions(-) create mode 100644 source/EasyWay/CancellationContext.cs delete mode 100644 source/EasyWay/ICancellationContext.cs delete mode 100644 source/EasyWay/Internals/Contexts/CancellationContext.cs delete mode 100644 source/EasyWay/Internals/Contexts/ICancellationContextConstructor.cs diff --git a/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs b/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs index fce0fe8..91e0b7a 100644 --- a/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs +++ b/samples/EasyWay.Samples/Commands/SampleCommandHandler.cs @@ -5,7 +5,7 @@ namespace EasyWay.Samples.Commands { internal sealed class SampleCommandHandler : CommandHandler { - private readonly ICancellationContext _cancellationContext; + private readonly CancellationContext _cancellationContext; private readonly ISampleAggragateRootRepository _repository; @@ -20,7 +20,7 @@ internal sealed class SampleCommandHandler : CommandHandler private readonly IUserContext _userContext; public SampleCommandHandler( - ICancellationContext cancellationContext, + CancellationContext cancellationContext, ISampleAggragateRootRepository repository, SampleAggregateRootFactory factory, SampleDomainService domainService, diff --git a/samples/EasyWay.Samples/Program.cs b/samples/EasyWay.Samples/Program.cs index e8d1e97..fb08d2e 100644 --- a/samples/EasyWay.Samples/Program.cs +++ b/samples/EasyWay.Samples/Program.cs @@ -41,9 +41,9 @@ await kernel app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor executor, IWebApiResultMapper mapper) => { - + var x = await executor.Command(command); - return await executor.Command(command); + return mapper.Map(x); }); app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IModuleExecutor executor, IWebApiResultMapper mapper) => diff --git a/source/EasyWay/CancellationContext.cs b/source/EasyWay/CancellationContext.cs new file mode 100644 index 0000000..6b81aab --- /dev/null +++ b/source/EasyWay/CancellationContext.cs @@ -0,0 +1,9 @@ +namespace EasyWay +{ + public sealed class CancellationContext + { + public CancellationToken Token { get; private set; } + + internal void Set(CancellationToken token) => Token = token; + } +} diff --git a/source/EasyWay/ICancellationContext.cs b/source/EasyWay/ICancellationContext.cs deleted file mode 100644 index 5c53ae3..0000000 --- a/source/EasyWay/ICancellationContext.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace EasyWay -{ - public interface ICancellationContext - { - CancellationToken Token { get; } - } -} diff --git a/source/EasyWay/Internals/Commands/CommandExecutor.cs b/source/EasyWay/Internals/Commands/CommandExecutor.cs index 61cff50..9118081 100644 --- a/source/EasyWay/Internals/Commands/CommandExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandExecutor.cs @@ -9,24 +9,24 @@ internal sealed class CommandExecutor : ICommandExecutor { private readonly IServiceProvider _serviceProvider; - private readonly ICancellationContextConstructor _cancellationContextConstructor; + private readonly CancellationContext _cancellationContext; private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler; public CommandExecutor( IServiceProvider serviceProvider, - ICancellationContextConstructor cancellationContextConstructor, + CancellationContext cancellationContext, IUnitOfWorkCommandHandler unitOfWorkCommandHandler) { _serviceProvider = serviceProvider; - _cancellationContextConstructor = cancellationContextConstructor; + _cancellationContext = cancellationContext; _unitOfWorkCommandHandler = unitOfWorkCommandHandler; } public async Task Execute(TCommand command, CancellationToken cancellationToken) where TCommand : Command { - _cancellationContextConstructor.Set(cancellationToken); + _cancellationContext.Set(cancellationToken); var validator = _serviceProvider.GetService>(); diff --git a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs b/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs index cd5d6de..78957f6 100644 --- a/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs +++ b/source/EasyWay/Internals/Commands/CommandWithOperationResultExecutor.cs @@ -9,17 +9,17 @@ internal sealed class CommandWithOperationResultExecutor : ICommandWith { private readonly IServiceProvider _serviceProvider; - private readonly ICancellationContextConstructor _cancellationContextConstructor; + private readonly CancellationContext _cancellationContext; private readonly IUnitOfWorkCommandHandler _unitOfWorkCommandHandler; public CommandWithOperationResultExecutor( IServiceProvider serviceProvider, - ICancellationContextConstructor cancellationContextConstructor, + CancellationContext cancellationContext, IUnitOfWorkCommandHandler unitOfWorkCommandHandler) { _serviceProvider = serviceProvider; - _cancellationContextConstructor = cancellationContextConstructor; + _cancellationContext = cancellationContext; _unitOfWorkCommandHandler = unitOfWorkCommandHandler; } @@ -27,7 +27,7 @@ public async Task> Command where TOperationResult : OperationResult { - _cancellationContextConstructor.Set(cancellationToken); + _cancellationContext.Set(cancellationToken); var validator = _serviceProvider.GetService>(); diff --git a/source/EasyWay/Internals/Contexts/CancellationContext.cs b/source/EasyWay/Internals/Contexts/CancellationContext.cs deleted file mode 100644 index 9ddc6fa..0000000 --- a/source/EasyWay/Internals/Contexts/CancellationContext.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace EasyWay.Internals.Contexts -{ - internal sealed class CancellationContext : ICancellationContext, ICancellationContextConstructor - { - public CancellationToken Token { get; private set; } - - public void Set(CancellationToken token) => Token = token; - } -} diff --git a/source/EasyWay/Internals/Contexts/Extensions.cs b/source/EasyWay/Internals/Contexts/Extensions.cs index cc3d72f..9c715da 100644 --- a/source/EasyWay/Internals/Contexts/Extensions.cs +++ b/source/EasyWay/Internals/Contexts/Extensions.cs @@ -6,8 +6,7 @@ internal static class Extensions { internal static IServiceCollection AddContexts(this IServiceCollection services) { - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/source/EasyWay/Internals/Contexts/ICancellationContextConstructor.cs b/source/EasyWay/Internals/Contexts/ICancellationContextConstructor.cs deleted file mode 100644 index cbf92a8..0000000 --- a/source/EasyWay/Internals/Contexts/ICancellationContextConstructor.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace EasyWay.Internals.Contexts -{ - internal interface ICancellationContextConstructor - { - void Set(CancellationToken token); - } -} diff --git a/source/EasyWay/Internals/Queries/QueryExecutor.cs b/source/EasyWay/Internals/Queries/QueryExecutor.cs index 8b58bbf..9c01e3a 100644 --- a/source/EasyWay/Internals/Queries/QueryExecutor.cs +++ b/source/EasyWay/Internals/Queries/QueryExecutor.cs @@ -9,21 +9,21 @@ internal sealed class QueryExecutor : IQueryExecutor { private readonly IServiceProvider _serviceProvider; - private readonly ICancellationContextConstructor _cancellationContextConstructor; + private readonly CancellationContext _cancellationContext; public QueryExecutor( IServiceProvider serviceProvider, - ICancellationContextConstructor cancellationContextConstructor) + CancellationContext cancellationContext) { _serviceProvider = serviceProvider; - _cancellationContextConstructor = cancellationContextConstructor; + _cancellationContext = cancellationContext; } public async Task> Execute(TQuery query, CancellationToken cancellationToken = default) where TQuery : Query where TReadModel : ReadModel { - _cancellationContextConstructor.Set(cancellationToken); + _cancellationContext.Set(cancellationToken); var queryType = query.GetType(); From 9710f80115d8093d517208aac76ff60f20292c78 Mon Sep 17 00:00:00 2001 From: adimiko Date: Tue, 11 Mar 2025 07:25:43 +0100 Subject: [PATCH 3/3] Refactored code --- source/EasyWay/Internals/Extensions.cs | 3 +-- source/EasyWay/Internals/Policies/Extensions.cs | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/EasyWay/Internals/Extensions.cs b/source/EasyWay/Internals/Extensions.cs index ece3355..a6a05c1 100644 --- a/source/EasyWay/Internals/Extensions.cs +++ b/source/EasyWay/Internals/Extensions.cs @@ -108,8 +108,7 @@ internal static IServiceCollection AddAsImplementedInterfaces( if (interfaceType.IsGenericType) { expectedTypes = typesFromAsseblies - .Where(x => x.IsClass && x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType)) - .ToList(); + .Where(x => x.IsClass && x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType)); } else { diff --git a/source/EasyWay/Internals/Policies/Extensions.cs b/source/EasyWay/Internals/Policies/Extensions.cs index 5030935..e7d736c 100644 --- a/source/EasyWay/Internals/Policies/Extensions.cs +++ b/source/EasyWay/Internals/Policies/Extensions.cs @@ -9,7 +9,7 @@ internal static IServiceCollection AddPolicies( this IServiceCollection services, IEnumerable assemblies) { - /* + services.Scan(s => s.FromAssemblies(assemblies) .AddClasses(c => c.AssignableTo(typeof(IPolicy<,>))) .AsImplementedInterfaces() @@ -19,12 +19,12 @@ internal static IServiceCollection AddPolicies( .AddClasses(c => c.AssignableTo(typeof(IPolicy<,,>))) .AsImplementedInterfaces() .WithTransientLifetime()); - */ - + + /* services.AddAsImplementedInterfaces(typeof(IPolicy<,>), ServiceLifetime.Transient, assemblies); services.AddAsImplementedInterfaces(typeof(IPolicy<,,>), ServiceLifetime.Transient, assemblies); - + */ return services; }