new MethodExecuter()
.ForAllInvocationsCheckCondition(!Context.WannaShutDown)
.When(shouldRun).ThenInvoke(x => x.Methods(new Action[] { B.Process, A.Process }))
.When(shouldRun).ThenInvoke(x => x.Method(B.Process).Repeat(100))
.Invoke(x => x.Method(C.Process));
I deliberately do not say anything about what this API does. I did show this API my team mates and they did not like it. For them it is unreadable.
What do you think about the API? What tips do you have? What do you think about complexitiy, usability and readability of fluent APIs and how to achieve good APIs?
EDIT Why did I build this API?
Well, I have a bunch of methods that are invoked synchronously when a condition is false. The condition applies foreach method invocation.
if (!Context.WannaShutDown) A.Process();
if (!Context.WannaShutDown) B.Process();
if (!Context.WannaShutDown) C.Process();
if (!Context.WannaShutDown) D.Process();
if (!Context.WannaShutDown) E.Process();
if (!Context.WannaShutDown) F.Process();
if (!Context.WannaShutDown)
{
if (condition)
{
for (int i = 0; i <= 100 i++)
{
Z.Process();
}
}
...
I do not like such crap code. What I wanted to do is to solve "the problem" once with a class that is responsible for invocating methods. I tried to build an API that is as simple as possible, but it seems I failed.
.ForEach
operator is not necessarily better than a simpleforeach
loop. With that being said, this could be a good design, but I think you need to explain a bit the pain it abstracts away. To put it another way, C# already has a language facility for executing methods: the language itself is already designed for this purpose. \$\endgroup\$