Skip to content

Commit f6f2924

Browse files
Mediator rearchitecture (#163)
* . * . * . * . * .
1 parent 33fa73f commit f6f2924

File tree

32 files changed

+216
-339
lines changed

32 files changed

+216
-339
lines changed

src/OneBeyond.Studio.Application.SharedKernel.Tests/Authorization/AuthorizationRequirementHandlerTests.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ public async Task TestSimpleParameterlessRequirementHandlingSucceeds()
2727

2828
var command = new TestableCommands.Command1();
2929

30-
await mediator.CommandAsync<TestableCommands.Command1, bool>(command);
30+
await mediator.Send(command);
3131

32-
Assert.AreEqual(2, testableContainer.Count);
32+
Assert.HasCount(2, testableContainer);
3333
// Auth handler is executed first
3434
Assert.AreEqual(
3535
typeof(TestableAuthorizationRequirementHandlers.Requirement2Handler<TestableCommands.Command1>).FullName,
3636
testableContainer.Dequeue());
3737
// Command handler is executed last
3838
Assert.AreEqual(
39-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command1>).FullName,
39+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
4040
testableContainer.Dequeue());
4141
}
4242
}
@@ -52,16 +52,16 @@ public async Task TestRequirementHandlingSucceedsWhenHandlerDependsOnCommand()
5252

5353
var command2 = new TestableCommands.Command2();
5454

55-
await mediator.CommandAsync<TestableCommands.Command2, bool>(command2);
55+
await mediator.Send(command2);
5656

57-
Assert.AreEqual(2, testableContainer.Count);
57+
Assert.HasCount(2, testableContainer);
5858
// Appropriate (based on the command interface) auth handler is executed first
5959
Assert.AreEqual(
6060
typeof(TestableAuthorizationRequirementHandlers.Requirement2ViaSomething1Handler<TestableCommands.Command2>).FullName,
6161
testableContainer.Dequeue());
6262
// Command handler is executed last
6363
Assert.AreEqual(
64-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command2>).FullName,
64+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
6565
testableContainer.Dequeue());
6666
}
6767

@@ -73,16 +73,16 @@ public async Task TestRequirementHandlingSucceedsWhenHandlerDependsOnCommand()
7373

7474
var command3 = new TestableCommands.Command3();
7575

76-
await mediator.CommandAsync<TestableCommands.Command3, bool>(command3);
76+
await mediator.Send(command3);
7777

78-
Assert.AreEqual(2, testableContainer.Count);
78+
Assert.HasCount(2, testableContainer);
7979
// Appropriate (based on the command interface) auth handler is executed first
8080
Assert.AreEqual(
8181
typeof(TestableAuthorizationRequirementHandlers.Requirement2ViaSomething2Handler<TestableCommands.Command3>).FullName,
8282
testableContainer.Dequeue());
8383
// Command handler is executed last
8484
Assert.AreEqual(
85-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command3>).FullName,
85+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
8686
testableContainer.Dequeue());
8787
}
8888
}
@@ -98,9 +98,9 @@ public async Task TestPolicyRequirementsAreHandledByOrLogicAndPolicySucceedsEven
9898

9999
var command4 = new TestableCommands.Command4();
100100

101-
await mediator.CommandAsync<TestableCommands.Command4, bool>(command4);
101+
await mediator.Send(command4);
102102

103-
Assert.AreEqual(3, testableContainer.Count);
103+
Assert.HasCount(3, testableContainer);
104104
// First requirement handler is executed and fails
105105
Assert.AreEqual(
106106
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement1Handler<TestableCommands.Command4>).FullName}: {new TestableAuthorizationRequirements.Requirement1(true, 42, "Forty two")} - Failure",
@@ -111,7 +111,7 @@ public async Task TestPolicyRequirementsAreHandledByOrLogicAndPolicySucceedsEven
111111
testableContainer.Dequeue());
112112
// Command handler is executed last
113113
Assert.AreEqual(
114-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command4>).FullName,
114+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
115115
testableContainer.Dequeue());
116116
}
117117
}
@@ -127,17 +127,17 @@ public async Task TestPolicyRequirementsAreHandlerByOrLogicAndSecondRequirementN
127127

128128
var command5 = new TestableCommands.Command5();
129129

130-
await mediator.CommandAsync<TestableCommands.Command5, bool>(command5);
130+
await mediator.Send(command5);
131131

132-
Assert.AreEqual(2, testableContainer.Count);
132+
Assert.HasCount(2, testableContainer);
133133
// First requirement handler is executed and succeeds
134134
Assert.AreEqual(
135135
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement2Handler<TestableCommands.Command5>).FullName}",
136136
testableContainer.Dequeue());
137137
// Second requirement handler is not executed
138138
// Command handler is executed last
139139
Assert.AreEqual(
140-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command5>).FullName,
140+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
141141
testableContainer.Dequeue());
142142
}
143143
}
@@ -155,12 +155,12 @@ public async Task TestPolicyRequirementsAreHandledByOrLogicAndPolicyFailsWhenBot
155155

156156
try
157157
{
158-
await mediator.CommandAsync<TestableCommands.Command9, bool>(command9);
158+
await mediator.Send(command9);
159159
Assert.Fail();
160160
}
161161
catch (AuthorizationPolicyFailedException)
162162
{
163-
Assert.AreEqual(2, testableContainer.Count);
163+
Assert.HasCount(2, testableContainer);
164164
// First requirement handler is executed and fails
165165
Assert.AreEqual(
166166
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement1Handler<TestableCommands.Command9>).FullName}: {new TestableAuthorizationRequirements.Requirement1(true, 41, "Forty one")} - Failure",
@@ -185,9 +185,9 @@ public async Task TestPoliciesAreHandledByAndLogicAndEntireCheckSucceedsWhenBoth
185185

186186
var command6 = new TestableCommands.Command6();
187187

188-
await mediator.CommandAsync<TestableCommands.Command6, bool>(command6);
188+
await mediator.Send(command6);
189189

190-
Assert.AreEqual(3, testableContainer.Count);
190+
Assert.HasCount(3, testableContainer);
191191
// First requirement handler is executed and succeeds
192192
Assert.AreEqual(
193193
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement2Handler<TestableCommands.Command6>).FullName}",
@@ -198,7 +198,7 @@ public async Task TestPoliciesAreHandledByAndLogicAndEntireCheckSucceedsWhenBoth
198198
testableContainer.Dequeue());
199199
// Command handler is executed last
200200
Assert.AreEqual(
201-
typeof(TestableCommandHandlers.GenericCommandHandler<TestableCommands.Command6>).FullName,
201+
typeof(TestableCommandHandlers.GenericCommandHandler<IRequest<bool>>).FullName,
202202
testableContainer.Dequeue());
203203
}
204204
}
@@ -216,12 +216,12 @@ public async Task TestPoliciesAreHandledByAndLogicAndEntireCheckFailsWhenFirstPo
216216

217217
try
218218
{
219-
await mediator.CommandAsync<TestableCommands.Command7, bool>(command7);
219+
await mediator.Send(command7);
220220
Assert.Fail();
221221
}
222222
catch (AuthorizationPolicyFailedException)
223223
{
224-
Assert.AreEqual(1, testableContainer.Count);
224+
Assert.HasCount(1, testableContainer);
225225
// First requirement handler is executed and fails
226226
Assert.AreEqual(
227227
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement3Handler<TestableCommands.Command7>).FullName}: {new TestableAuthorizationRequirements.Requirement3(true)} - Failure",
@@ -245,12 +245,12 @@ public async Task TestPoliciesAreHandledByAndLogicAndEntireCheckFailsWhenFirstPo
245245

246246
try
247247
{
248-
await mediator.CommandAsync<TestableCommands.Command8, bool>(command8);
248+
await mediator.Send(command8);
249249
Assert.Fail();
250250
}
251251
catch (AuthorizationPolicyFailedException)
252252
{
253-
Assert.AreEqual(2, testableContainer.Count);
253+
Assert.HasCount(2, testableContainer);
254254
// First requirement handler is executed and succeeds
255255
Assert.AreEqual(
256256
$"{typeof(TestableAuthorizationRequirementHandlers.Requirement1Handler<TestableCommands.Command8>).FullName}: {new TestableAuthorizationRequirements.Requirement1(false, 45, "Forty five")} - Success",
@@ -276,7 +276,7 @@ public async Task TestRequestsWithoutPolicyAssignedToThemFail()
276276

277277
try
278278
{
279-
await mediator.CommandAsync<TestableCommands.Command10, bool>(command10);
279+
await mediator.Send(command10);
280280
}
281281
catch (AuthorizationPolicyMissingException exception)
282282
{
@@ -304,7 +304,7 @@ protected override void ConfigureTestServices(
304304

305305
containerBuilder.RegisterGeneric(typeof(TestableCommandHandlers.GenericCommandHandler<>))
306306
.AsImplementedInterfaces()
307-
.InstancePerLifetimeScope();
307+
.InstancePerLifetimeScope();
308308

309309
containerBuilder.AddAuthorizationRequirementHandlers(
310310
new SharedKernel.Authorization.AuthorizationOptions

src/OneBeyond.Studio.Application.SharedKernel.Tests/Authorization/TestableAuthorizationRequirementHandlers.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
using System.Threading.Tasks;
55
using EnsureThat;
66
using OneBeyond.Studio.Application.SharedKernel.Authorization;
7-
using OneBeyond.Studio.Core.Mediator.Commands;
7+
using OneBeyond.Studio.Core.Mediator;
88

99
namespace OneBeyond.Studio.Application.SharedKernel.Tests.Authorization;
1010

1111
internal static class TestableAuthorizationRequirementHandlers
1212
{
1313
public sealed class Requirement2Handler<TRequest>
1414
: IAuthorizationRequirementHandler<TestableAuthorizationRequirements.Requirement2, TRequest>
15-
where TRequest : ICommand<bool>
15+
where TRequest : IBaseRequest
1616
{
1717
private readonly Queue<string> _testableContainer;
1818

@@ -81,7 +81,7 @@ public Task HandleAsync(
8181

8282
public sealed class Requirement1Handler<TRequest>
8383
: IAuthorizationRequirementHandler<TestableAuthorizationRequirements.Requirement1, TRequest>
84-
where TRequest : ICommand<bool>
84+
where TRequest : IBaseRequest
8585
{
8686
private readonly Queue<string> _testableContainer;
8787

@@ -114,7 +114,7 @@ public Task HandleAsync(
114114

115115
public sealed class Requirement3Handler<TRequest>
116116
: IAuthorizationRequirementHandler<TestableAuthorizationRequirements.Requirement3, TRequest>
117-
where TRequest : ICommand<bool>
117+
where TRequest : IBaseRequest
118118
{
119119
private readonly Queue<string> _testableContainer;
120120

src/OneBeyond.Studio.Application.SharedKernel.Tests/Authorization/TestableCommandHandlers.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using EnsureThat;
5-
using OneBeyond.Studio.Core.Mediator.Commands;
5+
using OneBeyond.Studio.Core.Mediator;
66

77
namespace OneBeyond.Studio.Application.SharedKernel.Tests.Authorization;
88

99
internal static class TestableCommandHandlers
1010
{
1111
public sealed class GenericCommandHandler<TCommand>
12-
: ICommandHandler<TCommand, bool>
13-
where TCommand : ICommand<bool>
12+
: IRequestHandler<TCommand, bool>
13+
where TCommand : IRequest<bool>
1414
{
1515
private readonly Queue<string> _testableContainer;
1616

@@ -21,7 +21,7 @@ public GenericCommandHandler(Queue<string> testableContainer)
2121
_testableContainer = testableContainer;
2222
}
2323

24-
public Task<bool> HandleAsync(TCommand request, CancellationToken cancellationToken)
24+
public Task<bool> Handle(TCommand request, CancellationToken cancellationToken)
2525
{
2626
_testableContainer.Enqueue(GetType().FullName!);
2727
return Task.FromResult(true);
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,82 @@
11
using OneBeyond.Studio.Core.Mediator;
2-
using OneBeyond.Studio.Core.Mediator.Commands;
32
using OneBeyond.Studio.Domain.SharedKernel.Authorization;
43

54
namespace OneBeyond.Studio.Application.SharedKernel.Tests.Authorization;
65

76
internal static class TestableCommands
87
{
9-
public interface IRequestWithSomething1 : ICommand
8+
public interface IRequestWithSomething1 : IBaseRequest
109
{
1110
}
1211

13-
public interface IRequestWithSomething2 : ICommand
12+
public interface IRequestWithSomething2 : IBaseRequest
1413
{
1514
}
1615

1716
[AuthorizationPolicy(
1817
typeof(TestableAuthorizationRequirements.Requirement2))]
19-
public sealed record Command1 : ICommand<bool>
18+
public sealed record Command1 : IRequest<bool>
2019
{
2120
}
2221

2322
[AuthorizationPolicy(
2423
typeof(TestableAuthorizationRequirements.Requirement2))]
25-
public sealed record Command2 : ICommand<bool>, IRequestWithSomething1
24+
public sealed record Command2 : IRequest<bool>, IRequestWithSomething1
2625
{
2726
}
2827

2928
[AuthorizationPolicy(
3029
typeof(TestableAuthorizationRequirements.Requirement2))]
31-
public sealed record Command3 : ICommand<bool>, IRequestWithSomething2
30+
public sealed record Command3 : IRequest<bool>, IRequestWithSomething2
3231
{
3332
}
3433

3534
[AuthorizationPolicy(
3635
typeof(TestableAuthorizationRequirements.Requirement1), new object[] { true, 42, "Forty two" },
3736
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { false })]
38-
public sealed record Command4 : ICommand<bool>
37+
public sealed record Command4 : IRequest<bool>
3938
{
4039
}
4140

4241
[AuthorizationPolicy(
4342
typeof(TestableAuthorizationRequirements.Requirement2), new object[] { },
4443
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { false })]
45-
public sealed record Command5 : ICommand<bool>
44+
public sealed record Command5 : IRequest<bool>
4645
{
4746
}
4847

4948
[AuthorizationPolicy(
5049
typeof(TestableAuthorizationRequirements.Requirement1), new object[] { true, 41, "Forty one" },
5150
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { true })]
52-
public sealed record Command9 : ICommand<bool>
51+
public sealed record Command9 : IRequest<bool>
5352
{
5453
}
5554

5655
[AuthorizationPolicy(
5756
typeof(TestableAuthorizationRequirements.Requirement2))]
5857
[AuthorizationPolicy(
5958
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { false })]
60-
public sealed record Command6 : ICommand<bool>
59+
public sealed record Command6 : IRequest<bool>
6160
{
6261
}
6362

6463
[AuthorizationPolicy(
6564
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { true })]
6665
[AuthorizationPolicy(
6766
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { false })]
68-
public sealed record Command7 : ICommand<bool>
67+
public sealed record Command7 : IRequest<bool>
6968
{
7069
}
7170

7271
[AuthorizationPolicy(
7372
typeof(TestableAuthorizationRequirements.Requirement1), new object[] { false, 45, "Forty five" })]
7473
[AuthorizationPolicy(
7574
typeof(TestableAuthorizationRequirements.Requirement3), new object[] { true })]
76-
public sealed record Command8 : ICommand<bool>
75+
public sealed record Command8 : IRequest<bool>
7776
{
7877
}
7978

80-
public sealed record Command10 : ICommand<bool>
79+
public sealed record Command10 : IRequest<bool>
8180
{
8281
}
8382
}

0 commit comments

Comments
 (0)