-
Notifications
You must be signed in to change notification settings - Fork 2
/
Without.cs
74 lines (59 loc) · 1.67 KB
/
Without.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Xunit;
namespace WithoutMediatR.RequestResponseMultipleDispatchWithReduce;
file record SomeRequest(string Message);
file interface IMyValidator
{
bool Validate(SomeRequest request);
}
file class ValidatorA : IMyValidator
{
bool IMyValidator.Validate(SomeRequest request)
{
// domain logic
return true;
}
}
file class ValidatorB : IMyValidator
{
bool IMyValidator.Validate(SomeRequest request)
{
// domain logic
return false;
}
}
file class Validators : IMyValidator
{
private readonly IEnumerable<IMyValidator> _handlers;
internal Validators(IEnumerable<IMyValidator> handlers)
{
_handlers = handlers;
}
bool IMyValidator.Validate(SomeRequest request) =>
_handlers
.Select(h => h.Validate(request)) // map
.Aggregate((acc, i) => acc && i); // reduce
}
file class Client
{
private readonly IMyValidator _validator;
internal Client(IMyValidator validator)
{
_validator = validator;
}
internal string DispatchToAll(SomeRequest request) =>
_validator.Validate(request)
? "all good!"
: "sorry, the request is not valid";
}
public class Without
{
[Fact]
void dispatch_all_requests()
{
var validators = new Validators(
new IMyValidator[]{ new ValidatorA(), new ValidatorB()});
var client = new Client(validators);
var response = client.DispatchToAll(new SomeRequest("my message"));
Assert.Equal("sorry, the request is not valid", response);
}
}