Interfaces in AL (part 3)

by Jun 22, 2020AL Language

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

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

In previous parts, I have described the basics of Interfaces in AL Language (what is an interface, how to work with them in AL and started working on advanced example).

In the last part, we will continue with our advanced example and will add Enums to use the whole power of Interfaces.

Enums

Enums are described in a separate article.

Example

So, we use our Advanced example from part 2 as a starting point hence those who haven’t read the second part yet, check it first. Now, we create Enum specifically for this example.

 enum 50100 "TKA Inspection File Type"
 {
    Extensible = true;
    
    value(0; " ")
    {
        Caption = ' ';
    }
    value(5; "TKA BLB Type")
    {
        Caption = 'BLB Type';
    }
    value(10; "TKA XXS Type")
    {
        Caption = 'XXS Type';
    }
 }

We have an Enum that is similar to the original Option Data Type. But what is the advantage of Enums? With Enums, we can specify an implementation for every value in the object using property Implementation.

Firstly, the Enum must implement an interface(s). Then for every value, we can define a Codeunit object that implements the interface for the specific value. If the Enum implements an interface all values must have concrete specification through defined Codeunit. We can use property DefaultImplementation that will be used for values that do not have implementation specification defined directly.

 enum 50100 "TKA Inspection File Type" implements "TKA Inspection Format"
 {
    Extensible = true;
    DefaultImplementation = "TKA Inspection Format" = "TKA Default Format";
    
    value(0; " ")
    {
        Caption = ' ';
    }
    value(5; "TKA BLB Type")
    {
        Implementation = "TKA Inspection Format" = "TKA BLB Inspec. Format Mgnt.";
        Caption = 'BLB Type';
    }
    value(10; "TKA XXS Type")
    {
        Implementation = "TKA Inspection Format" = "TKA BLB Inspec. Format Mgnt.";
        Caption = 'XXS Type';
    }
 }

And finally, how to use this implementation. With the old approach, there will be an Options field on a page to allow users to choose a format that they are importing. Then in the code, there will be if/case statement which decides based on selected option value which Codeunit should be run. That means if we want to add a new format, we would have to change existing code (that could be in a different extension and impossible to change!).

However, with Enums, we have an Enum field on a page to allow users to choose the format (the Enum which is extensible from other extensions). Then in the code, we create an interface variable and assign the selected value of Enum. Then we call the requested methods using the interface object. That means no change is required when we need to add new supported format.

 codeunit 50100 "TKA Import Management"
 {
    procedure ImportFile(SelectedInspectionFileType: Enum "TKA Inspection File Type"; FilePath: Text)
    var
        InspectionFormat: Interface "TKA Inspection Format";
    begin
        InspectionFormat := SelectedInspectionFileType;
        InspectionFormat.LoadFile(FilePath);
    end;
 }

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