I would like to know what is the best way to avoid repeating some code structure using Generics Func or any other way. As a practical example, let me call 20 different WCF methods, but I would like to have code to handle the exception.
Let's say this is a wcf proxy
class ClassWithMethodsToCall
{
public Out1 GetOut1(In1 inParam) { return null; }
public Out2 GetOut2(In2 inParam) { return null; }
public Out3 GetOut3(In3 inParam) { return null; }
}
class Out1 { }
class In1 { }
class Out2 { }
class In2 { }
class Out3 { }
class In3 { }
I created the following to have a single error handling
class CallerHelperWithCommonExceptionHandler
{
public Tout Call<Tout, Tin>(Tin parameters, Func<Tin,Tout> wcfMethodToCall)
{
try
{
return wcfMethodToCall(parameters);
}
catch (Exception ex)
{
throw;
}
}
}
And I use it:
var callerHelper = new CallerHelperWithCommonExceptionHandler();
var theFunctionsToCall = new ClassWithMethodsToCall();
var in1 = new In1();
var ou1 = callerHelper.Call<Out1, In1>(in1, theFunctionsToCall.GetOut1);
var in2 = new In2();
var ou2 = callerHelper.Call<Out2, In2>(in2, theFunctionsToCall.GetOut2);
Is there a more elegant way? Alternatives in an object oriented way, template template template?
Thanks al
source
share