Search code examples
c#genericsc#-4.0wcf-ria-services

DRY (refactor) this code that uses callbacks?


I have several methods that have a return type IEnumerable<T>. These methods are part of DomainService class, and on the client they are generated with a return type of InvokeOperation<T>

public InvokeOperation<IEnumerable<T>> MethodA(string prm, Action<InvokeOperation<IEnumerable<T>>> 

public InvokeOperation<IEnumerable<T>> MethodB(Action<InvokeOperation<IEnumerable<T>>> 

WHen calling these methods, the code in the callback is basically the same

void SomeMethodA(string someString, Action<ResultsArgs<string>> operationCompleted)
{
    MyContext.MethodA(someString, c =>
        {
            // same code (operationCompleted parameter is used)
        }, null);
}

void SomeMethodA(Action<ResultsArgs<string>> operationCompleted)
{
    MyContext.MethodB(c =>
        {
            // same code (operationCompleted parameter is used)
        }, null);
}

How can I refactor this so there is no duplicate code?


Solution

  • make a method that takes a c and operationCompleted....

    then

      MyContext.MethodA(someString, c => MyShinyNewMethod(c, operationCompleted), null);