Execute multiple steps with error checking [Answered]
I have multiple steps that would be executed one by one with error checking after completing each step.
Eg:
If a step fails, all subsequent steps will not be executed and jump to ResultHandling.
I had give it a try by adding a IsError variable and using it to check the execution state but it looks dirty.
Are there any better way to implement this?
25 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
A common pattern for this (mostly in the functional world) is what's called "railway-oriented programming", in which you return from each of your steps a
Result<T>
type which represents either a success or an error. If you combine this with a method which chains these results, you can end up with some very readable and error-proof code.
So in your case you could have something like
The result of each method call would encode whether it succeeded or not, in which case it will only continue if the previous result was a success, otherwise it'll just pass down the error.Thank you, I will try
I'm not fan of try-catch and every step would handle and specific error on its own. So I don't use that
There are also libraries for this which you could check out, notably Remora.Results and LanguageExtensions (although the latter contains a lot of other stuff)
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yep
Imo it contains waaay too much for its own good
It's like trying to make C# into F# without actually changing anything about the language itself
Depending on your use case, your own implementation is also pretty easy to make
but once you go down the monad path, be prepared for some headwinds 😄
it's basicly a class for which the continue returns either a new class containing the result or is empty
and continue on an empty one is just another empty one without invoking the step
$monad
A monad is a pattern which requires a type to define three methods, here visualized as a C# interface:
Monads are predominantly used in functional programming, but C# natively contains a few monadic structures:
* Async/await, where the inner value is the result of the task.
*
Nullable<T>
.
* IEnumerable<T>
, where Select
is equivalent to Map
, and SelectMany
is equivalent to Bind
.Although really you don't need to worry about what a monad is supposed to do, it's just a useful patterns for situations like this
will try to create something similar first.
yeah sorry for maybe being a bit overwhelming
you could ofcourse also go an easier way, making it an array of func's
which return a tuple or some object you can check if it succeeded
and just chain them up manually
it's less fun though, and probably a lot messier code. But probably easier concept to grasp
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I wonder why your suggestion is messy.
Depend on what you said, I would create something like this :
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
(a Task is also a monad, so makes sense that it follows the same structure
I guess you ment me saying it might be messier>
I think it's messier cuz it's alot of code, which might end up bloated because all the logic is in the single place. You might want to pass along the results, start adding some conversions / type checking etc.
If you use monads it becomes a bit more of a conceptual work than hard coded work. Which will probably result in less code and imo thus less 'messy'.
I imagine especially conversions between the actual data results will get messy in your example.
But if you never need any other logic in there, just looping over the steps can be the easiest as mentioned.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
This is really nice though, kind of same as what Thinker said
yep
continue ftw
Maybe if it's in some other complex use case. My steps would return execution logs and a boolean variable to indicate if it's success. Each step has their own way to collect data which it needs.
ah yeah, each step is not strongly dependent on its prior step but start in a correct order.
you'll probably be fine with an easy loop and exiting then
Thank you guys.
✅ This post has been marked as answered!