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.