Last active
December 11, 2015 11:28
-
-
Save LambdaSix/4593902 to your computer and use it in GitHub Desktop.
To return null or throw an exception? :/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidationMethods | |
{ | |
private IDictionary<object, Func<object, object, Boolean>> _functionTable; | |
public IDictionary<object, Func<object, object, Boolean>> FunctionTable | |
{ | |
get | |
{ | |
return _functionTable | |
?? (_functionTable = new Dictionary<object, Func<object, object, bool>>()); | |
} | |
} | |
/// <summary> | |
/// Invoke a function previously registered by its token value. | |
/// </summary> | |
/// <param name="token">Object token function is mapped to.</param> | |
/// <param name="arg1">First argument to function</param> | |
/// <param name="arg2">Second argument to function</param> | |
/// <returns>Nullable{bool}, true or false for result of function, null for an invalid token</returns> | |
public bool? Invoke(object token, object arg1, object arg2) | |
{ | |
Func<object, object, Boolean> function; | |
FunctionTable.TryGetValue(token, out function); | |
if (function != null) | |
return function.Invoke(arg1, arg2); | |
// call failed | |
// throw new Exception("Function not present in mapping table, check function name."); | |
return null; | |
} | |
public void AddMethod(Func<object, object, Boolean> function, object token) | |
{ | |
FunctionTable.Add(token, function); | |
} | |
public void RemoveMethod(object token) | |
{ | |
FunctionTable.Remove(token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment