Skip to content

Commit 810e841

Browse files
authored
Add tests and fix Command with response (#3)
* Add middleware support (#1) Add functionality for configuring and executing custom middlewares before or after commands. * update example * update example * Add tests and fix Command with response Command with response when middleware configured was not working properly
1 parent c2128b8 commit 810e841

File tree

21 files changed

+341
-6
lines changed

21 files changed

+341
-6
lines changed

OpenMediator.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{18DC
1414
.github\workflows\open-mediator-nuget-package.yml = .github\workflows\open-mediator-nuget-package.yml
1515
EndProjectSection
1616
EndProject
17+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{00CDF08F-6557-44E0-9B54-D0FC0CB1879A}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Integration.Test", "test\OpenMediator.Integration.Test\OpenMediator.Integration.Test.csproj", "{D06610C3-70D7-48FD-9075-A40CA685B58A}"
20+
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Shared.Integration.Test", "test\OpenMediator.Shared.Integration.Test\OpenMediator.Shared.Integration.Test.csproj", "{2436B77B-FA64-4DF2-A93D-DA175B411C44}"
22+
EndProject
1723
Global
1824
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1925
Debug|Any CPU = Debug|Any CPU
@@ -24,12 +30,22 @@ Global
2430
{A45D5C4D-8957-4619-868F-6B6F7AC4E42A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2531
{A45D5C4D-8957-4619-868F-6B6F7AC4E42A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2632
{A45D5C4D-8957-4619-868F-6B6F7AC4E42A}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{D06610C3-70D7-48FD-9075-A40CA685B58A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{D06610C3-70D7-48FD-9075-A40CA685B58A}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{D06610C3-70D7-48FD-9075-A40CA685B58A}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{D06610C3-70D7-48FD-9075-A40CA685B58A}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{2436B77B-FA64-4DF2-A93D-DA175B411C44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{2436B77B-FA64-4DF2-A93D-DA175B411C44}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{2436B77B-FA64-4DF2-A93D-DA175B411C44}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{2436B77B-FA64-4DF2-A93D-DA175B411C44}.Release|Any CPU.Build.0 = Release|Any CPU
2741
EndGlobalSection
2842
GlobalSection(SolutionProperties) = preSolution
2943
HideSolutionNode = FALSE
3044
EndGlobalSection
3145
GlobalSection(NestedProjects) = preSolution
3246
{A45D5C4D-8957-4619-868F-6B6F7AC4E42A} = {185D813C-7310-4B27-A1B8-5DE91B7084FA}
3347
{18DC3938-D752-44C5-A4A3-23BED5C157C9} = {8F49E2C3-A3E4-4114-A8D6-7FF7CC4E66EC}
48+
{D06610C3-70D7-48FD-9075-A40CA685B58A} = {00CDF08F-6557-44E0-9B54-D0FC0CB1879A}
49+
{2436B77B-FA64-4DF2-A93D-DA175B411C44} = {00CDF08F-6557-44E0-9B54-D0FC0CB1879A}
3450
EndGlobalSection
3551
EndGlobal

examples/WebApplicationAPI/Controllers/UserController.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Microsoft.AspNetCore.Mvc;
22
using OpenMediator.Buses;
3-
using WebApplicationAPI.UseCases;
3+
using WebApplicationAPI.Domain;
4+
using WebApplicationAPI.UseCases.CreateUser;
5+
using WebApplicationAPI.UseCases.GetUser;
6+
using WebApplicationAPI.ViewModels;
47

58
namespace WebApplicationAPI.Controllers;
69

@@ -13,7 +16,14 @@ public async Task<IActionResult> CreateUser()
1316
{
1417
var command = new CreateUserCommand(1, "UserTest");
1518
await _mediator.SendAsync(command);
16-
1719
return Ok();
1820
}
21+
22+
[HttpGet("{id:int}")]
23+
public async Task<IActionResult> GetUser(int id)
24+
{
25+
var command = new GetUserCommand(id);
26+
var user = await _mediator.SendAsync<GetUserCommand, User>(command);
27+
return Ok(UserViewModel.FromUser(user));
28+
}
1929
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace WebApplicationAPI.Domain;
2+
3+
public sealed class User
4+
{
5+
public User(int id, string name, string email)
6+
{
7+
Id = id;
8+
Name = name;
9+
Email = email;
10+
}
11+
12+
public int Id { get; private set; }
13+
public string Name { get; private set; }
14+
public string Email { get; private set; } = string.Empty;
15+
}

examples/WebApplicationAPI/MediatorMiddleware/CustomMediatorMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ public async Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next)
1717
// Do something after the command
1818
_logger.LogInformation("=========> After command: {Command}", command.GetType().Name);
1919
Task.Delay(1000).Wait();
20-
2120
}
2221
}

examples/WebApplicationAPI/UseCases/CreateUserCommand.cs renamed to examples/WebApplicationAPI/UseCases/CreateUser/CreateUserCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using OpenMediator;
22

3-
namespace WebApplicationAPI.UseCases;
3+
namespace WebApplicationAPI.UseCases.CreateUser;
44

55
public record CreateUserCommand(int Id, string Name) : ICommand;

examples/WebApplicationAPI/UseCases/CreateUserCommandHandler.cs renamed to examples/WebApplicationAPI/UseCases/CreateUser/CreateUserCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using OpenMediator;
22

3-
namespace WebApplicationAPI.UseCases;
3+
namespace WebApplicationAPI.UseCases.CreateUser;
44

55
public record CreateUserCommandHandler(ILogger<CreateUserCommandHandler> _logger) : ICommandHandler<CreateUserCommand>
66
{
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using OpenMediator;
2+
using WebApplicationAPI.Domain;
3+
4+
namespace WebApplicationAPI.UseCases.GetUser;
5+
6+
public record GetUserCommand(int Id) : ICommand<User>;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using OpenMediator;
2+
using WebApplicationAPI.Domain;
3+
4+
namespace WebApplicationAPI.UseCases.GetUser;
5+
6+
public record GetUserCommandHandler(ILogger<GetUserCommandHandler> _logger) : ICommandHandler<GetUserCommand, User>
7+
{
8+
public async Task<User> HandleAsync(GetUserCommand command)
9+
{
10+
// Simulate some work
11+
_logger.LogInformation("=========> Handling command: {Command}", command.GetType().Name);
12+
await Task.Delay(1000);
13+
14+
return new User(
15+
id: 1,
16+
name: "Ralph",
17+
email: "[email protected]");
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using WebApplicationAPI.Domain;
2+
3+
namespace WebApplicationAPI.ViewModels;
4+
5+
public sealed record UserViewModel(
6+
int Id,
7+
string Name
8+
)
9+
{
10+
public static UserViewModel FromUser(User user) => new(user.Id, user.Name);
11+
}

examples/WebApplicationAPI/WebApplicationAPI.http

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
POST {{WebApplicationAPI_HostAddress}}/api/user
44

55
###
6+
7+
@id=99
8+
GET {{WebApplicationAPI_HostAddress}}/api/user/{{id}}
9+
10+
###

0 commit comments

Comments
 (0)