The upcoming Dynamics 365 Business Central 2020 Wave 1 release (version 16) introduces the concept of Interfaces in AL.
An interface is used when you want to decide which capabilities need to be available for an object, while allowing actual implementations to differ, as long as they comply with the defined interface. This allows for writing code that reduces the dependency on implementation details, makes it easier to reuse code, and supports a polymorphing way of calling object methods, which again can be used for substituting business logic.
With the new AL language version, you can use the new interface object to declare an interface name along with its methods, and apply the implements keyword along with the interface names on objects that implement the interface methods. The interface object itself does not contain any code, only signatures, and cannot itself be called from code, but must be implemented by other objects. The compiler checks to ensure implementations adhere to assigned interfaces.
A great post that describes interfaces in AL was written by Tobias Fenster some weeks ago and you can find it here.
As I’ve described in my previous post, Microsoft is using the interface object in the new version 16 codebase on different places and the first place that comes to my eyes when building my apps for version 16 was on the sales price calculation module.
Dynamics 365 Business Central 2020 Wave 1 (or version 16) has a totally new way of handling sales prices based on AL Interfaces. If you check for example the OnValidate event of the Quantity field of the Sales Line table, now you can see that the new Interface “Price Calculation” object is used:
In this OnValidate trigger, to retrieve the price for the Sales Line record the AL code calls a GetPriceCalculationHandler procedure as follows:
Inside this procedure, there’s a call to a GetHandler method (defined in the Price Calculation Mgt. codeunit) by passing the interface:
The GetHandler method retrieves from the Price Calculation Setup table the value of the Implementation field and then call the Init method defined in the interface itself:
This field is an enum field type called Price Calculation Handler:
and the enum is defined as follows:
As you can see, you can extend it with your own implementation’s values.
The PriceCalculation interface is defined as follows (contract):
What happens in the new 2020 Wave 1 codebase? A marvellous thing… the new price calculation implementation based on interfaces permits you to define your custom price logic by implementing the PriceCalculation interface on a separate codeunit. If you want to add your custom price management logic to Dynamics 365 Business Central, now you don’t have to modify code or interact with events raised by the standard, but simply add your new interface implementation (codeunit) and add it on the Price Calculation Setup table.
The standard codebase has two implementations for the PriceCalculation interface: Price Calculation – V15 (logic until Dynamics 365 Business Central version 15) and Price Calculation – V16 (the new logic for Dynamics 365 Business Central version 16). This is how Price Calculation – V16 is implemented:
and this is the the implementation for Price Calculation – V15 (as you can see, it’s declared as ObsoleteState = Pending):
To create your custom price calculation in your extension, just add a new value in the Implementation field in the Price Calculation Setup table with an enumextension object:
and then create a new codeunit inside your extension by implementing the Price Calculation interface and add your custom logic in the ApplyDiscount and ApplyPrice methods:
Very clean and elegant… 😉