Trigger or Callback?
Hi, folks!
Consider I need to perform some logic which should fire because values of object properties has been changed.
Is there ANY reason for using class callbacks %OnBeforeSave or %OnAfterSave instead of Trigger?
Hi, folks!
Consider I need to perform some logic which should fire because values of object properties has been changed.
Is there ANY reason for using class callbacks %OnBeforeSave or %OnAfterSave instead of Trigger?
%On... callbacks are served and integrated into he OBJECT world and typically don't care about any Trigger.
Triggers live in the SQL TABLE world of your class, with a hand full off %-variables unknown at the Object side
and without an actual instance of object but directly writing to Globals.Take a look to the generated .INT code of your class and see what you have at hands.I always found it quite ambitious to attempt a common code of both sides when directly changing Objects or Tables.
Summary:- for SQL access have Triggers
- for Object access have your %On* methods
Applies only before 2014.1 as pointed out.
But Trigger code doesn't look much better.
Thanks, I missed that leaving before
And I've seen the new generated code added to %Save()
It confirmed my feelings about %* ....
Thanks, Danny!
Actually, I raised already similar discussion but I still prefer object callbacks because of better code readability.
BTW, if I have both unified Trigger and OnBeforSave callback would they fire both?
And why don't we have unified ObjectScript callbacks?
Since 2014.1 we have 'unified' Triggers in Caché : if you use 'Foreach = row/object' in your trigger, it will be called from SQL and Object operations, a lot easier to write common code :
See also in the docs : http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...
SQL Code and it's style and methodology was there long before Objects in actual style where introduced. (feeling mid 90ties)
It was mainly kept for backward compatibility.
In Caché 5 (2002) they were a real parallel universe Object - SQL.
Later Computed + SQLcomputed Properties were for long time the only place for common code.