Spread the love

Interfaces are one of those things that most developers that have worked with other systems than NAV has always been hoping for, and now we finally got them, although in a limited edition since to get the most out of interfaces your programming language has to be OOP, which AL is not, one can only hope for the future 😉 

All source code can be found here: https://dfredborg.visualstudio.com/Business%20Central/_git/Business%20Central?version=GBinterfaces

So, what is an interface? 🤔 Well the name kind of gives it away, it is a concept that will allow you to protect your code, by giving other functions the ability to implement your interface instead of calling the underlying code directly. It is also a way to implement a strong contract, forcing other people to implement everything the way that you have intended.

In Business Central the real power in interfaces is that it allows you extend your code more easily, one of the most common problems that we have faced in AL is that you have not been able to extend the option type, to solve this we got the Enum data type, which was a step in the correct direction, but we still had a problem if we wanted to be able to change what happened when a certain option was chosen. With interfaces, you now could extend your enums and their functionality, so let us look at how you can do this. 

First thing you must do is create a new object of type Interface and add at least one function, now it is important to note that interfaces can only contain functions with no code, but only as a signature. And you should also note, that whenever one wishes to implement your interface, they must implement all the functions, in your interface object. In the code below I have created an interface called IGetMyCode with a simple function GetColor that returns a text: 

interface "IGetMyColor"
{
  procedure GetColor(): Text;
}

Next, we need to create some codeunits that implements our interface, this is done by adding the implements IGetMyColor, and it must contain a function with the same name as the interface which was GetColor, I have created three simple codeunits Red, Blue, and Green that just exits with a text value. 

codeunit 50100 Red implements IGetMyColor
{
    procedure GetColor(): Text
    begin
        exit('Red');
    end;  
 
}


codeunit 50101 Blue implements IGetMyColor
{
    procedure GetColor(): Text
    begin
        exit('Blue');
    end;  
 

}
codeunit 50102 Green implements IGetMyColor
{
    procedure GetColor(): Text
    begin
        exit('Green');
    end;  
 

}

Next, we need to implement our interface in our enum object, you must add an Implementation where you define which function you wish to implement and which version of the interface that you wish to implement. 

enum 50100 "Colors" implements IGetMyColor
{
    Extensible = true;


    value(0; Blue)
    {
        Caption = 'Blue';
        Implementation = IGetMyColor = Blue;
    }


    value(1; Red)
    {
        Caption = 'Red';
        Implementation = IGetMyColor = Red;


    }


    value(2; Green)
    {
        Caption = 'Green';
        Implementation = IGetMyColor = Green;

    }
}

And that is it, I know this was a very simple implementation of interfaces, and you can do a lot more with interfaces than this example if you are interested in interfaces and to see what you can use them for, please consider taking a look at how Microsoft has implemented interfaces in the Sales Price function. 

I am really looking forward to seeing how people are going to start using Interfaces across their extensions to make the code easier to integrate and to see if Microsoft is going to implement even more concepts from OOP 🤞. Well until next time stay safe.  😷

Leave a Reply