Edit: fixed several syntax and consistency issues to make the code a little more apparent and close to what I actually am doing.
I’ve got some code that looks like this:
SomeClass someClass;
var finalResult =
DoSomething(() =>
{
var result = SomeThingHappensHere();
someClass = result.Data;
return result;
})
.DoSomething(() => return SomeOtherThingHappensHere(someClass))
.DoSomething(() => return AndYetAnotherThing())
.DoSomething(() => return AndOneMoreThing(someClass))
.Result;
HandleTheFinalResultHere(finalResult);
where the DoSomething method is an extension method, and it expects a Func passed into it. So, each of the method calls in each of the DoSomething => lambda’s returns a Result type.
this is similar to a Maybe monad. Except instead of checking for nulls, I am checking the status of the Result class, and either calling the Func that was passed into DoSomething or returning the previous Result without calling the Func
the problem i face is that want to have this kind of composition in my code, but i also need to be able to pass data from one of the composed call results into the call of another, as you can see with the someClass variable.
My question isn’t whether or not this is technically correct… i know this works, because I’m currently doing it. My question is whether or not this is an abuse of closures, or command-query separation, or any other principles… and then to ask what better patterns there are for handling this situation, because I’m fairly sure that I’m stuck in a “shiny new hammer” mode with this type of code, right now.
As has already been noted, you’ve almost implemented a Monad here.
Your code is a bit inelegant in that the lambdas have side-effects. Monads solve this more elegantly.
So, why not turn your code into a proper Monad?
Bonus: you can use LINQ syntax!
I present:
LINQ to Results
Example:
With LINQ to Results, this first executes
SomeThingHappensHere. If that succeeds, it gets the value of theDataproperty of the result and executesSomeOtherThingHappensHere. If that succeeds, it executesAndYetAnotherThing, and so on.As you can see, you can easily chain operations and refer to results of previous operations. Each operation will be executed one after another, and execution will stop when an error is encountered.
The
from x inbit each line is a bit noisy, but IMO nothing of comparable complexity will get more readable than this!How do we make this work?
Monads in C# consist of three parts:
a type Something-of-T,
Select/SelectManyextension methods for it, anda method to convert a T into a Something-of-T.
All you need to do is create something that looks like a Monad, feels like a Monad and smells like a Monad, and everything will work automagically.
The types and methods for LINQ to Results are as follows.
Result<T> type:
A straightforward class that represents a result. A result is either a value of type T, or an error. A result can be constructed from a T or from an Exception.
Extension methods:
Implementations for the
SelectandSelectManymethods. The method signatures are given in the C# spec, so all you have to worry about is their implementations. These come quite naturally if you try to combine all method arguments in a meaningful way.You can freely modify the Result<T> class and the extension methods, for example, to implement more complex rules. Only the signatures of the extension methods must be exactly as stated.