C
C#

help

Execute multiple steps with error checking [Answered]

FFroH.LVT9/29/2022
I have multiple steps that would be executed one by one with error checking after completing each step. Eg:
void Start(){
Step1();
Step2();
....
StepN();

ResultHandling();
}
void Start(){
Step1();
Step2();
....
StepN();

ResultHandling();
}
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?
UUUnknown User9/29/2022
Message Not Public
Sign In & Join Server To View
TThinker9/29/2022
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
void Start() {
var result = Step1()
.Continue(Step2)
.Continue(Step3)
// ...
.Continue(StepN);

if (result.IsError) {
// Do error handling
}
}
void Start() {
var result = Step1()
.Continue(Step2)
.Continue(Step3)
// ...
.Continue(StepN);

if (result.IsError) {
// Do error handling
}
}
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.
FFroH.LVT9/29/2022
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
TThinker9/29/2022
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)
UUUnknown User9/29/2022
Message Not Public
Sign In & Join Server To View
TThinker9/29/2022
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
UUUnknown User9/29/2022
4 Messages Not Public
Sign In & Join Server To View
TThinker9/29/2022
$monad
MMODiX9/29/2022
A monad is a pattern which requires a type to define three methods, here visualized as a C# interface:
interface IMonad<T>
{
// Applies a Func<T, U> to the inner value of the monad.
IMonad<U> Map<U>(Func<T, U> mapping);
// Applies a Func<T, IMonad<U>> to the inner value of the monad, and returns the result.
IMonad<U> Bind<U>(Func<T, IMonad<U>> binding);
// Returns a new monad with an inner value.
static abstract IMonad<T> Return(T value);
}
interface IMonad<T>
{
// Applies a Func<T, U> to the inner value of the monad.
IMonad<U> Map<U>(Func<T, U> mapping);
// Applies a Func<T, IMonad<U>> to the inner value of the monad, and returns the result.
IMonad<U> Bind<U>(Func<T, IMonad<U>> binding);
// Returns a new monad with an inner value.
static abstract IMonad<T> Return(T value);
}
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.
TThinker9/29/2022
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
FFroH.LVT9/29/2022
HmmNoted will try to create something similar first.
TThinker9/29/2022
yeah sorry for maybe being a bit overwhelming
UUUnknown User9/29/2022
5 Messages Not Public
Sign In & Join Server To View
FFroH.LVT9/29/2022
I wonder why your suggestion is messy. Depend on what you said, I would create something like this :
class SeriesStepInvoker{
Array<Func<T>> Steps;
void Start(){
foreach( var step in Steps){
T res = step()
if(T.IsError){
break;
}
}
ResultHandling()
}
}
class SeriesStepInvoker{
Array<Func<T>> Steps;
void Start(){
foreach( var step in Steps){
T res = step()
if(T.IsError){
break;
}
}
ResultHandling()
}
}
UUUnknown User9/29/2022
2 Messages Not Public
Sign In & Join Server To View
TThinker9/29/2022
(a Task is also a monad, so makes sense that it follows the same structure catsip
UUUnknown User9/29/2022
2 Messages Not Public
Sign In & Join Server To View
FFroH.LVT9/29/2022
This is really nice though, kind of same as what Thinker said
TThinker9/29/2022
yep continue ftw
FFroH.LVT9/29/2022
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.
UUUnknown User9/29/2022
Message Not Public
Sign In & Join Server To View
FFroH.LVT9/29/2022
Thank you guys.
AAccord9/29/2022
✅ This post has been marked as answered!

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Window opacity isn't being affected?I have a button click method over here that should set a window to opaque: ```cs private voiCoding winforms on ubuntu? is it possible now? if yes how?Can I work with winforms on ubuntu? I know winforms used to be limited to windows only, is it still System.Net.Sockets spookynessHello! : I've been tasked with updating our web app backend to .net 6 and I am almost done. HoweveINotifyDataErrorInfo helpWhen using the implementation for INotifyDataErrorInfo, is it possible when raising errors change evHow to pass value to parameter that require IEnumable string [Answered]Help me with the syntaxIs Array.Copy decent for copying sections of memory?I'm making a thing which relies pretty heavily on copying (not very large) sections of memory insideRelationship between Microsoft.AspNetCore.Identity.EntityFrameworkCore and AADHey guys, I read a bunch of Azure Active Directory documents and see that they recommend us not to sMSIX URI is inaccessible in AppinstallerHello, I have an Appinstaller and MSIX hosted in a public Google Cloud Storage bucket, meaning theyI'm having problems with a range of numbers and random generator (im new)I have an array of number (1-50, scaled), where one will be selected randomly and then every number EFCore Updating an item after mapping with AutomapperHey I am using Ef core to store some data in sqlite. I need to translate it to another object to shoIs it possible to allow an installer to downgrade dll's while not overwriting other app files?When creating an installer. (using Wix) Is there a way to have better control over the reinstall proIs there any way to simplify this, or a design pattern I should be following?```cs public event EventHandler<FizzbuzzDetermineNumberRequest>? DetermineNumberRequestHandlers; How to get service that not registed with interfaceHelpAssigning all StackPanel child element marginI am trying to set the margin for all elements in stackpanel from resor. Can you point out why it's Generics and type inference```cs public TDest MapTo<TSource, TDest>(TSource src) { var res = _mapper.Map<TSourcCrash of the background worker because of the SQL connectionI have a background worker running on some machine, which getting random crashes because of the SQL Minimum need data to create an EntityMy entity: ```cs public class Book : BaseEntity { public string Name { get; set; } MediatR commands and DTOs [Answered]Currently, I am passing DTOs to my controllers, I am using MediatR and it supports commands. I use tHow can I send int value from 1 program, to another via internet?```fix Hello. Is there any easy way to connect 2 programs to each other over the internet? I mean,InterfaceHi, i've this interface but there is only 1 class which doesn't need any parameter in `getAll(long i