diff --git a/docs/Registering and Calling Handlers.md b/docs/Registering and Calling Handlers.md index f320300..4d894a5 100644 --- a/docs/Registering and Calling Handlers.md +++ b/docs/Registering and Calling Handlers.md @@ -26,10 +26,5 @@ public Task ExecuteAsync(string property, CancellationToken cancellation ## Directly -Simply expose the base `ExecuteAsync` method with a new public method, passing in a command/query request object directly. +Simply call the base `ExecuteAsync` method, passing in a command/query request object directly. -```csharp -// Inside DoThingHandler -public new Task ExecuteAsync(DoThingCommand command, CancellationToken cancellationToken = default) - => base.ExecuteAsync(command, cancellationToken); -``` diff --git a/src/DannyGoodacre.Core/CommandQuery/CommandHandler.cs b/src/DannyGoodacre.Core/CommandQuery/CommandHandler.cs index bb620d5..297cdcf 100644 --- a/src/DannyGoodacre.Core/CommandQuery/CommandHandler.cs +++ b/src/DannyGoodacre.Core/CommandQuery/CommandHandler.cs @@ -13,8 +13,7 @@ public abstract class CommandHandler(ILogger logger) : CommandHandlerBase(logger) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => result; + protected private override Result MapResult(Result result) => result; } /// @@ -28,6 +27,5 @@ public abstract class CommandHandler(ILogger logger) : CommandHandlerBase>(logger) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => new(result); + protected private override Result MapResult(Result result) => new(result); } diff --git a/src/DannyGoodacre.Core/CommandQuery/CommandHandlerBase.cs b/src/DannyGoodacre.Core/CommandQuery/CommandHandlerBase.cs index 66e54b6..56ed4e8 100644 --- a/src/DannyGoodacre.Core/CommandQuery/CommandHandlerBase.cs +++ b/src/DannyGoodacre.Core/CommandQuery/CommandHandlerBase.cs @@ -44,7 +44,8 @@ protected virtual void Validate(ValidationState validationState, TCommand comman /// The command to validate and process. /// A to observe while performing the operation. /// A indicating the outcome of the operation. - protected async Task ExecuteAsync(TCommand command, CancellationToken cancellationToken = default) + // ReSharper disable once MemberCanBeProtected.Global + public async virtual Task ExecuteAsync(TCommand command, CancellationToken cancellationToken = default) { var validationState = new ValidationState(); @@ -76,7 +77,7 @@ protected async Task ExecuteAsync(TCommand command, CancellationToken c } catch (Exception ex) { - // LogFailed(Logger, ex, CommandName); + LogFailed(Logger, ex, CommandName); return MapResult(Result.InternalError(ex.Message)); } diff --git a/src/DannyGoodacre.Core/CommandQuery/QueryHandler.cs b/src/DannyGoodacre.Core/CommandQuery/QueryHandler.cs index 2e68d55..c0f2385 100644 --- a/src/DannyGoodacre.Core/CommandQuery/QueryHandler.cs +++ b/src/DannyGoodacre.Core/CommandQuery/QueryHandler.cs @@ -16,9 +16,7 @@ public abstract partial class QueryHandler(ILogger logger) /// /// A to populate with the operation's outcome. /// The query request to validate. - protected virtual void Validate(ValidationState validationState, TQuery queryRequest) - { - } + protected virtual void Validate(ValidationState validationState, TQuery queryRequest) { } /// /// The internal query logic. @@ -34,7 +32,8 @@ protected virtual void Validate(ValidationState validationState, TQuery queryReq /// The query to validate and process. /// A to observe while performing the operation. /// A indicating the outcome of the operation. - protected async Task> ExecuteAsync(TQuery query, CancellationToken cancellationToken) + // ReSharper disable once MemberCanBeProtected.Global + public async Task> ExecuteAsync(TQuery query, CancellationToken cancellationToken = default) { var validationState = new ValidationState(); diff --git a/src/DannyGoodacre.Core/CommandQuery/StateCommandHandler.cs b/src/DannyGoodacre.Core/CommandQuery/StateCommandHandler.cs index fb19342..5528a95 100644 --- a/src/DannyGoodacre.Core/CommandQuery/StateCommandHandler.cs +++ b/src/DannyGoodacre.Core/CommandQuery/StateCommandHandler.cs @@ -13,8 +13,7 @@ public abstract class StateCommandHandler(ILogger logger, IStateUnit s : StateCommandHandlerBase(logger, stateUnit) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => result; + protected private override Result MapResult(Result result) => result; } /// @@ -28,6 +27,5 @@ public abstract class StateCommandHandler(ILogger logger, ISt : StateCommandHandlerBase>(logger, stateUnit) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => new(result); + protected private override Result MapResult(Result result) => new(result); } diff --git a/src/DannyGoodacre.Core/CommandQuery/StateCommandHandlerBase.cs b/src/DannyGoodacre.Core/CommandQuery/StateCommandHandlerBase.cs index f2d2dbf..5319132 100644 --- a/src/DannyGoodacre.Core/CommandQuery/StateCommandHandlerBase.cs +++ b/src/DannyGoodacre.Core/CommandQuery/StateCommandHandlerBase.cs @@ -15,15 +15,15 @@ internal StateCommandHandlerBase(ILogger logger, IStateUnit stateUnit) : base(lo private IStateUnit StateUnit { get; } - protected new async Task ExecuteAsync(TCommand command, CancellationToken cancellationToken = default) + public async override Task ExecuteAsync(TCommand command, CancellationToken cancellationToken = default) { try { - var result = await base.ExecuteAsync(command, cancellationToken); + TResult result = await base.ExecuteAsync(command, cancellationToken); if (result.IsSuccess) { - await StateUnit.SaveChangesAsync(cancellationToken); + _ = await StateUnit.SaveChangesAsync(cancellationToken); } return result; diff --git a/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandler.cs b/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandler.cs index ba3b3b6..ee482cf 100644 --- a/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandler.cs +++ b/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandler.cs @@ -14,8 +14,7 @@ public abstract class TransactionCommandHandler(ILogger logger, ITrans : TransactionCommandHandlerBase(logger, transactionUnit) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => result; + protected private override Result MapResult(Result result) => result; } /// @@ -30,6 +29,5 @@ public abstract class TransactionCommandHandler(ILogger logge : TransactionCommandHandlerBase>(logger, transactionUnit) where TCommand : ICommand { - protected private override Result MapResult(Result result) - => new(result); + protected private override Result MapResult(Result result) => new(result); } diff --git a/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandlerBase.cs b/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandlerBase.cs index 7f688e3..20a3ea7 100644 --- a/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandlerBase.cs +++ b/src/DannyGoodacre.Core/CommandQuery/TransactionCommandHandlerBase.cs @@ -27,13 +27,13 @@ internal TransactionCommandHandlerBase(ILogger logger, ITransactionUnit transact /// protected virtual int ExpectedChanges { get; set; } = -1; - protected new async Task ExecuteAsync(TCommand command, CancellationToken cancellationToken) + public async override Task ExecuteAsync(TCommand command, CancellationToken cancellationToken = default) { - await using var transaction = await TransactionUnit.BeginTransactionAsync(cancellationToken); + await using ITransaction transaction = await TransactionUnit.BeginTransactionAsync(cancellationToken); try { - var result = await base.ExecuteAsync(command, cancellationToken); + TResult result = await base.ExecuteAsync(command, cancellationToken); if (!result.IsSuccess) { @@ -42,7 +42,7 @@ internal TransactionCommandHandlerBase(ILogger logger, ITransactionUnit transact return result; } - var actualChanges = await TransactionUnit.SaveChangesAsync(cancellationToken); + int actualChanges = await TransactionUnit.SaveChangesAsync(cancellationToken); if (ExpectedChanges != -1 && actualChanges != ExpectedChanges) { diff --git a/src/DannyGoodacre.Core/DannyGoodacre.Core.csproj b/src/DannyGoodacre.Core/DannyGoodacre.Core.csproj index 3a946e9..e4cc6e5 100644 --- a/src/DannyGoodacre.Core/DannyGoodacre.Core.csproj +++ b/src/DannyGoodacre.Core/DannyGoodacre.Core.csproj @@ -3,7 +3,7 @@ net10.0 DannyGoodacre.Core - 0.4.0 + 0.5.0 Danny Goodacre A lightweight CQRS and clean architecture foundation library, including a result pattern and state management. https://github.com/dannygoodacre/DannyGoodacre.Core @@ -13,7 +13,7 @@ - + diff --git a/src/DannyGoodacre.Core/Extensions/ServiceCollectionExtensions.cs b/src/DannyGoodacre.Core/Extensions/ServiceCollectionExtensions.cs index e6ec070..b4afe34 100644 --- a/src/DannyGoodacre.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/DannyGoodacre.Core/Extensions/ServiceCollectionExtensions.cs @@ -11,17 +11,17 @@ public static class ServiceCollectionExtensions { public IServiceCollection AddCommandHandlers(params Assembly[] assemblies) { - var handlerTypes = assemblies + IEnumerable handlerTypes = assemblies .SelectMany(x => x.GetTypes()) .Where(x => x is { IsAbstract: false, IsClass: true } && x.IsCommandHandler()); - foreach (var handlerType in handlerTypes) + foreach (Type handlerType in handlerTypes) { - services.AddScoped(handlerType); + _ = services.AddScoped(handlerType); - var interfaces = handlerType.GetHandlerInterfaces(); + IEnumerable interfaces = handlerType.GetHandlerInterfaces(); - foreach (var serviceType in interfaces) + foreach (Type serviceType in interfaces) { services.AddScoped(serviceType, handlerType); } @@ -32,17 +32,17 @@ public IServiceCollection AddCommandHandlers(params Assembly[] assemblies) public IServiceCollection AddQueryHandlers(params Assembly[] assemblies) { - var handlerTypes = assemblies + IEnumerable handlerTypes = assemblies .SelectMany(x => x.GetTypes()) .Where(x => x is { IsAbstract: false, IsClass: true } && x.IsQueryHandler()); - foreach (var handlerType in handlerTypes) + foreach (Type handlerType in handlerTypes) { services.AddScoped(handlerType); - var interfaces = handlerType.GetHandlerInterfaces(); + IEnumerable interfaces = handlerType.GetHandlerInterfaces(); - foreach (var serviceType in interfaces) + foreach (Type serviceType in interfaces) { services.AddScoped(serviceType, handlerType); } diff --git a/src/DannyGoodacre.Core/Extensions/TypeExtensions.cs b/src/DannyGoodacre.Core/Extensions/TypeExtensions.cs index 046ccbf..1589ba6 100644 --- a/src/DannyGoodacre.Core/Extensions/TypeExtensions.cs +++ b/src/DannyGoodacre.Core/Extensions/TypeExtensions.cs @@ -8,13 +8,13 @@ internal static class TypeExtensions { public bool IsCommandHandler() { - var baseType = type.BaseType; + Type? baseType = type.BaseType; while (baseType is not null) { if (baseType.IsGenericType) { - var definition = baseType.GetGenericTypeDefinition(); + Type definition = baseType.GetGenericTypeDefinition(); if (definition == typeof(CommandHandlerBase<,>)) { @@ -30,13 +30,13 @@ public bool IsCommandHandler() public bool IsQueryHandler() { - var baseType = type.BaseType; + Type? baseType = type.BaseType; while (baseType is not null) { if (baseType.IsGenericType) { - var definition = baseType.GetGenericTypeDefinition(); + Type definition = baseType.GetGenericTypeDefinition(); if (definition == typeof(QueryHandler<,>)) { diff --git a/src/DannyGoodacre.Core/ValidationState.cs b/src/DannyGoodacre.Core/ValidationState.cs index 6bce443..ebf0d4c 100644 --- a/src/DannyGoodacre.Core/ValidationState.cs +++ b/src/DannyGoodacre.Core/ValidationState.cs @@ -11,7 +11,7 @@ public IReadOnlyDictionary> Errors public void AddError(string property, string error) { - if (_errors.TryGetValue(property, out var err)) + if (_errors.TryGetValue(property, out List? err)) { err.Add(error); diff --git a/src/DannyGoodacre.Testing.Core/DannyGoodacre.Testing.Core.csproj b/src/DannyGoodacre.Testing.Core/DannyGoodacre.Testing.Core.csproj index 8d28ce4..64efd85 100644 --- a/src/DannyGoodacre.Testing.Core/DannyGoodacre.Testing.Core.csproj +++ b/src/DannyGoodacre.Testing.Core/DannyGoodacre.Testing.Core.csproj @@ -3,7 +3,7 @@ net10.0 DannyGoodacre.Testing.Core - 0.3.0 + 0.5.0 Danny Goodacre A lightweight testing foundation library. https://github.com/dannygoodacre/DannyGoodacre.Core @@ -13,10 +13,10 @@ - - + + - + diff --git a/src/DannyGoodacre.Testing.Core/StateCommandHandlerTestBase.cs b/src/DannyGoodacre.Testing.Core/StateCommandHandlerTestBase.cs index 477ef69..60af568 100644 --- a/src/DannyGoodacre.Testing.Core/StateCommandHandlerTestBase.cs +++ b/src/DannyGoodacre.Testing.Core/StateCommandHandlerTestBase.cs @@ -27,12 +27,10 @@ public void StateSetUp() protected Mock StateUnitMock { get; private set; } = null!; protected void SetupLogger_CanceledWhilePersistingChanges() - => LoggerMock.Setup(LogLevel.Information, - $"Command '{CommandName}' was canceled while persisting changes."); + => LoggerMock.Setup(LogLevel.Information, $"Command '{CommandName}' was canceled while persisting changes."); protected void SetupLogger_FailedWhilePersistingChanges(Exception exception) - => LoggerMock.Setup(LogLevel.Critical, - $"Command '{CommandName}' failed while persisting changes.", exception: exception); + => LoggerMock.Setup(LogLevel.Critical, $"Command '{CommandName}' failed while persisting changes.", exception: exception); protected void SetupStateUnit_SaveChangesAsync() => StateUnitMock diff --git a/src/DannyGoodacre.Testing.Core/TestBase.cs b/src/DannyGoodacre.Testing.Core/TestBase.cs index abe20c2..bf5496e 100644 --- a/src/DannyGoodacre.Testing.Core/TestBase.cs +++ b/src/DannyGoodacre.Testing.Core/TestBase.cs @@ -5,8 +5,7 @@ namespace DannyGoodacre.Testing.Core; public abstract class TestBase { [TearDown] - public void BaseTearDown() - => VerifyAllAndNoOtherCalls(); + public void BaseTearDown() => VerifyAllAndNoOtherCalls(); protected static void AssertSuccess(Result result) { @@ -132,26 +131,26 @@ protected static void AssertInternalError(Result result, string error) private void VerifyAllAndNoOtherCalls() { - var mockFields = GetType() + IEnumerable mockFields = GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) - .Where(f => f.FieldType.IsGenericType - && f.FieldType.GetGenericTypeDefinition() == typeof(Mock<>)); + .Where(x => x.FieldType.IsGenericType + && x.FieldType.GetGenericTypeDefinition() == typeof(Mock<>)); - foreach (var mockField in mockFields) + foreach (FieldInfo mockField in mockFields) { - var mock = mockField.GetValue(this); + object? mock = mockField.GetValue(this); if (mock is null) { continue; } - var type = mock.GetType(); + Type type = mock.GetType(); - var verifyAllMethod = type.GetMethod("VerifyAll", Type.EmptyTypes); + MethodInfo? verifyAllMethod = type.GetMethod("VerifyAll", Type.EmptyTypes); verifyAllMethod?.Invoke(mock, null); - var verifyNoOtherCallsMethod = type.GetMethod("VerifyNoOtherCalls", Type.EmptyTypes); + MethodInfo? verifyNoOtherCallsMethod = type.GetMethod("VerifyNoOtherCalls", Type.EmptyTypes); verifyNoOtherCallsMethod?.Invoke(mock, null); } } diff --git a/src/DannyGoodacre.Testing.Core/TransactionCommandHandlerTestBase.cs b/src/DannyGoodacre.Testing.Core/TransactionCommandHandlerTestBase.cs index d423062..72b56d8 100644 --- a/src/DannyGoodacre.Testing.Core/TransactionCommandHandlerTestBase.cs +++ b/src/DannyGoodacre.Testing.Core/TransactionCommandHandlerTestBase.cs @@ -40,8 +40,7 @@ public override void BaseSetUp() protected void SetupLogger_UnexpectedNumberOfChanges(int expected, int actual) => LoggerMock - .Setup(LogLevel.Error, - $"Command '{CommandName}' attempted to persist an unexpected number of changes: Expected '{expected}', Actual '{actual}'."); + .Setup(LogLevel.Error, $"Command '{CommandName}' attempted to persist an unexpected number of changes: Expected '{expected}', Actual '{actual}'."); protected void SetupLogger_CanceledDuringRollback() => LoggerMock diff --git a/tests/DannyGoodacre.Core.Tests/DannyGoodacre.Core.Tests.csproj b/tests/DannyGoodacre.Core.Tests/DannyGoodacre.Core.Tests.csproj index 3121ef8..1a4b7bb 100644 --- a/tests/DannyGoodacre.Core.Tests/DannyGoodacre.Core.Tests.csproj +++ b/tests/DannyGoodacre.Core.Tests/DannyGoodacre.Core.Tests.csproj @@ -13,7 +13,7 @@ - +