The Command pattern is one of my all time favorite patterns. I have used it numerous times in programming with great success. Recently, while rearchitecting an application I am reworking for a client I needed a means to encapsulate the logic that called a web service and caught the corresponding error or processed the result if it succeeded.
The principle problem is that these calls must support the ability to retry the call should it fail. The previous developer used loops in each function to support this requirement, flagrantly violating DRY. My approach was a permutation on the Command pattern. Pass a command object into a Executor class. The class would treat the object generically the same, simply calling a command method which would call the particular web service method via a layer of indirection. Here is my first rough implementation of this idea:
In addition, I wanted to make sure I could control the return type of the call so I could maintain type safety, hence I used generics. In a later version of this code, I made the RetryAttempts property virtual and defaulted it to 0, which would yield only one attempt. Derivative command objects could overwrite this as necessary.
For the most part this worked except that it yielded three classes for each web service operation (input, result, and command classes). This is simply too much ceremony and given the team that will support the application would result in being very ornery and error prone. Hence I decided to look for another way. I found it using lambda expressions.
There are three basic lambda expressions in .NET: predicate which is used for Is comparisons, action which is used to simply call a function with no return value, a func which is the same action, save it supports a return value. An analysis of the previous code yielded the stipulation that all service calls will return a class with ResultBase as its parent. Hence we arrived at the following code:
This is the structure of Command.Execute:
So what we have done is used a lambda to replace the original command object. This will operate exactly the same way. (Note: this is a different solution as the solution was reached at a later date). This cleans up our code base and reduces the amount of ceremony we need to go through, no more creating the command objects. However, we really dont like exposing the Func syntax in the main code, if our developers are not as fluent with C# as we are this can cause a great deal of confusion. In my case, the developers left to support this application will not be as fluent and need to have certain things stored away where they can be easily copied. For this, the best pattern is the Factory pattern. Hence we change our code to the following:
Not really much has changed except the addition of the CommandFactory static class. Here is the implementation:
This actually works perfectly for my case. You can see that we were able to remove the crazy lambda that would have caused confusion and instead replace it with a much simpler expression and IN ADDITION, provide a function for it. This helps give our code meaning. This code also reads much easier with all the typing inferred rather then explicitly stated.