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

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here