debugcsharpMinor
Decorating service methods calls for centralized error processing
Viewed 0 times
callserrormethodscentralizedservicefordecoratingprocessing
Problem
I have multiple services with a lot of methods. Every method returns an
Up until now I came up with this solution:
```
#region Decorators
#region [Getters]
#region [Void]
protected delegate int KeeperAPIDelegateVoid(string sessionID, T1 obj1);
protected delegate int KeeperAPIDelegateVoid(string sessionID, T1 obj1, T2 obj2);
protected void DataGrabber(KeeperAPIDelegateVoid action, params object[] args)
{
int result = action.Invoke(SessionID, (T1)args[0]);
if (result != 0) throw HandleError(result);
}
protected void DataGrabber(KeeperAPIDelegateVoid action, params object[] args)
{
int result = action.Invoke(SessionID, (T1)args[0], (T2)args[1]);
if (result != 0) throw HandleError(result);
}
#endregion
#region [Typed]
protected delegate int KeeperAPIDelegate(string sessionID, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, T4 obj4, T5 obj5, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, T4 obj4, T5 obj5, T6 obj6, out TReturn1 rObj1, out TReturn2 rObj2, out TReturn3 rObj3);
protected TReturn DataGrabber(KeeperAPIDelegate action)
{
TReturn data;
int result = action.Invoke(SessionID, out data);
if (result == 0) return data;
throw HandleError(result);
}
protected TReturn DataGrabber(KeeperAPIDelegate action, params object[] args)
{
TReturn data;
int result = action.Invoke(SessionID, (T1)args[0], out data);
if (result == 0) return data;
throw HandleError(resul
int as a result code. All methods have 2 types of signatures:- Only with
INparams
- n of
INparams and mOUTparams in the end (in most cases 1OUTparam)
Up until now I came up with this solution:
```
#region Decorators
#region [Getters]
#region [Void]
protected delegate int KeeperAPIDelegateVoid(string sessionID, T1 obj1);
protected delegate int KeeperAPIDelegateVoid(string sessionID, T1 obj1, T2 obj2);
protected void DataGrabber(KeeperAPIDelegateVoid action, params object[] args)
{
int result = action.Invoke(SessionID, (T1)args[0]);
if (result != 0) throw HandleError(result);
}
protected void DataGrabber(KeeperAPIDelegateVoid action, params object[] args)
{
int result = action.Invoke(SessionID, (T1)args[0], (T2)args[1]);
if (result != 0) throw HandleError(result);
}
#endregion
#region [Typed]
protected delegate int KeeperAPIDelegate(string sessionID, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, T4 obj4, T5 obj5, out TReturn rObj);
protected delegate int KeeperAPIDelegate(string sessionID, T1 obj1, T2 obj2, T3 obj3, T4 obj4, T5 obj5, T6 obj6, out TReturn1 rObj1, out TReturn2 rObj2, out TReturn3 rObj3);
protected TReturn DataGrabber(KeeperAPIDelegate action)
{
TReturn data;
int result = action.Invoke(SessionID, out data);
if (result == 0) return data;
throw HandleError(result);
}
protected TReturn DataGrabber(KeeperAPIDelegate action, params object[] args)
{
TReturn data;
int result = action.Invoke(SessionID, (T1)args[0], out data);
if (result == 0) return data;
throw HandleError(resul
Solution
If the service methods are not changeable, I don't see a solution that is fundamentally better then yours...
- If there are only one or a few methods with 3 return values, consider to create more descriptive custom types for the return values (instead of using
Tuple).
- The check for
(result == 0)is implemented for each method. You could do that within the methodHandleError. However, that requires, thatHandleErrordoes not return the exception but throws it directly if result is != 0.
Context
StackExchange Code Review Q#138323, answer score: 2
Revisions (0)
No revisions yet.