Free C # Builder

Wanted to write a little loosely like this ...

new Actor().DoSomething<Type>().WithOption(options).When(Conditions);

      

I understand method chaining and how to pass an action to a subsequent method / object.

For example, the DoSomething action will be passed to the object containing the WithOption method, and the withOption action will be passed to the object containing the When method.

It could be something like

interface Actor {
  ActorOption DoSomething<T>();
}

interface ActorOption {
  ActorCondition WithOption();
}

interface ActorCondition{
  void When();
}

      

So the actual action takes place in the WHEN method. So far so good.

To execute code with or without a default parameter, it could be called new Actor.DoSomething<T>().WithDefaultOption().Always();

My concern is that if someone leaves the code in the middle, that means new Actor.DoSomething<T>()

. It seems like the actor has to do something that is completely free.

I cannot think of a design that can cover a similar scenario. Please suggest.

PS: I'm writing a wrapper over some third party class, so I don't have full control over the inner implementation. For example, I cannot set a condition if parameters are set or vice versa.

+3


source to share


3 answers


if you write

new Actor().DoSomething<Type>().WithOption(options).When(Conditions);

      

You can write an alternative:



Actor actor = new Actor();
doResult = actor.DoSomething<Type>();
optionResult = doResult.WithOption(options);
conditionResult = optionResult.When(Conditions);

      

So, you can see that your parameters and your state are added after DoSomething is done, so it doesn't affect the DoSomething method. If you are executing your code finally in the "When" method, it is not intuitive. Rather, you prepare your actor and get the result with DoSomething () ("lamda-style").

And if someone leaves some method calls ... Yes, of course, then it is missing ... If necessary, you must add it as a parameter to DoSomething. If not, you must define default parameters that are used if no parameters are specified.

+1


source


The actual action should not take place in the method When

. Instead, try making your interface smooth so that the last method "does something":

new Actor<Type>().WithOption(options).When(Conditions).DoSomething();

      



So, your actor will be configured first and then launched. If you don't like this syntax, you can still find something like:

new Actor().DoSomething(config => config.WithOption(options).When(conditions));

      

+1


source


There are several ways to monad. Maybe they can help you.

public static class Maybe
{
    public static TResult With<TInput, TResult>
        (this TInput o, Func<TInput, TResult> evaluator)
        where TInput : class where TResult : class
    {
        return o == null ? null : evaluetor(o);
    }

    public static TResult Return<TInput, TResult>
        (this TInput o, Func<TInput, TResult> evaluator, TResult failureValue)
        where TInput : class
    {
        return o == null ? failureValue : evaluator(o);
    }

    public static bool ReturnSuccess<TInput>(this TInput o)
        where TInput : class
    {
        return o != null;
    }

    public static TInput If<TInput>(this TInput o, Predicate<TInput> evaluator)
        where TInput : class
    {
        if (o == null)
            return null;
        return evaluator(o) ? o : null;
    }

    public static TInput Do<TInput>(this TInput o, Action<TInput> action)
        where TInput : class
    {
        if (o == null) return null;
        action(o);
        return o;
    }
    public static TInput If<TInput>(this TInput o, Func<TInput, bool> evaluator)
        where TInput : class
    {
        if (o == null) return null;
        return evaluator(o) ? o : null;
    }        
}

      

-1


source







All Articles