I want to know how can I check for pre-requisites of a routine when I call it, so that it might execute or notify its non-executability. Language is C#.
I am implementing a Pipeline design-pattern with a Controller object, a DataContainer object, and some Task objects. When the controller asks a task for execution, it is the responsibility of each task to check for its applicability. Specifically, it should check the DataContainer object for available data.
I could imagine some ways to do it in Python:
#
if Task.can_run():
Task.run()
else:
Task.notify_controller()
#
if Controller.check_runnability(Task):
Task.run()
else:
Controller.do_something_else()
# Task.can_run() throws exception instead of returning boolean:
try:
Task.can_run();
except:
Controller.do_something_else() # or maybe Task.notify_controller()
else:
Task.run()
# not sure about syntax for the following assertion, but you get the idea
assert (Task.can_run(), Task.notify_controller())
Task.run()
What I would like to know (and didn't find with Google) is the "canonical"/most-common/"right" C# idiom for starting a routine only if some pre-requisite is met.