@@ -167,6 +167,89 @@ public async Task<IActionResult> CreateUser(CreateUserRequest request)
167167}
168168```
169169
170+ ### Pattern Matching with Match()
171+
172+ The ` Match() ` method provides a functional approach to handling results by executing one of two callbacks based on success or failure:
173+
174+ ``` csharp
175+ [HttpGet (" {id}" )]
176+ public async Task < IActionResult > GetUser (int id )
177+ {
178+ var result = await _dispatcher .Dispatch (new GetUserRequest (id ));
179+
180+ return result .Match (
181+ user => Ok (user ),
182+ error => NotFound (new { error })
183+ );
184+ }
185+
186+ [HttpPost ]
187+ public async Task < IActionResult > CreateUser (CreateUserRequest request )
188+ {
189+ var result = await _dispatcher .Dispatch (request );
190+
191+ return result .Match (
192+ user => CreatedAtAction (nameof (GetUser ), new { id = user .Id }, user ),
193+ error => BadRequest (new { error })
194+ );
195+ }
196+ ```
197+
198+ #### Accessing Exception Information
199+
200+ Use the overload that accepts both error message and exception when you need detailed error information:
201+
202+ ``` csharp
203+ [HttpGet (" {id}" )]
204+ public async Task < IActionResult > GetUser (int id )
205+ {
206+ var result = await _dispatcher .Dispatch (new GetUserRequest (id ));
207+
208+ return result .Match (
209+ user => Ok (user ),
210+ (error , exception ) =>
211+ {
212+ if (exception is not null )
213+ {
214+ _logger .LogError (exception , " Failed to get user {UserId}" , id );
215+ }
216+ return NotFound (new { error , hasException = exception is not null });
217+ }
218+ );
219+ }
220+ ```
221+
222+ #### Non-Generic Result
223+
224+ The ` Match() ` method is also available on the non-generic ` Result ` type:
225+
226+ ``` csharp
227+ public async Task < IActionResult > DeleteUser (int id )
228+ {
229+ var result = await _dispatcher .Dispatch (new DeleteUserRequest (id ));
230+
231+ return result .Match (
232+ () => NoContent (),
233+ error => NotFound (new { error })
234+ );
235+ }
236+
237+ // With exception handling
238+ public async Task < IActionResult > DeleteUser (int id )
239+ {
240+ var result = await _dispatcher .Dispatch (new DeleteUserRequest (id ));
241+
242+ return result .Match (
243+ () => NoContent (),
244+ (error , exception ) =>
245+ {
246+ _logger .LogError (exception , " Failed to delete user {UserId}" , id );
247+ return StatusCode (500 , new { error });
248+ }
249+ );
250+ }
251+ ```
252+
170253## Exception Handling
171254
172255ResultR automatically catches exceptions in handlers and converts them to failure results:
0 commit comments