I have several methods that look like this:
public void foo()
{
try
{
doSomething();
}
catch(Exception e)
{
Log.Error(e);
}
}
You can use delegates and lambdas:
private void ExecuteWithLogging(Action action) {
try {
action();
} catch (Exception e) {
Log.Error(e);
}
}
public void fooSimple() {
ExecuteWithLogging(doSomething);
}
public void fooParameter(int myParameter) {
ExecuteWithLogging(() => doSomethingElse(myParameter));
}
public void fooComplex(int myParameter) {
ExecuteWithLogging(() => {
doSomething();
doSomethingElse(myParameter);
});
}
In fact, you could rename ExecuteWithLogging
to something like ExecuteWebserviceMethod
and add other commonly used stuff, such as checking credentials, opening and closing a database connection, etc.