I have a class that has about 200+ methods, each of these methods makes a call into the database, or a network resource.
Ideally, I would like to wrap each call in a try/catch, to catch any of the common network or SQL exceptions, and give the user the opportunity to try again (if appropriate). However, to add this code to each call would be very time consuming, and bloated as far as the code is concerned.
I've thought about wrapping each of the method calls in another method, creating a delegate, and wrapping the delegate code in the try/catch.. something like this...
(Ignore the syntax.. it's just a conceptual example)
bool CallUpdatePassenger(int PassengerId,string PassengerName,string PhoneNumber)
{
Delegate del= Delegate.CreateDelegate(typeof(UpdatePassengerDelegate), typeof(IPassengerServices).GetMethod("RemoteUpdatePassenger"));
bool Res=(bool)CallDelegate(del,PassengerName,PhoneNumber);
}
object CallDelegate(Delegate del,params object[] args)
{
object Result=null;
try
{
Result=del.DynamicInvoke(args);
}
catch (Some.Timeout.Error.Or.Whatever te)
{
// take some action.. maybe retry etc..
}
return Result;
}
Maybe there's a more pracitcal way of doing this ?
The code is automatically generated (by a tool which I wrote) I can quite easily include something like the above, but I want to avoid writing the above code for every method call.
Also, if I do something like the above, I can time methods, and log method calls etc. It just seems a bit clumsy (and not strongly typed).
Thanks
Rich.
You should be able to just do something like:
T Execute<T>(Func<T> func) {
try {
return func();
} catch (...) {
...
}
}
bool CallUpdatePassenger(some args here) {
return Execute( () => realObj.RemoteUpdatePassenger(some args here));
}
alternatively, you could use meta-programming to write a dynamic "decorator" for the underlying methods on the fly... but unless you are familiar with ILGenerator
etc probably best not to - it is a fairly advanced topic.
I think your basic idea is good, but there's a simpler way to implement it (at least of you're using .Net 3.5 or more recent):
void WithStandardRetryLogic(Action method)
{
try
{
method();
}
catch (Some.Timeout.Error.Or.Whatever te)
{
// take some action.. maybe retry etc..
}
}
Example use:
WithStandardRetryLogic(delegate() { CallUpdatePassenger(PassengerId, PassengerName, PhoneNumber); });
This may also be something where an AOP framework might be useful, but I have not tried that solution.
If you can use PostSharp, you can use this aspect:
[Serializable]
public class RetryAttribute : MethodInterceptionAspect
{
private readonly int _times;
public RetryAttribute(int times)
{
_times = times;
}
public override void OnInvoke(MethodInterceptionArgs args)
{
for (var left = _times; left > 0; left--)
{
try
{
args.Proceed();
break;
}
catch (Exception)
{
}
}
args.Proceed(); // optional
}
}
Usage would be like this:
[Retry(2)]
public static void DoIt()
{
Console.WriteLine("tried");
throw new Exception();
}
I think one of the possible solutions can be AOP programming. You can mark any method you want with an attribute and inject the try/catch
code on compilation.
See a sample here
Hope this helps.