You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## 🎯 Result Types: The Foundation of Robust Message-Oriented Architecture
104
+
105
+
The built-in `Result` and `Result<T>` types are fundamental to Foundatio.Mediator's design, providing a discriminated union pattern that's essential for message-oriented architectures. Instead of relying on exceptions for control flow, Result types enable explicit, type-safe handling of all operation outcomes.
106
+
107
+
### Why Result Types Are Critical
108
+
109
+
**Message-oriented architectures benefit from Result types because:**
110
+
111
+
-**🎯 Explicit Error Handling** - All possible outcomes are represented in the type system
112
+
-**🚫 No Hidden Exceptions** - Errors are data, not exceptional control flow
113
+
-**📊 Rich Status Information** - Beyond success/failure: validation, conflicts, authorization, etc.
114
+
-**🔄 Composable Operations** - Chain operations with confidence about what can happen
115
+
-**📈 Better Observability** - Track success rates, error patterns, and business metrics
116
+
-**🛡️ Defensive Programming** - Force consumers to handle all possible scenarios
## 🎯 Built-in Result Type - Essential for Message-Oriented Architecture
150
+
151
+
Foundatio.Mediator includes a comprehensive `Result` and `Result<T>` type that acts as a discriminated union, allowing handlers to return different operation outcomes without exceptions. This is crucial for message-oriented architectures where you need to handle various scenarios gracefully.
152
+
153
+
### Why Result Types Matter
154
+
155
+
In message-oriented systems, operations can have many outcomes beyond just success/failure:
156
+
157
+
-**Success** with data
158
+
-**Validation errors** with detailed field-level messages
:Result.NotFound($"User with ID {command.Id} not found");
241
+
}
242
+
}
72
243
```
73
244
74
245
75
246
## 🎪 Beautiful Middleware Pipeline
76
247
77
-
Create elegant middleware that runs before, after, and finally around your handlers:
248
+
Create elegant middleware that runs before, after, and finally around your handlers. Middleware works seamlessly with the Result type for comprehensive error handling:
78
249
79
250
```csharp
80
251
publicclassLoggingMiddleware
@@ -111,17 +282,38 @@ public class ValidationMiddleware
111
282
{
112
283
publicHandlerResultBefore(objectmessage)
113
284
{
114
-
if (!TryValidate(message, outvarerrors))
285
+
if (!MiniValidator.TryValidate(message, outvarerrors))
115
286
{
116
-
// If validation fails, short-circuit the handler execution
Copy file name to clipboardExpand all lines: src/Foundatio.Mediator.SourceGenerator/MediatorImplementationGenerator.cs
-19Lines changed: 0 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -180,25 +180,6 @@ public static string GenerateMediatorImplementation(List<HandlerInfo> handlers)
180
180
source.AppendLine(" }");
181
181
source.AppendLine();
182
182
183
-
// Generate Publish method (sync)
184
-
source.AppendLine(" public void Publish(object message, CancellationToken cancellationToken = default)");
185
-
source.AppendLine(" {");
186
-
source.AppendLine(" var handlersList = GetAllApplicableHandlers(message).ToList();");
187
-
source.AppendLine();
188
-
source.AppendLine(" // Check if any handlers require async execution");
189
-
source.AppendLine(" if (handlersList.Any(h => h.IsAsync))");
190
-
source.AppendLine(" {");
191
-
source.AppendLine(" var messageTypeName = message.GetType().FullName;");
192
-
source.AppendLine(" throw new InvalidOperationException($\"Cannot use synchronous Publish with async-only handlers for message type {messageTypeName}. Use PublishAsync instead.\");");
193
-
source.AppendLine(" }");
194
-
source.AppendLine();
195
-
source.AppendLine(" // Execute all handlers synchronously");
196
-
source.AppendLine(" foreach (var handler in handlersList)");
0 commit comments