C
C#6mo ago
M B V R K

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, TCommandResultclass, 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 Succeeededare 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 ifs 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 <3
GitHub
ExpenovaApp/Source/ExpenseService/ExpenseService.Application/Behavi...
Contribute to MbarkT3STO/ExpenovaApp development by creating an account on GitHub.
4 Replies
M B V R K
M B V R K6mo ago
GitHub
ExpenovaApp/Source/ExpenseService/ExpenseService.Application/Catego...
Contribute to MbarkT3STO/ExpenovaApp development by creating an account on GitHub.
Anu6is
Anu6is6mo ago
Why throw exceptions that you later have to catch and wrap in a result instead of just returning a failed or exception result to begin with
M B V R K
M B V R K6mo ago
What do you mean?
Anu6is
Anu6is6mo ago
like why not have things like this return a failed result as opposed to throwing an exception.