-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
248 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,66 @@ | ||
using MediatR; | ||
using ErrorOr; | ||
|
||
using MediatR; | ||
using MediatR.Chained; | ||
|
||
internal class App(IMediator mediator) | ||
{ | ||
public async Task RunAsync() | ||
{ | ||
Console.WriteLine("Normal:"); | ||
|
||
await mediator.Send(new TestCommand1("Hello")); | ||
await mediator.Send(new TestCommand2("Hello", "World")); | ||
await mediator.Send(new TestCommand3("Hello", "World", "Again")); | ||
|
||
Console.WriteLine("Chained:"); | ||
|
||
await mediator | ||
IErrorOr? result = await mediator | ||
.Add(new TestCommand1("Hello")) | ||
.FailWhen(x => x.IsError) | ||
.Add(x => new TestCommand2(x.Value, "World")) | ||
.Add(x => new TestCommand3(x.Value.Item1, x.Value.Item2, "Again")) | ||
.SendAsync<IErrorOr>(); | ||
|
||
Console.WriteLine(result!.IsError); | ||
|
||
object? result2 = await mediator | ||
.Add(new TestCommand1("Hello")) | ||
.Add(x => new TestCommand2(x, "World")) | ||
.Add(x => new TestCommand3(x.Item1, x.Item2, "Again")) | ||
.Add(x => new TestCommand2(x.Value, "World")) | ||
.Add(x => new TestCommand3(x.Value.Item1, x.Value.Item2, "Again")) | ||
.SendAsync(); | ||
|
||
Console.WriteLine(result2); | ||
} | ||
} | ||
|
||
public record TestCommand1(string Param1) : IRequest<string>; | ||
public record TestCommand2(string Param1, string Param2) : IRequest<(string, string)>; | ||
public record TestCommand3(string Param1, string Param2, string Param3) : IRequest<(string, string, string)>; | ||
public record TestCommand1(string Param1) : IRequest<ErrorOr<string>>; | ||
public record TestCommand2(string Param1, string Param2) : IRequest<ErrorOr<(string, string)>>; | ||
public record TestCommand3(string Param1, string Param2, string Param3) : IRequest<ErrorOr<(string, string, string)>>; | ||
|
||
internal class Command1Handler : IRequestHandler<TestCommand1, string> | ||
internal class Command1Handler : IRequestHandler<TestCommand1, ErrorOr<string>> | ||
{ | ||
public Task<string> Handle(TestCommand1 request, CancellationToken cancellationToken) | ||
public async Task<ErrorOr<string>> Handle(TestCommand1 request, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"Command1Handler: {request.Param1}"); | ||
return Task.FromResult(request.Param1); | ||
return await Task.FromResult(Error.Failure("test")); | ||
} | ||
} | ||
|
||
internal class Command2Handler : IRequestHandler<TestCommand2, (string, string)> | ||
internal class Command2Handler : IRequestHandler<TestCommand2, ErrorOr<(string, string)>> | ||
{ | ||
public Task<(string, string)> Handle(TestCommand2 request, CancellationToken cancellationToken) | ||
public async Task<ErrorOr<(string, string)>> Handle(TestCommand2 request, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"Command2Handler: {request.Param1}, {request.Param2}"); | ||
return Task.FromResult((request.Param1, request.Param2)); | ||
return await Task.FromResult((request.Param1, request.Param2)); | ||
} | ||
} | ||
|
||
internal class Command3Handler : IRequestHandler<TestCommand3, (string, string, string)> | ||
internal class Command3Handler : IRequestHandler<TestCommand3, ErrorOr<(string, string, string)>> | ||
{ | ||
public Task<(string, string, string)> Handle(TestCommand3 request, CancellationToken cancellationToken) | ||
public async Task<ErrorOr<(string, string, string)>> Handle(TestCommand3 request, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"Command3Handler: {request.Param1}, {request.Param2}, {request.Param3}"); | ||
return Task.FromResult((request.Param1, request.Param2, request.Param3)); | ||
return await Task.FromResult((request.Param1, request.Param2, request.Param3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.