Implementing Hooks Pattern in Microsoft Dynamics NAV

Hi guys,

Today we are going to discuss one of the interesting topic which is about “Implementing Hooks Pattern in Microsoft Dynamics NAV”. What are its significance? What do we need to use that?

So getting started, Today agenda will be

  1. Introduction
  2. Usage
  3. Steps to Implement Hooks Pattern
  4. Pros & Cons
  5. Example
  6. Conclusion

Introduction:

As a developer, adding new code to NAV means interfering with code shipped by Microsoft. Minimize your footprint of changes in Microsoft code, so that, when a new NAV version is shipped, you avoid conflicts and upgrade impact. The core NAV code is the “danger zone” – the less you touch it, the happier your upgrade will be.

When doing development over years, by different developers with different mindsets, the standard codebase gets changed a lot, adding multiple lines of code, adding local and global variants, adding or changing keys, changing existing  business logic, … .  In other terms, the standard text objects are being changed all over the place.. .

After years, it’s not clear why a change was done, and what was the place where the change was intended to be done.  And the latter is quite important in an upgrade process, when code in the base product is being refactored: if the exact place of the posting of the Customer Entry is being redesigned to a separate number, the first thing I need to know, is that I did a certain change at the place: “where the posting of the Customer Entry starts”.  The definition of that place, we call a “Hook”.

By minimizing the code in already existing application objects, you will make the upgrade process much easier, and all customization business logic will be grouped in new objects.  When using atomic coding, it will be very readable what is being customized on a certain place in an existing part of the application.

To minimize the impact of customizations, the idea of hooks is:

  • First of all, name the places in the already existing code where customization is needed;
  • Second, place your business logic completely outside the already existing application code.

Usage:

This pattern is good to apply on the following areas:

  • All objects of the default applications that need to be changed
  • On objects that should not hold any business logic (like tables, pages, XMLPorts)

Steps to Implement Hook Pattern:

Step 1 – if it doesn’t exist yet – you create your Hook Codeunit.  As the name assumes .. this is always a codeunit.  We apply the following rules to it:

  • One Hook always hooks into one object.  Which basically means that I will only declare this new codeunit in one other object (which is its parent object)
  • The naming convention is: “The_Original_Object_Name Hook”.  Naming conventions are important, just to find your mapped object, and also to be able to group the Hooks.

Step 2 – you  create the hook, which is basically a method (function) in your codeunit.  The naming is important:

  • The naming of the hook should NOT describe what it is going to do (So, examples like “CheckMandatoryFields”, “FillCustomFields” should not be used as a hook)
  • The naming of the hook should describe WHERE the hook is placed, not what the hook will be doing (as nobody is able to look into the future .. :-))
  • To help with the naming, it is a good convention to use the “On”-prefix for these triggers.  This way, it’s very clear what are hooks, and what aren’t..

Step 3 – it’s time to hook it to its corresponding object and right place in the business logic of that object.  You do this by declaring your codeunit as a global in your object, and using the created hook function on its place in the business logic.  This way, these one-liners apply:

  • A Hook Codeunit is only used once in one object only (its corresponding object)
  • A Hook (function) is used only once in that object.  As a consequence, changing the parameters has no consequence: you only need to change one function-call
  • The codeunit is declared as a global.  That exact global is the only custom declaration in the existing object .. Everything else is pushed to the hook-codeunit.

Step 4 – Implement your business logic in the hook.  Do this in the most atomic way, as there is a good chance that this same hook is going to be used for other business logic as well.  Best is to use a one-line-function-call to business logic, so that the Hook Function itself stays readable.

Pros & Cons:

Pros:

  • It will make the upgrade process much easier.
  • It will be very readable what is being customized on a certain place in an existing part of the application.
  • It is specially help full in handling large customization.
  • It easy, elegant, and very manageable.
  • Some time we can re-use the code.

Cons:

  • We have to keep Standard function and hook functions in sync always.
  • It is mostly not useable, when most changes happen in the more places in one object.
  • Making tricks and putting code in different places is making code hard to read and un-necessary complex.
  • It may increase the add-on cost because of extra objects and extra time(Assumption)
  • We can’t use in-built functions in Hook code units like currPage.xxx,
  • Test the whole functionality.
  • More investment and less benefit, because Microsoft already released merge tool from NAV2015, it saves our merging time.

Example:

Scenario:

Suppose, we want to add business logic just before posting a sales document.  In that case, we have to look for the most relevant place, which is somewhere in the “Sales-Post” codeunit.  So,

  1. Create codeunit “Sales-Post Hook”.1
  2. Create the hook function “OnBeforePostDocument”.2
  3. Declare a global in the “Sales-Post”-codeunit, called “SalesPostHook”.  Then, call the Hook Function that you created in Step 2 in the right place.3
  4. Implement the business logic, by calling out to a new function.  And implement the test-codeunit.4

Conclusion:

You create hooks on all places you don’t want to write business logic, and on all existing objects which you would like to customize.

  • This is a very consistent way of working, as in any case, your business logic ends up in either a hook, or in its corresponding objects from a specific design pattern.  But you know that the entry point is always a hook.
  • You know what to expect in any case, both changed business logic in existing code and business logic in newly created code is entered from a hook.

I hope you will like my post. Don’t forget to share it. If you have any confusion/query feel free to ask. If you want help on any specific topic feel free to contact me.

 

 

Advertisements

Author: zohaib.ahmed

I am an IT professional, a GEEK by nature who loves to get his hands dirty by experiencing new technologies in the domain of computer science. I am doing development in different domain since last 3 years. Right now, I am working as a Dynamics NAV technical consultant.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s