Skip to content

Commit

Permalink
CommandResult, CreationResult
Browse files Browse the repository at this point in the history
Rename,
Record
  • Loading branch information
Brice Chapellier committed Aug 7, 2023
1 parent 6807f70 commit 724dd8c
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/HerrGeneral.Core/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<CreationResult> Send(CreationCommand command, CancellationToke
/// <param name="command"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<CommandResultV2> Send(Command command, CancellationToken cancellationToken = default) =>
public async Task<CommandResult> Send(Command command, CancellationToken cancellationToken = default) =>
await Send(command, typeof(CommandHandlerWrapper<>), cancellationToken).ConfigureAwait(false);

private async Task<TResult> Send<TResult>(CommandBase<TResult> command, Type openWrapperType, CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/HerrGeneral.Core/WriteSide/CommandHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace HerrGeneral.Core.WriteSide;

internal class CommandHandlerWrapper<TCommand> : CommandHandlerWrapperBase<TCommand, CommandResultV2> where TCommand : Command
internal class CommandHandlerWrapper<TCommand> : CommandHandlerWrapperBase<TCommand, CommandResult> where TCommand : Command
{
protected override CommandPipeline.HandlerDelegate<TCommand, CommandResultV2> BuildPipeline(IServiceProvider serviceProvider) =>
protected override CommandPipeline.HandlerDelegate<TCommand, CommandResult> BuildPipeline(IServiceProvider serviceProvider) =>
base
.BuildPipeline(serviceProvider)
.WithExceptionToCommandResult()
Expand Down
6 changes: 3 additions & 3 deletions src/HerrGeneral.Core/WriteSide/CommandPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public static HandlerDelegate<TCommand, TResult> WithUnitOfWork<TCommand, TResul
};


public static HandlerDelegate<TCommand, CommandResultV2> WithExceptionToCommandResult<TCommand>(
this HandlerDelegate<TCommand, CommandResultV2> next) =>
next.WithTryCatch(CommandResultV2.PanicFail, CommandResultV2.DomainFail);
public static HandlerDelegate<TCommand, CommandResult> WithExceptionToCommandResult<TCommand>(
this HandlerDelegate<TCommand, CommandResult> next) =>
next.WithTryCatch(CommandResult.PanicFail, CommandResult.DomainFail);

public static HandlerDelegate<TCommand, CreationResult> WithExceptionToCreationResult<TCommand>(
this HandlerDelegate<TCommand, CreationResult> next) =>
Expand Down
28 changes: 21 additions & 7 deletions src/HerrGeneral.Test.Extension/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,45 @@ public static class Extension
/// <param name="serviceProvider"></param>
/// <param name="withResultAssertion"></param>
/// <returns></returns>
public static async Task<CommandResultV2> Send(this Command request, IServiceProvider serviceProvider, bool withResultAssertion = true)
public static async Task<CommandResult> Send(this Command request, IServiceProvider serviceProvider, bool withResultAssertion = true)
{
var res = await serviceProvider.GetRequiredService<Mediator>().Send(request);

if (withResultAssertion)
res.IsSuccess.ShouldBe(true, $"{request.GetType().FullName}: {res}");
res.IsSuccess.ShouldBeTrue($"{request.GetType().FullName}: {res}");

return res;
}

/// <summary>
/// Send the creation command and return the Id of the created aggregate
/// </summary>
/// <param name="request"></param>
/// <param name="serviceProvider"></param>
/// <returns></returns>
public static async Task<Guid> Send(this CreationCommand request, IServiceProvider serviceProvider) =>
(await serviceProvider
.GetRequiredService<Mediator>()
.Send(request))
.Match(id => id,
domainError => throw new Exception(domainError.Message),
exception => throw exception);

/// <summary>
/// Send the creation command with assertion on the success result by default
/// </summary>
/// <param name="request"></param>
/// <param name="serviceProvider"></param>
/// <param name="withResultAssertion"></param>
/// <returns></returns>
public static async Task<Guid> Send(this CreationCommand request, IServiceProvider serviceProvider, bool withResultAssertion = true)
public static async Task<CreationResult> Send(this CreationCommand request, IServiceProvider serviceProvider, bool withResultAssertion)
{
var res = await serviceProvider.GetRequiredService<Mediator>().Send(request);

if (withResultAssertion)
res.IsSuccess.ShouldBe(true, $"{request.GetType().FullName}: {res}");
res.IsSuccess.ShouldBeTrue($"{request.GetType().FullName}: {res}");

return res.AggregateId;
return res;
}

/// <summary>
Expand All @@ -54,7 +68,7 @@ public static async Task<Guid> Send(this CreationCommand request, IServiceProvid
/// <param name="task"></param>
/// <typeparam name="TDomainError"></typeparam>
/// <exception cref="ArgumentNullException"></exception>
public static async Task ShouldHaveDomainErrorOfType<TDomainError>(this Task<CommandResultV2> task) where TDomainError : DomainError
public static async Task ShouldHaveDomainErrorOfType<TDomainError>(this Task<CommandResult> task) where TDomainError : DomainError
{
if (task == null) throw new ArgumentNullException(nameof(task));
(await task)
Expand All @@ -71,7 +85,7 @@ public static async Task ShouldHaveDomainErrorOfType<TDomainError>(this Task<Com
/// <param name="task"></param>
/// <typeparam name="TError"></typeparam>
/// <exception cref="ArgumentNullException"></exception>
public static async Task ShouldHavePanicExceptionOfType<TError>(this Task<CommandResultV2> task) where TError : Exception
public static async Task ShouldHavePanicExceptionOfType<TError>(this Task<CommandResult> task) where TError : Exception
{
if (task == null) throw new ArgumentNullException(nameof(task));
(await task)
Expand Down
2 changes: 1 addition & 1 deletion src/HerrGeneral.WriteSide/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace HerrGeneral.WriteSide;
/// <summary>
/// A command returning a CommandResultV2
/// </summary>
public abstract class Command : CommandBase<CommandResultV2>
public abstract class Command : CommandBase<CommandResult>
{
/// <summary>
/// Ctor
Expand Down
2 changes: 1 addition & 1 deletion src/HerrGeneral.WriteSide/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace HerrGeneral.WriteSide;
/// Handler for command returning a CommandResultV2
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public abstract class CommandHandler<TCommand> : CommandHandlerBase<TCommand, CommandResultV2>
public abstract class CommandHandler<TCommand> : CommandHandlerBase<TCommand, CommandResult>
where TCommand : Command
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Result of an handled command.
/// </summary>
public sealed class CommandResultV2 : IWithSuccess
public sealed record CommandResult : IWithSuccess
{
private readonly Exception? _panicException;
private readonly DomainError? _domainError;
Expand All @@ -15,30 +15,30 @@ public sealed class CommandResultV2 : IWithSuccess
/// </summary>
public bool IsSuccess => !IsDomainError && !IsPanicError;

private CommandResultV2() { }
private CommandResult() { }

private CommandResultV2(DomainError error) => _domainError = error;
private CommandResult(DomainError error) => _domainError = error;

private CommandResultV2(Exception panicException) => _panicException = panicException;
private CommandResult(Exception panicException) => _panicException = panicException;

/// <summary>
/// Factory for success.
/// </summary>
public static CommandResultV2 Success { get; } = new();
public static CommandResult Success { get; } = new();

/// <summary>
/// Factory for domain error.
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
public static CommandResultV2 DomainFail(DomainError error) => new(error);
public static CommandResult DomainFail(DomainError error) => new(error);

/// <summary>
/// Factory for panic exception.
/// </summary>
/// <param name="panicException"></param>
/// <returns></returns>
public static CommandResultV2 PanicFail(Exception panicException) => new(panicException);
public static CommandResult PanicFail(Exception panicException) => new(panicException);

/// <summary>
/// Evaluates a specified function, based on the .current state.
Expand Down Expand Up @@ -117,9 +117,9 @@ public void MatchPanicException(Action<Exception> onPanicException)
/// <summary>
/// True if success, else false.
/// </summary>
/// <param name="resultV2"></param>
/// <param name="result"></param>
/// <returns></returns>
public static implicit operator bool(CommandResultV2 resultV2) => resultV2.IsSuccess;
public static implicit operator bool(CommandResult result) => result.IsSuccess;

/// <summary>
/// Display the result
Expand Down
16 changes: 6 additions & 10 deletions src/HerrGeneral.WriteSide/CreationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
/// Result of an handled creation command
/// Contain the aggregate id
/// </summary>
public sealed class CreationResult : IWithSuccess
public sealed record CreationResult : IWithSuccess
{
/// <summary>
/// Id of the created aggregate
/// </summary>
public Guid AggregateId { get; }

private readonly Guid _aggregateId;
private readonly Exception? _panicException;
private readonly DomainError? _domainError;

Expand All @@ -22,7 +18,7 @@ public sealed class CreationResult : IWithSuccess
private bool IsPanicError => _panicException != null;

private CreationResult(Guid aggregateId) =>
AggregateId = aggregateId;
_aggregateId = aggregateId;

private CreationResult(DomainError error) =>
_domainError = error;
Expand Down Expand Up @@ -63,7 +59,7 @@ public TResult Match<TResult>(Func<Guid, TResult> onSuccess, Func<DomainError, T
if (onPanicError == null) throw new ArgumentNullException(nameof(onPanicError));

return IsSuccess
? onSuccess(AggregateId)
? onSuccess(_aggregateId)
: IsDomainError
? onDomainError(_domainError ?? throw new InvalidOperationException())
: onPanicError(_panicException ?? throw new InvalidOperationException());
Expand All @@ -81,7 +77,7 @@ public void Match(Action<Guid> onSuccess, Action<DomainError> onDomainError, Act
if (onDomainError == null) throw new ArgumentNullException(nameof(onDomainError));

if (IsSuccess)
onSuccess(AggregateId);
onSuccess(_aggregateId);
else if (IsDomainError)
onDomainError(_domainError ?? throw new InvalidOperationException());
else
Expand All @@ -96,7 +92,7 @@ public void MatchSuccess(Action<Guid> success)
{
if (success == null) throw new ArgumentNullException(nameof(success));

if (IsSuccess) success(AggregateId);
if (IsSuccess) success(_aggregateId);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions test/HerrGeneral.Test/Data/WriteSide/CreatePing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace HerrGeneral.Test.Data.WriteSide;

public class CreatePing : CreationCommand
{
public static readonly Guid AggregateId = Guid.NewGuid();

public string Message { get; set; } = string.Empty;

public class Handler : CreationCommandHandler<CreatePing>
Expand All @@ -14,8 +16,8 @@ public Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)

public override async Task<CreationResult> Handle(CreatePing command, CancellationToken cancellationToken)
{
await Publish(new Pong($"{command.Message} received", command.Id, Guid.NewGuid()), cancellationToken);
return CreationResult.Success(Guid.NewGuid());
await Publish(new Pong($"{command.Message} received", command.Id, AggregateId), cancellationToken);
return CreationResult.Success(AggregateId);
}

}
Expand Down
4 changes: 2 additions & 2 deletions test/HerrGeneral.Test/Data/WriteSide/Ping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)
{
}

public override async Task<CommandResultV2> Handle(Ping command, CancellationToken cancellationToken)
public override async Task<CommandResult> Handle(Ping command, CancellationToken cancellationToken)
{
await Publish(new Pong($"{command.Message} received", command.Id, Guid.NewGuid()), cancellationToken);
return CommandResultV2.Success;
return CommandResult.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)
{
}

public override async Task<CommandResultV2> Handle(PingWithFailureInCommandHandler command, CancellationToken cancellationToken)
public override async Task<CommandResult> Handle(PingWithFailureInCommandHandler command, CancellationToken cancellationToken)
{
await Publish(new Pong("Command received", command.Id, Guid.NewGuid()), cancellationToken);
throw new PingError().ToException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)
{
}

public override async Task<CommandResultV2> Handle(PingWithFailureInEventHandler command, CancellationToken cancellationToken)
public override async Task<CommandResult> Handle(PingWithFailureInEventHandler command, CancellationToken cancellationToken)
{
await Publish(new PongWithFailureInEventHandlerEvent(command.Id, Guid.NewGuid()), cancellationToken);
return CommandResultV2.Success;
return CommandResult.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)
{
}

public override async Task<CommandResultV2> Handle(PingWithPanicException command, CancellationToken cancellationToken)
public override async Task<CommandResult> Handle(PingWithPanicException command, CancellationToken cancellationToken)
{
await Publish(new Pong("Command received", command.Id, Guid.NewGuid()), cancellationToken);
throw new PanicException();
Expand Down
6 changes: 3 additions & 3 deletions test/HerrGeneral.Test/RegistrationShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ private class PingHandler : CommandHandler<Ping>
public PingHandler(Dependency dependency, IEventDispatcher eventDispatcher) : base(eventDispatcher) =>
_dependency = dependency;

public override Task<CommandResultV2> Handle(Ping command, CancellationToken cancellationToken)
public override Task<CommandResult> Handle(Ping command, CancellationToken cancellationToken)
{
_dependency.Called = true;
return Task.FromResult(CommandResultV2.Success);
return Task.FromResult(CommandResult.Success);
}
}

Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task Resolve_main_handler()

var response = await mediator.Send(new Ping { Message = "Ping" });

response.ShouldBe(CommandResultV2.Success);
response.ShouldBe(CommandResult.Success);
container.GetInstance<Dependency>().Called.ShouldBe(true);
}

Expand Down
8 changes: 4 additions & 4 deletions test/HerrGeneral.Test/ScannerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public Command1Handler(IEventDispatcher eventDispatcher) : base(eventDispatcher)
{
}

public override Task<CommandResultV2> Handle(Command1 command, CancellationToken cancellationToken) =>
Task.FromResult(CommandResultV2.Success);
public override Task<CommandResult> Handle(Command1 command, CancellationToken cancellationToken) =>
Task.FromResult(CommandResult.Success);
}
}

Expand All @@ -72,8 +72,8 @@ protected Command2HandlerBase(IEventDispatcher eventDispatcher) : base(eventDisp
{
}

public override Task<CommandResultV2> Handle(Command2 command, CancellationToken cancellationToken) =>
Task.FromResult(CommandResultV2.Success);
public override Task<CommandResult> Handle(Command2 command, CancellationToken cancellationToken) =>
Task.FromResult(CommandResult.Success);
}

private class Command2Handler : Command2HandlerBase
Expand Down
9 changes: 6 additions & 3 deletions test/HerrGeneral.Test/SendShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public SendShould(ITestOutputHelper output)

[Fact]
public async Task Resolve_main_handler() =>
(await new Ping { Message = "Ping" }.Send(_container, false))
.ShouldBe(CommandResultV2.Success);
(await new Ping { Message = "Ping" }
.Send(_container, false))
.ShouldBe(CommandResult.Success);

[Fact]
public async Task Resolve_main_handler_for_creation_command() =>
await new CreatePing { Message = "Ping" }.Send(_container);
(await new CreatePing { Message = "Ping" }
.Send(_container, false))
.ShouldBe(CreationResult.Success(CreatePing.AggregateId));

[Fact]
public async Task Dispatch_events_on_write_side()
Expand Down
2 changes: 1 addition & 1 deletion test/HerrGeneral.Test/UnitOfWorkShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Not_be_mandatory()


var result = await new Ping { Message = "Ping" }.Send(container, false);
result.ShouldBe(CommandResultV2.Success);
result.ShouldBe(CommandResult.Success);
}

private Container Register(IUnitOfWork unitOfWork) =>
Expand Down

0 comments on commit 724dd8c

Please sign in to comment.