There are many ways to hide this cat, but I think one of the sources of this difficulty is that you actually call the method in the InvokeMethod() (go figure!) Method.
Typically, we use free APIs to convert syntax that evaluates from the inside out to something that can be expressed in order from left to right. Thus, the components of the interface expression builder are used to create state in the entire expression, and only at the end does "real work" happen.
One solution to your immediate problem is to queue each action with its associated parameters (call conditions, number of retries, etc.) and add the ExecuteAll() method to MethodExecuter , which cancels and performs fully configured actions at the end of the member chain.
Another solution would be to put all the execution options inside the InvokeMethod() method; something like:
.Invoke(x => x.Method(A.Process).Repeat(100))
This method will look something like this:
public MethodExecuter Invoke(Action<IExecutionBuilder> executionBuilder) { var builder = new ExecutionBuilder(); executionBuilder(builder); var action = builder.Action; var repeat = builder.RepeatCount; if (_condition) { for (int i = 1; i <= repeat; i++) { action(); } } return this; }
I did not work with this in Visual Studio, but other elements would be like this:
public interface IExecutionBuilder { IExecutionBuilder Method(Action action); IExecutionBuilder Repeat(int count); } public class ExecutionBuilder : IExecutionBuilder { public ExecutionBuilder() { RepeatCount = 1;
Please note that RepeatCount and Action not displayed on the interface. That way, you wonβt see these members when you call .Invoke(x => x. , But you can access them when using the specific ExecutionBuilder class inside the Invoke() method.
Jay
source share