Browse Source

Custom command middlewares.

pull/390/head
Sebastian Stehle 7 years ago
parent
commit
a7b2dbb094
  1. 41
      src/Squidex.Infrastructure/Commands/CustomCommandMiddlewareRunner.cs
  2. 13
      src/Squidex.Infrastructure/Commands/ICustomCommandMiddleware.cs
  3. 3
      src/Squidex/Config/Domain/EntitiesServices.cs
  4. 72
      tests/Squidex.Infrastructure.Tests/Commands/CustomCommandMiddlewareRunnerTests.cs

41
src/Squidex.Infrastructure/Commands/CustomCommandMiddlewareRunner.cs

@ -0,0 +1,41 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Squidex.Infrastructure.Commands
{
public sealed class CustomCommandMiddlewareRunner : ICommandMiddleware
{
private readonly IEnumerable<ICustomCommandMiddleware> extensions;
public CustomCommandMiddlewareRunner(IEnumerable<ICustomCommandMiddleware> extensions)
{
Guard.NotNull(extensions, nameof(extensions));
this.extensions = extensions.Reverse().ToList();
}
public async Task HandleAsync(CommandContext context, Func<Task> next)
{
foreach (var handler in extensions)
{
next = Join(handler, context, next);
}
await next();
}
private static Func<Task> Join(ICommandMiddleware handler, CommandContext context, Func<Task> next)
{
return () => handler.HandleAsync(context, next);
}
}
}

13
src/Squidex.Infrastructure/Commands/ICustomCommandMiddleware.cs

@ -0,0 +1,13 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
namespace Squidex.Infrastructure.Commands
{
public interface ICustomCommandMiddleware : ICommandMiddleware
{
}
}

3
src/Squidex/Config/Domain/EntitiesServices.cs

@ -227,6 +227,9 @@ namespace Squidex.Config.Domain
services.AddSingletonAs<EnrichWithSchemaIdCommandMiddleware>()
.As<ICommandMiddleware>();
services.AddSingletonAs<CustomCommandMiddlewareRunner>()
.As<ICommandMiddleware>();
services.AddSingletonAs<InviteUserCommandMiddleware>()
.As<ICommandMiddleware>();

72
tests/Squidex.Infrastructure.Tests/Commands/CustomCommandMiddlewareRunnerTests.cs

@ -0,0 +1,72 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FakeItEasy;
using Xunit;
namespace Squidex.Infrastructure.Commands
{
public class CustomCommandMiddlewareRunnerTests
{
public sealed class Command : ICommand
{
public List<int> Values { get; set; } = new List<int>();
public long ExpectedVersion { get; set; }
}
public sealed class CustomMiddleware : ICustomCommandMiddleware
{
private readonly int value;
public CustomMiddleware(int value)
{
this.value = value;
}
public Task HandleAsync(CommandContext context, Func<Task> next)
{
if (context.Command is Command command)
{
command.Values.Add(value);
}
return next();
}
}
[Fact]
public async Task Should_run_extensions_in_right_order()
{
var command = new Command();
var context = new CommandContext(command, A.Fake<ICommandBus>());
var sut = new CustomCommandMiddlewareRunner(new[]
{
new CustomMiddleware(10),
new CustomMiddleware(12),
new CustomMiddleware(14)
});
var isNextCalled = false;
await sut.HandleAsync(context, () =>
{
isNextCalled = true;
Assert.Equal(new[] { 10, 12, 14 }, command.Values);
return Task.CompletedTask;
});
Assert.True(isNextCalled);
}
}
}
Loading…
Cancel
Save