renamed Behavior

This commit is contained in:
2025-02-02 11:51:34 +04:00
parent 54ea0925dd
commit d437d06c09
10 changed files with 126 additions and 115 deletions

View File

@@ -42,12 +42,12 @@ public class Dispatch
await commandHandler.Received(1).Handle(testCommand, CancellationToken.None);
}
public class TestOpenBehaviour<TRequest, TResponse> : IDispatchBehaviour<TRequest, TResponse>
public class TestOpenBehavior<TRequest, TResponse> : IDispatchBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly Action<TRequest> _callback;
public TestOpenBehaviour(Action<TRequest> callback)
public TestOpenBehavior(Action<TRequest> callback)
{
_callback = callback;
}
@@ -60,7 +60,7 @@ public class Dispatch
}
[Fact]
public async Task WhenPipelineConfigured_ShouldCallAllBehavioursInOrder()
public async Task WhenPipelineConfigured_ShouldCallAllBehaviorsInOrder()
{
// Given
var testCommand = new TestCommand();
@@ -72,15 +72,15 @@ public class Dispatch
.Returns(testCommandResult)
.AndDoes(_ => calls.Add("commandHandler"));
var firstBehaviour = Substitute.For<IDispatchBehaviour<TestCommand, TestCommandResult>>();
firstBehaviour.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
var firstBehavior = Substitute.For<IDispatchBehavior<TestCommand, TestCommandResult>>();
firstBehavior.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestCommandResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("firstBehaviour"));
.AndDoes(_ => calls.Add("firstBehavior"));
var secondBehaviour = Substitute.For<IDispatchBehaviour<TestCommand, TestCommandResult>>();
secondBehaviour.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
var secondBehavior = Substitute.For<IDispatchBehavior<TestCommand, TestCommandResult>>();
secondBehavior.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestCommandResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("secondBehaviour"));
.AndDoes(_ => calls.Add("secondBehavior"));
ServiceCollection serviceCollection =
[
@@ -90,22 +90,22 @@ public class Dispatch
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestCommand, TestCommandResult>),
(IServiceProvider _) => firstBehaviour,
typeof(IDispatchBehavior<TestCommand, TestCommandResult>),
(IServiceProvider _) => firstBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestCommand, TestCommandResult>),
(IServiceProvider _) => secondBehaviour,
typeof(IDispatchBehavior<TestCommand, TestCommandResult>),
(IServiceProvider _) => secondBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<,>),
typeof(TestOpenBehaviour<,>),
typeof(IDispatchBehavior<,>),
typeof(TestOpenBehavior<,>),
ServiceLifetime.Transient
),
];
serviceCollection.AddTransient<Action<TestCommand>>(_ => (TestCommand _) => calls.Add("thirdBehaviour"));
serviceCollection.AddTransient<Action<TestCommand>>(_ => (TestCommand _) => calls.Add("thirdBehavior"));
var services = serviceCollection.BuildServiceProvider();
var sut = new CommandDispatcherImpl(services, new ConcurrentMethodsCache());
@@ -115,11 +115,11 @@ public class Dispatch
// Then
result.ShouldBeSameAs(testCommandResult);
await firstBehaviour.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await secondBehaviour.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await firstBehavior.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await secondBehavior.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await commandHandler.Received(1).Handle(testCommand, CancellationToken.None);
calls.ShouldBe(["firstBehaviour", "secondBehaviour", "thirdBehaviour", "commandHandler"]);
calls.ShouldBe(["firstBehavior", "secondBehavior", "thirdBehavior", "commandHandler"]);
}
[Fact]
@@ -136,15 +136,15 @@ public class Dispatch
.Returns(testCommandResult)
.AndDoes(_ => calls.Add("commandHandler"));
var firstBehaviour = Substitute.For<IDispatchBehaviour<TestCommand, TestCommandResult>>();
firstBehaviour.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
var firstBehavior = Substitute.For<IDispatchBehavior<TestCommand, TestCommandResult>>();
firstBehavior.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestCommandResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("firstBehaviour"));
.AndDoes(_ => calls.Add("firstBehavior"));
var secondBehaviour = Substitute.For<IDispatchBehaviour<TestCommand, TestCommandResult>>();
secondBehaviour.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
var secondBehavior = Substitute.For<IDispatchBehavior<TestCommand, TestCommandResult>>();
secondBehavior.Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ValueTask.FromResult(testCommandResultAborted))
.AndDoes(_ => calls.Add("secondBehaviour"));
.AndDoes(_ => calls.Add("secondBehavior"));
ServiceCollection serviceCollection =
[
@@ -154,22 +154,22 @@ public class Dispatch
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestCommand, TestCommandResult>),
(IServiceProvider _) => firstBehaviour,
typeof(IDispatchBehavior<TestCommand, TestCommandResult>),
(IServiceProvider _) => firstBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestCommand, TestCommandResult>),
(IServiceProvider _) => secondBehaviour,
typeof(IDispatchBehavior<TestCommand, TestCommandResult>),
(IServiceProvider _) => secondBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<,>),
typeof(TestOpenBehaviour<,>),
typeof(IDispatchBehavior<,>),
typeof(TestOpenBehavior<,>),
ServiceLifetime.Transient
),
];
serviceCollection.AddTransient<Action<TestCommand>>(_ => (TestCommand _) => calls.Add("thirdBehaviour"));
serviceCollection.AddTransient<Action<TestCommand>>(_ => (TestCommand _) => calls.Add("thirdBehavior"));
var services = serviceCollection.BuildServiceProvider();
var sut = new CommandDispatcherImpl(services, new ConcurrentMethodsCache());
@@ -179,10 +179,10 @@ public class Dispatch
// Then
result.ShouldBeSameAs(testCommandResultAborted);
await firstBehaviour.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await secondBehaviour.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await firstBehavior.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await secondBehavior.Received(1).Handle(testCommand, Arg.Any<DispatchFurtherDelegate<TestCommandResult>>(), Arg.Any<CancellationToken>());
await commandHandler.Received(0).Handle(testCommand, CancellationToken.None);
calls.ShouldBe(["firstBehaviour", "secondBehaviour"]);
calls.ShouldBe(["firstBehavior", "secondBehavior"]);
}
}

View File

@@ -3,12 +3,12 @@ using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddBehaviour
public class AddBehavior
{
public class TestCommand {}
public class TestCommandResult {}
[ExcludeFromCodeCoverage]
public class NonGenericTestOpenBehaviour : IDispatchBehaviour<TestCommand, TestCommandResult>
public class NonGenericTestOpenBehavior : IDispatchBehavior<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> Handle(TestCommand request, DispatchFurtherDelegate<TestCommandResult> next, CancellationToken cancellationToken)
{
@@ -20,27 +20,27 @@ public class AddBehaviour
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterDispatchBehaviour(ServiceLifetime lifetime)
public void WhenCalled_ShouldRegisterDispatchBehavior(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddBehaviour<NonGenericTestOpenBehaviour>(lifetime));
.AddBehavior<NonGenericTestOpenBehavior>(lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IDispatchBehaviour<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(NonGenericTestOpenBehaviour)
descriptor.ServiceType == typeof(IDispatchBehavior<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(NonGenericTestOpenBehavior)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[ExcludeFromCodeCoverage]
public class InvalidTestBehaviour : IDispatchBehaviour
public class InvalidTestBehavior : IDispatchBehavior
{
public Type RequestType => throw new NotImplementedException();
@@ -57,7 +57,7 @@ public class AddBehaviour
// Then
Should.Throw<InvalidOperationException>(() => services.AddCqrs(opt => opt
.AddBehaviour<InvalidTestBehaviour>())
.AddBehavior<InvalidTestBehavior>())
);
}
}

View File

@@ -3,10 +3,10 @@ using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddOpenBehaviour
public class AddOpenBehavior
{
[ExcludeFromCodeCoverage]
public class TestOpenBehaviour<TRequest, TResponse> : IDispatchBehaviour<TRequest, TResponse>
public class TestOpenBehavior<TRequest, TResponse> : IDispatchBehavior<TRequest, TResponse>
where TRequest: notnull
{
public ValueTask<TResponse> Handle(TRequest request, DispatchFurtherDelegate<TResponse> next, CancellationToken cancellationToken)
@@ -19,27 +19,27 @@ public class AddOpenBehaviour
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterOpenDispatchBehaviour(ServiceLifetime lifetime)
public void WhenCalled_ShouldRegisterOpenDispatchBehavior(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddOpenBehaviour(typeof(TestOpenBehaviour<,>), lifetime));
.AddOpenBehavior(typeof(TestOpenBehavior<,>), lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IDispatchBehaviour<,>)
&& descriptor.ImplementationType == typeof(TestOpenBehaviour<,>)
descriptor.ServiceType == typeof(IDispatchBehavior<,>)
&& descriptor.ImplementationType == typeof(TestOpenBehavior<,>)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[ExcludeFromCodeCoverage]
public class InvalidOpenBehaviour : IDispatchBehaviour
public class InvalidOpenBehavior : IDispatchBehavior
{
public Type RequestType => throw new NotImplementedException();
@@ -53,18 +53,18 @@ public class AddOpenBehaviour
ServiceCollection services = new();
// When
var invalidOpenDispatchBehaviourType = typeof(InvalidOpenBehaviour);
var invalidOpenDispatchBehaviorType = typeof(InvalidOpenBehavior);
// Then
Should.Throw<ArgumentException>(() => services.AddCqrs(opt => opt
.AddOpenBehaviour(invalidOpenDispatchBehaviourType))
.AddOpenBehavior(invalidOpenDispatchBehaviorType))
);
}
public class TestCommand {}
public class TestCommandResult {}
[ExcludeFromCodeCoverage]
public class NonGenericTestOpenBehaviour : IDispatchBehaviour<TestCommand, TestCommandResult>
public class NonGenericTestOpenBehavior : IDispatchBehavior<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> Handle(TestCommand request, DispatchFurtherDelegate<TestCommandResult> next, CancellationToken cancellationToken)
{
@@ -79,11 +79,11 @@ public class AddOpenBehaviour
ServiceCollection services = new();
// When
var nonGenericOpenDispatchBehaviourType = typeof(NonGenericTestOpenBehaviour);
var nonGenericOpenDispatchBehaviorType = typeof(NonGenericTestOpenBehavior);
// Then
Should.Throw<ArgumentException>(() => services.AddCqrs(opt => opt
.AddOpenBehaviour(nonGenericOpenDispatchBehaviourType))
.AddOpenBehavior(nonGenericOpenDispatchBehaviorType))
);
}
}

View File

@@ -42,12 +42,12 @@ public class Dispatch
await queryHandler.Received(1).Handle(testQuery, CancellationToken.None);
}
public class TestOpenBehaviour<TRequest, TResponse> : IDispatchBehaviour<TRequest, TResponse>
public class TestOpenBehavior<TRequest, TResponse> : IDispatchBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly Action<TRequest> _callback;
public TestOpenBehaviour(Action<TRequest> callback)
public TestOpenBehavior(Action<TRequest> callback)
{
_callback = callback;
}
@@ -60,7 +60,7 @@ public class Dispatch
}
[Fact]
public async Task WhenPipelineConfigured_ShouldCallAllBehavioursInOrder()
public async Task WhenPipelineConfigured_ShouldCallAllBehaviorsInOrder()
{
// Given
var testQuery = new TestQuery();
@@ -72,15 +72,15 @@ public class Dispatch
.Returns(testQueryResult)
.AndDoes(_ => calls.Add("queryHandler"));
var firstBehaviour = Substitute.For<IDispatchBehaviour<TestQuery, TestQueryResult>>();
firstBehaviour.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
var firstBehavior = Substitute.For<IDispatchBehavior<TestQuery, TestQueryResult>>();
firstBehavior.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestQueryResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("firstBehaviour"));
.AndDoes(_ => calls.Add("firstBehavior"));
var secondBehaviour = Substitute.For<IDispatchBehaviour<TestQuery, TestQueryResult>>();
secondBehaviour.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
var secondBehavior = Substitute.For<IDispatchBehavior<TestQuery, TestQueryResult>>();
secondBehavior.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestQueryResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("secondBehaviour"));
.AndDoes(_ => calls.Add("secondBehavior"));
ServiceCollection serviceCollection =
[
@@ -90,22 +90,22 @@ public class Dispatch
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestQuery, TestQueryResult>),
(IServiceProvider _) => firstBehaviour,
typeof(IDispatchBehavior<TestQuery, TestQueryResult>),
(IServiceProvider _) => firstBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestQuery, TestQueryResult>),
(IServiceProvider _) => secondBehaviour,
typeof(IDispatchBehavior<TestQuery, TestQueryResult>),
(IServiceProvider _) => secondBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<,>),
typeof(TestOpenBehaviour<,>),
typeof(IDispatchBehavior<,>),
typeof(TestOpenBehavior<,>),
ServiceLifetime.Transient
),
];
serviceCollection.AddTransient<Action<TestQuery>>(_ => (TestQuery _) => calls.Add("thirdBehaviour"));
serviceCollection.AddTransient<Action<TestQuery>>(_ => (TestQuery _) => calls.Add("thirdBehavior"));
var services = serviceCollection.BuildServiceProvider();
var sut = new QueryDispatcherImpl(services, new ConcurrentMethodsCache());
@@ -115,11 +115,11 @@ public class Dispatch
// Then
result.ShouldBeSameAs(testQueryResult);
await firstBehaviour.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await secondBehaviour.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await firstBehavior.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await secondBehavior.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await queryHandler.Received(1).Handle(testQuery, CancellationToken.None);
calls.ShouldBe(["firstBehaviour", "secondBehaviour", "thirdBehaviour", "queryHandler"]);
calls.ShouldBe(["firstBehavior", "secondBehavior", "thirdBehavior", "queryHandler"]);
}
[Fact]
@@ -136,15 +136,15 @@ public class Dispatch
.Returns(testQueryResult)
.AndDoes(_ => calls.Add("queryHandler"));
var firstBehaviour = Substitute.For<IDispatchBehaviour<TestQuery, TestQueryResult>>();
firstBehaviour.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
var firstBehavior = Substitute.For<IDispatchBehavior<TestQuery, TestQueryResult>>();
firstBehavior.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ((DispatchFurtherDelegate<TestQueryResult>)args[1]).Invoke())
.AndDoes(_ => calls.Add("firstBehaviour"));
.AndDoes(_ => calls.Add("firstBehavior"));
var secondBehaviour = Substitute.For<IDispatchBehaviour<TestQuery, TestQueryResult>>();
secondBehaviour.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
var secondBehavior = Substitute.For<IDispatchBehavior<TestQuery, TestQueryResult>>();
secondBehavior.Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>())
.Returns(args => ValueTask.FromResult(testQueryResultAborted))
.AndDoes(_ => calls.Add("secondBehaviour"));
.AndDoes(_ => calls.Add("secondBehavior"));
ServiceCollection serviceCollection =
[
@@ -154,22 +154,22 @@ public class Dispatch
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestQuery, TestQueryResult>),
(IServiceProvider _) => firstBehaviour,
typeof(IDispatchBehavior<TestQuery, TestQueryResult>),
(IServiceProvider _) => firstBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<TestQuery, TestQueryResult>),
(IServiceProvider _) => secondBehaviour,
typeof(IDispatchBehavior<TestQuery, TestQueryResult>),
(IServiceProvider _) => secondBehavior,
ServiceLifetime.Transient
),
new ServiceDescriptor(
typeof(IDispatchBehaviour<,>),
typeof(TestOpenBehaviour<,>),
typeof(IDispatchBehavior<,>),
typeof(TestOpenBehavior<,>),
ServiceLifetime.Transient
),
];
serviceCollection.AddTransient<Action<TestQuery>>(_ => (TestQuery _) => calls.Add("thirdBehaviour"));
serviceCollection.AddTransient<Action<TestQuery>>(_ => (TestQuery _) => calls.Add("thirdBehavior"));
var services = serviceCollection.BuildServiceProvider();
var sut = new QueryDispatcherImpl(services, new ConcurrentMethodsCache());
@@ -179,10 +179,10 @@ public class Dispatch
// Then
result.ShouldBeSameAs(testQueryResultAborted);
await firstBehaviour.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await secondBehaviour.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await firstBehavior.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await secondBehavior.Received(1).Handle(testQuery, Arg.Any<DispatchFurtherDelegate<TestQueryResult>>(), Arg.Any<CancellationToken>());
await queryHandler.Received(0).Handle(testQuery, CancellationToken.None);
calls.ShouldBe(["firstBehaviour", "secondBehaviour"]);
calls.ShouldBe(["firstBehavior", "secondBehavior"]);
}
}