How it works...

The Handled pattern allows you to define pieces of code that you will allow a subscriber to bypass. This is done by raising an event before the piece of code, and using a variable parameter that the subscriber can set to notify the application that the rest of the code should be bypassed. If there are no subscribers that set the flag to bypass, then the original code will continue to be executed.

This is extremely useful in the scenarios where you have processes or calculations that may not be a one-size-fits-all kind of thing.

Using our recipe example, by subscribing to the OnBeforeGetLastDirectUnitCost() event, a developer can perform their own logic to calculate the last direct unit cost, such as, maybe they only want to look at documents from a specific time period. Once the calculation is performed, their logic will set IsHandled := true so that the original code will not be executed and overwrite their custom calculation.

Caution!
Care needs to be taken with this pattern. Since you cannot guarantee that the subscriber will actually handle the functionality properly, you need to make sure to verify that any critical data is handled correctly.

You don't want to create a situation where your function is bypassed and it leads to incomplete or incorrect data.
This pattern can also be used in reverse. Instead of using a Boolean value to bypass your code, you can use it to ensure that there is an active subscriber to the event that performs some logic.

In this scenario, instead of skipping your code if the flag is not set, you might throw an error (or some other handling).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.82.217