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;
 }

Leave a Reply

Your email address will not be published. Required fields are marked *