我有几种返回类型为IEnumerable<T>
的方法。这些方法是DomainService
类的一部分,在客户端上生成它们的返回类型为InvokeOperation<T>
public InvokeOperation<IEnumerable<T>> MethodA(string prm, Action<InvokeOperation<IEnumerable<T>>>
public InvokeOperation<IEnumerable<T>> MethodB(Action<InvokeOperation<IEnumerable<T>>>
当调用这些方法时,回调中的代码基本相同
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);
}
如何重构这一点,以便没有重复的代码?
答案 0 :(得分:1)
制作一个采用c和operationCompleted ....
的方法然后
MyContext.MethodA(someString, c => MyShinyNewMethod(c, operationCompleted), null);
答案 1 :(得分:0)
我认为您只需提取一个以InvokeOperation<IEnumerable<T>>
和Action<ResultsArgs<string>>
为参数的方法。然后在c => { }
。
void SomeMethodA(string someString, Action<ResultsArgs<string>> operationCompleted)
{
MyContext.MethodA(someString, c =>
{
NewMethod(c, operationCompleted);
}, null);
}
void SomeMethodA(string someString, Action<ResultsArgs<string>> operationCompleted)
{
MyContext.MethodB(c =>
{
NewMethod(c, operationCompleted);
}, null);
}