Suppose I have a .NET assembly, A, which uses reflection to load assemblies B and C (also .NET). These two assemblies both implement a number of large interfaces (the same for both). When a method is called in A it tries to make B do the work. However, B is unreliable and might throw exceptions. Now A has two modes, one where it propagates the exceptions out to the caller of A and one where it calls matching methods on the more stable (but less performant) C.
Are there better ways (less code) to implement this scenario than wrapping all the methods B exposes in a huge implementation of all B's interfaces, only now surrounding every call with code as shown below? The assemblies B and C don't know anything about the error handling approach chosen, so they can't implemet the logic.
public class BErrorWrapper : I1, I2, I3{
...
public int DoSomeWork(int num){
if (FailWithExceptions)
{
try
{
return B.DoSomeWork(num);
}
catch(MyLibException e)
{
return C.DoSomeWOrk(num);
}
}
else
{
return B.DoSomeWork(num);
}
}
...
}