Business Central AL Interfaces
THE AL “INTERFACE” OBJECT
A new object type is available in Business Central 2020 Wave1: “interface” object
Since I have never talked about it, I speak of it now, I don’t report particular examples as there are already several published.
Below are some links to very well made examples, useful for understanding how to best use them. Enjoy the reading.
ABOUT “INTERFACES”
An interface is used when you want to decide which features should be available for an object, while allowing actual implementations to differ as long as they conform to the defined interface.
This allows you to write code that reduces dependence on implementation details, facilitates code reuse, and supports a polymorphic way of calling object methods, which can be used again to replace business logic.
Use the new interface object to declare an interface name with its methods and apply the implementation keyword along with the interface names on the objects that implement the interface methods.
The interface object does not contain any code, only signatures, and cannot be called from the code but must be implemented by other objects. The compiler verifies that implementations adhere to the assigned interfaces.
A new CodeAction QuickFix can be used to insert interface stubs, if compiler errors are missing on one or more interface implementations. Variables can be declared as a specific interface to allow objects that implement the interface to pass and then call interface implementations on the object passed in a polymorphic way.
“In Microsoft AL the interface is a new object type in which you could define that procedures should exist in objects that implement that interface.”
Snippet: “tinterface”
Typing the shortcut tinterface will create the basic layout for an interface object when using the AL Language extension in Visual Studio Code.
MICROSOFT EXAMPLE
Interface “IVehicle”
INTERFACE PAGE ON MICROSOFT DOCS
TESTING
** You need runtime “5.0”
An interface objects contains only the declared procedure, the body does not exist.
Interface “Test”
Procedure PrintRndNumber();
- Procedure with declaration only
Now, we need to declare the body of the procedure in a codeunit that implements this interface
Codeunit “test”
Procedure PrintRndNumber();
-
- Procedure with body
…and now we can test the New Interface object
Example
On ItemList page extension, we can recall the interface after declaration on VAR section.
F5 or install the APP
ANOTHER EXAMPLE (by Microsoft)
TESTING
Example link here https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-interfaces-in-al
https://github.com/rstefanetti/AL-Samples/blob/AL-Interfaces/intDemoInterface.al
ANOTHER GREAT EXAMPLES HERE
Great example here by Tobias Fester
https://tobiasfenster.io/interfaces-in-al-and-why-that-matters