Hooks

While the Façade has to be explicitly implemented to allow different code paths at runtime, there might also be scenarios where we are required to make changes to the code that we don't own. In these scenarios, we can implement a hook, which is nothing more than a function call that executes business logic outside of the code, but inside the transaction. The alternative to implementing hooks is raw source code modification, including creating additional variables.

The advantages of implementing hooks can be found in the area of upgradability. It does not increase maintainability, mainly because the name of the hook does not indicate what the hook does, but where it is called from. Hence we can see in the original source that there is a hook, but we need to use a Go To definition in order to see what business logic is executed.

Let's look at hooks with the help of an example:

Hooks

Remember, we only implement hooks when we don't want to disturb the original source code. In this example, we have a posting routine. Instead of adding code to this routine, we call out to a hook at a certain point. The hook is named accordingly.

We need to make sure that the code is called with the required parameters, and return the changed values when required.

Disabling the original source code

In some scenarios, we maybe required to disable large portions of original source code. If this code is not encapsulated into a single function, the anti-pattern would be to comment out a large block of code.

Instead of doing this, we need to ensure that the original code still works when our modification is disabled. We can do this by adding an enabled flag to the setup of our module. If the module is disabled, we run the original code path.

This will also allow the automatic merge routines that Microsoft ships as part of the development tools to understand the intention of what we want to do. Another benefit is being able to execute the standard test routines.

Predefined hooks

Hooks are not only used by developers who want to make modifications to the code, but also to the code that they don't own. When an architect expects others to make changes to certain processes, they can also implement Predefined hooks.

Note

This video explains the implementation of the Predefined hooks. You can view it at https://www.youtube.com/watch?v=cGaBqwfGCws&list=PLhZ3P-LY7CqmVszuvtJLujFyHpsVN0U_w&index=9.

An example of a Predefined hook can be found in the localization for North America. This localization has Codeunit 10201 and Transfer Custom Fields, and is called from all the large posting routines where Dynamics NAV creates Entry tables.

Predefined hooks
..................Content has been hidden....................

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