✅ Is this a correct way or I just mad things wrong?
Hi friends, hope you're doing well,
I'm working on a project which using
CQRS
with MediatR
,
I have a PipelineBehavior
for handling exception happened in the Commands
and Queries
,
Lets talk in a brief manner about Commands
, most of Commands in this project are returning a result ( type ) inherited from CommandResult<TValue, TCommandResult
class, that class used to implement the Result Pattern
,
How should this work usually?
Before, every Command
if executed successfully it should return a result ( its type inherited from CommandResult<TValue, TCommandResult
) that represents a succeeded operation, but in an Exception
situation the command should return a result ( its type inherited from CommandResult<TValue, TCommandResult
) that represents a failed operation, and both Faild
and Succeeeded
are the same type.
What I did?
I removed the exception handling from commands, and moved itt to that PiplineBehavior
,
So that behavior should execute the command, if it executed successfully, then its fine, but if an exception happened it will check which TResponse
related to the request and create a Failed result then return iit.
Please take a look on that behavior here :
https://github.com/MbarkT3STO/ExpenovaApp/blob/20c83b6076e197af5097c6c862db6ad9e7d0d42e/Source/ExpenseService/ExpenseService.Application/Behaviors/ExceptionHandlingBehavior.cs
Where is the issue here?
The issue is a method called CreateFailedResult
iit checks and returns the correct result, and the issue is that method checks all the commands
and also queries
, nd as you will notice that at this moment there are many if
s there, and in the future handreds of commands and queries will be added.
The question:
Is this technique I used here has a value or I just did things wrong ?
Is there any other way to do the same ?
Do you have any suggestion about this situation ?
Massive thanks in advance, with love <34 Replies
The following is an example of a command that I removed the exception handling from it
https://github.com/MbarkT3STO/ExpenovaApp/blob/20c83b6076e197af5097c6c862db6ad9e7d0d42e/Source/ExpenseService/ExpenseService.Application/Category/Commands/CreateCategoryCommand.cs
didn't you post this before?
Yeah, but get no answers
Thanks to
Github Copilot