Refactor ICommand
Rename DuplicateCommand to ExistingCommand
This commit is contained in:
parent
f60d161ce7
commit
f013cb1dff
@ -7,13 +7,13 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace Ordering.API.Infrastructure.Behaviors
|
namespace Ordering.API.Infrastructure.Behaviors
|
||||||
{
|
{
|
||||||
public class DuplicateCommandBehavior<TCommand, TResponse> : IPipelineBehavior<TCommand, TResponse>
|
public class ExistingCommandBehavior<TCommand, TResponse> : IPipelineBehavior<TCommand, TResponse>
|
||||||
where TCommand : ICommand
|
where TCommand : ICommand
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
private readonly IRequestManager _requestManager;
|
private readonly IRequestManager _requestManager;
|
||||||
|
|
||||||
public DuplicateCommandBehavior(IMediator mediator, IRequestManager requestManager)
|
public ExistingCommandBehavior(IMediator mediator, IRequestManager requestManager)
|
||||||
{
|
{
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
_requestManager = requestManager;
|
_requestManager = requestManager;
|
||||||
@ -24,8 +24,8 @@ namespace Ordering.API.Infrastructure.Behaviors
|
|||||||
var alreadyExists = await _requestManager.ExistAsync(command.CommandId);
|
var alreadyExists = await _requestManager.ExistAsync(command.CommandId);
|
||||||
if (alreadyExists)
|
if (alreadyExists)
|
||||||
{
|
{
|
||||||
var duplicateCommand = new DuplicateCommandResponse<TCommand, TResponse>.DuplicateCommand<TCommand>(command);
|
var existingCommand = new ExistingCommandResponse<TCommand, TResponse>.ExistingCommand(command);
|
||||||
return await _mediator.Send(duplicateCommand, cancellationToken);
|
return await _mediator.Send(existingCommand, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _requestManager.CreateRequestForCommandAsync<TCommand>(command.CommandId);
|
await _requestManager.CreateRequestForCommandAsync<TCommand>(command.CommandId);
|
@ -8,7 +8,7 @@ using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
|||||||
|
|
||||||
namespace Ordering.API.Application.Commands
|
namespace Ordering.API.Application.Commands
|
||||||
{
|
{
|
||||||
public class CancelOrderCommand : ICommand, IRequest<bool>
|
public class CancelOrderCommand : ICommand<bool>
|
||||||
{
|
{
|
||||||
public Guid CommandId { get; set; }
|
public Guid CommandId { get; set; }
|
||||||
|
|
||||||
|
@ -38,9 +38,9 @@ namespace Ordering.API.Application.Commands
|
|||||||
|
|
||||||
|
|
||||||
// Use for Idempotency in Command process
|
// Use for Idempotency in Command process
|
||||||
public class CancelOrderDuplicateCommand : DuplicateCommandResponse<CancelOrderCommand, bool>
|
public class CancelOrderExistingCommand : ExistingCommandResponse<CancelOrderCommand, bool>
|
||||||
{
|
{
|
||||||
protected override Task<bool> CreateResponseForDuplicateCommand(CancelOrderCommand command)
|
protected override Task<bool> CreateResponseForExistingCommand(CancelOrderCommand command)
|
||||||
{
|
{
|
||||||
// Ignore duplicate requests for processing order.
|
// Ignore duplicate requests for processing order.
|
||||||
return Task.FromResult(true);
|
return Task.FromResult(true);
|
||||||
|
@ -47,9 +47,9 @@
|
|||||||
|
|
||||||
|
|
||||||
// Use for Idempotency in Command process
|
// Use for Idempotency in Command process
|
||||||
public class CreateOrderDuplicateCommand : DuplicateCommandResponse<CreateOrderCommand, bool>
|
public class CreateOrderExistingCommand : ExistingCommandResponse<CreateOrderCommand, bool>
|
||||||
{
|
{
|
||||||
protected override Task<bool> CreateResponseForDuplicateCommand(CreateOrderCommand command)
|
protected override Task<bool> CreateResponseForExistingCommand(CreateOrderCommand command)
|
||||||
{
|
{
|
||||||
// Ignore duplicate requests for creating order.
|
// Ignore duplicate requests for creating order.
|
||||||
return Task.FromResult(true);
|
return Task.FromResult(true);
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediatR;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
|
||||||
{
|
|
||||||
public abstract class DuplicateCommandResponse<TCommand, TResponse> : IRequestHandler<DuplicateCommandResponse<TCommand, TResponse>.DuplicateCommand<TCommand>, TResponse>
|
|
||||||
{
|
|
||||||
public class DuplicateCommand<TCommand> : IRequest<TResponse>
|
|
||||||
{
|
|
||||||
public DuplicateCommand(TCommand command)
|
|
||||||
{
|
|
||||||
Command = command;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TCommand Command { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates the result value to return if a previous request was found
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected abstract Task<TResponse> CreateResponseForDuplicateCommand(TCommand command);
|
|
||||||
|
|
||||||
Task<TResponse> IRequestHandler<DuplicateCommand<TCommand>, TResponse>.Handle(DuplicateCommand<TCommand> request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return CreateResponseForDuplicateCommand(request.Command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,30 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||||
|
{
|
||||||
|
public abstract class ExistingCommandResponse<TCommand, TResponse> : IRequestHandler<ExistingCommandResponse<TCommand, TResponse>.ExistingCommand, TResponse>
|
||||||
|
{
|
||||||
|
public class ExistingCommand : IRequest<TResponse>
|
||||||
|
{
|
||||||
|
public ExistingCommand(TCommand command)
|
||||||
|
{
|
||||||
|
Command = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TCommand Command { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the result value to return if a previous request was found
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected abstract Task<TResponse> CreateResponseForExistingCommand(TCommand command);
|
||||||
|
|
||||||
|
Task<TResponse> IRequestHandler<ExistingCommand, TResponse>.Handle(ExistingCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return CreateResponseForExistingCommand(request.Command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||||
{
|
{
|
||||||
@ -6,4 +7,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
|||||||
{
|
{
|
||||||
Guid CommandId { get; }
|
Guid CommandId { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ICommand<out TResponse> : ICommand, IRequest<TResponse>
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
@ -38,9 +38,9 @@ namespace Ordering.API.Application.Commands
|
|||||||
|
|
||||||
|
|
||||||
// Use for Idempotency in Command process
|
// Use for Idempotency in Command process
|
||||||
public class ShipOrderDuplicateCommand : DuplicateCommandResponse<ShipOrderCommand, bool>
|
public class ShipOrderExistingCommand : ExistingCommandResponse<ShipOrderCommand, bool>
|
||||||
{
|
{
|
||||||
protected override Task<bool> CreateResponseForDuplicateCommand(ShipOrderCommand command)
|
protected override Task<bool> CreateResponseForExistingCommand(ShipOrderCommand command)
|
||||||
{
|
{
|
||||||
// Ignore duplicate requests for creating order.
|
// Ignore duplicate requests for creating order.
|
||||||
return Task.FromResult(true);
|
return Task.FromResult(true);
|
||||||
|
@ -42,7 +42,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof
|
|||||||
|
|
||||||
builder.RegisterGeneric(typeof(LoggingBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
builder.RegisterGeneric(typeof(LoggingBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
||||||
builder.RegisterGeneric(typeof(ValidatorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
builder.RegisterGeneric(typeof(ValidatorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
||||||
builder.RegisterGeneric(typeof(DuplicateCommandBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
builder.RegisterGeneric(typeof(ExistingCommandBehavior<,>)).As(typeof(IPipelineBehavior<,>));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user