Interfaces in AL (part 2)

by Apr 6, 2020AL Language

Home 9 Development 9 AL Language 9 Interfaces in AL (part 2)

This is 2. part from the article series about Interfaces in AL Language. See the first one.

In the first part of this series, I have described what Interfaces are and showed the example of how they can be created. In this article, I will show a more advanced example of interfaces and how we can handle with these structure within Visual Studio Code.

Snippets

As usual, Microsoft added shortcut (snippet) to help with creating a basic layout of the interface object. The snippet is called “tinterface”.

I also already mentioned that I prefer (and strongly recommend) snippets from Visual Studio Code plugin “waldo’s CRS AL Language Extension” created by Waldo. However, in this case, there is nothing special from the snippet provided by Microsoft.


 // tinterface
 interface MyInterface
 {
    procedure MyProcedure();
 }

 // tinterfacewaldo
 interface IMyInterface
 {
    procedure MyProcedure();
 }

Example

Now we can look at the example. Our goal is to create functionality to import and process different types of machine inspection files. The files are of two types – BLB and XXS. Both have a completely different structure, and also data that provide are different, so they need to be processed differently.

In the old approach, we would have added if/case statement on every place where we need a different behaviour. So if we had to add another format later, we would have to change the code on these places once again. Not anymore with interfaces!

So we will create a new object of type Interface. This is the only type where we do not assign object ID.

 interface "TKA Inspection Format"
 {
    procedure LoadFile(FilePath: Text);
    procedure ProcessFile(InspectionFile: Record "TKA Inspection File"): Boolean;
 }

Now, we create Codeunit that implements our interface. To define which interface we are implementing, we use construction “Codeunit ID OBJECTNAME implement INTERFACE 1, INTERFACE 2, …”

We can see that the interface name is underlined and error messages are shown. As we use an interface, we have to define (implement…) every method from this interface(s). That can be done using “Quick Fixes” – a yellow light bulb or using a combination of Ctrl + .

 codeunit 50100 "TKA BLB Inspec. Format Mgnt." implements "TKA Inspection Format"
 {
    procedure LoadFile(FilePath: Text)
    begin
    end;
    procedure ProcessFile(InspectionFile: Record "TKA Inspection File"): Boolean
    begin
    end;
 }

Now we have a basic structure of the Codeunit that implements our interface. We can add some functionality and create another one in the same way with different functionality.

 codeunit 50100 "TKA BLB Inspec. Format Mgnt." implements "TKA Inspection Format"
 {
    procedure LoadFile(FilePath: Text)
    begin
        ImportFileFromXML(FilePath);
    end;
    procedure ProcessFile(InspectionFile: Record "TKA Inspection File"): Boolean
    begin
        DoSomething();
        DoTask1();
        exit(IsOk());
    end;
 }
 codeunit 50101 "TKA XXS Inspec. Format Mgnt." implements "TKA Inspection Format"
 {
    procedure LoadFile(FilePath: Text)
    var
        TKAFileManagement: Codeunit "TKA File Management";
    begin
        TKAFileManagement.DecryptFile(FilePath);
        while TKAFileManagement.HasNextLine() do  
            TKAFileManagement.ImportDecryptedLine(FilePath);
    end;
    procedure ProcessFile(InspectionFile: Record "TKA Inspection File"): Boolean
    begin
        DoSomethingDifferent();
        if HasError() then
            exit(false);

        DoFinalThing();
        exit(true);
    end;
 }

And finally, how to benefit from the interface. When we need to call any of the methods in the interface, we just need to define which implementation of the interface we are using.

 codeunit 50102 "TKA Some Settings"
 {
    procedure TKAInspectionFormatFactory(var TKAInspectionFile: Interface "TKA Inspection Format")
    begin
        // Get specific Implementation based on settings (for example)

        if Something() then
            TKAInspectionFile := TKABLBInspecFormatMgnt;
        else
            TKAInspectionFile := TKAXXSInspecFormatMgnt;
    end;
 }

 codeunit 50101 "TKA Some Mgnt. Object"
 {
    procedure ImportAndProcessFile()
    var
        TKAInspectionFile: Record "TKA Inspection File"

        TKAInspectionFormat: Interface "TKA Inspection Format";
        FilePath: Text;
    begin
        ...
        TKAInspectionFormatFactory(TKAInspectionFormat);
        if TKAInspectionFormat.LoadFile(FilePath) then begin
            ...
            TKAInspectionFormat.ProcessFile(TKAInspectionFile);
        end;
        ...
    end;
 }

Now we have a solution that requires only one change in method TKAInspectionFormatFactory if we need to add new supported file format.

In the next part, I will explain how to use Interface together with Enums – that is the most universal usage for an interface that is currently available in Business Central.

Recent Articles from the category

Field Level Permissions (Restricted Fields)

Field Level Permissions (Restricted Fields)

I just add a new project to my GitHub repositories. It is a new extension for Business Central that extends security functionalities with field-level permission. Using this extension, users are able to define who can or can not change specific fields in any table in...

read more
How to let users choose field(s) properly

How to let users choose field(s) properly

When some complex functionality is developed, it is sometimes necessary to let users choose a specific field. This can be required for field permissions, mapping imported values or any similar process. Earlier, the usual way was to create a link on the Field table...

read more
Returning complex types in AL Language

Returning complex types in AL Language

In Microsoft Dynamics 365 Business Central 2021 w1 (released in May 2021), a profound change was introduced to ways how we can design our applications Until this version, the only way to return complex data types (like records, codeunits, Lists etc.) was using the var...

read more
Import various file types with Interfaces

Import various file types with Interfaces

A few weeks ago, I described a solution for importing an Excel file to the Business Central using Excel Buffer in AL Language (see here). In today's article, we will look at how to build a more complex solution that can be used for file imports without the needs to...

read more