Skip to content

Commit 39fae93

Browse files
committed
More tweaks
1 parent 71d93e8 commit 39fae93

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ services.AddMediator();
3333

3434
## 🧩 Simple Handler Example
3535

36-
Just add a class ending with `Handler` or `Consumer`. Methods must be named `Handle(Async)` or `Consume(Async)`. Supports multiple handler methods in a single class—for example, a `UserHandler` containing Create, Read, Update, Delete methods.
36+
Just add a class ending with `Handler` or `Consumer`. Methods must be named `Handle(Async)` or `Consume(Async)`. Classes and methods can be static if they are stateless. Supports multiple handler methods in a single class—for example, a `UserHandler` containing Create, Read, Update, Delete methods.
3737

3838
```csharp
3939
public record Ping(string Text);
@@ -70,7 +70,7 @@ public class EmailHandler
7070

7171
## 🎪 Simple Middleware Example
7272

73-
Discovered by convention; static or instance with DI:
73+
Discovered by convention; static or instance:
7474

7575
```csharp
7676
public static class ValidationMiddleware
@@ -85,17 +85,17 @@ public static class ValidationMiddleware
8585
## 📝 Logging Middleware Example
8686

8787
```csharp
88-
public class LoggingMiddleware
88+
public class LoggingMiddleware(ILogger<LoggingMiddleware> log)
8989
{
9090
public Stopwatch Before(object msg) => Stopwatch.StartNew();
9191

9292
public void Finally(object msg, Stopwatch sw, Exception? ex)
9393
{
9494
sw.Stop();
9595
if (ex != null)
96-
Console.WriteLine($"Error in {msg.GetType().Name}: {ex.Message}");
96+
log.LogInformation($"Error in {msg.GetType().Name}: {ex.Message}");
9797
else
98-
Console.WriteLine($"Handled {msg.GetType().Name} in {sw.ElapsedMilliseconds}ms");
98+
log.LogInformation($"Handled {msg.GetType().Name} in {sw.ElapsedMilliseconds}ms");
9999
}
100100
}
101101
```
@@ -105,7 +105,7 @@ public class LoggingMiddleware
105105
Result\<T> is our built-in discriminated union for message-oriented workflows, capturing success, validation errors, conflicts, not found states, and more—without relying on exceptions.
106106

107107
```csharp
108-
public class GetUserHandler
108+
public class UserHandler
109109
{
110110
public async Task<Result<User>> HandleAsync(GetUser query) {
111111
var user = await _repo.Find(query.Id);
@@ -115,6 +115,19 @@ public class GetUserHandler
115115
// implicitly converted to Result<User>
116116
return user;
117117
}
118+
119+
public async Task<Result<User>> HandleAsync(CreateUser cmd)
120+
{
121+
var user = new User {
122+
Id = Guid.NewGuid(),
123+
Name = cmd.Name,
124+
Email = cmd.Email,
125+
CreatedAt = DateTime.UtcNow
126+
};
127+
128+
await _repo.AddAsync(user);
129+
return user;
130+
}
118131
}
119132
```
120133

0 commit comments

Comments
 (0)