Select implementations with AL interfaces
Events support independent reactions, but the processing service needs exactly one selected strategy. An AL interface defines the contract that each strategy fulfills without exposing its concrete codeunit to the caller.
Define and implement a contract
An interface declares procedure signatures without implementation. A codeunit that implements the interface provides every declared procedure with a matching signature.
interface "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100];
}
codeunit 70504 "Standard Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
exit(StrSubstNo('Standard result for entry %1', Entry."Entry No."));
end;
}
codeunit 70505 "Priority Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
exit(StrSubstNo('Priority result for entry %1', Entry."Entry No."));
end;
}
The contract keeps the caller focused on the result. It also makes each implementation replaceable without a case statement that calls concrete codeunits.
Select through an enum
An enum can implement an interface and map each value to a codeunit. Assign the enum value to an interface variable, and then call the contract.
enum 70500 "Entry Process Mode" implements "Entry Processor"
{
Extensible = true;
DefaultImplementation = "Entry Processor" = "Standard Entry Processor";
UnknownValueImplementation = "Entry Processor" = "Unknown Entry Processor";
value(0; Standard)
{
Implementation = "Entry Processor" = "Standard Entry Processor";
}
value(1; Priority)
{
Implementation = "Entry Processor" = "Priority Entry Processor";
}
}
var
Processor: Interface "Entry Processor";
Processor := Entry."Process Mode";
Entry.Result := CopyStr(Processor.GetResult(Entry), 1, MaxStrLen(Entry.Result));
DefaultImplementation handles an enum value that has no explicit implementation mapping. This behavior supports an extensible enum value whose extension doesn't map the interface. UnknownValueImplementation handles an unknown enum value, such as a stored value whose defining extension is unavailable. A useful default provides conservative normal behavior. A useful unknown implementation raises a clear error instead of silently choosing behavior that might be wrong.
codeunit 70506 "Unknown Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
Error('The processing mode on entry %1 is unavailable.', Entry."Entry No.");
end;
}
Choose the simplest dependency
Use an interface when the caller needs one implementation, implementations share a meaningful contract, and selection can vary. A direct dependency remains clearer when only one stable implementation exists and substitution adds no value. An event remains a better fit when any number of subscribers independently react and the publisher doesn't need one authoritative result.
The selected strategy remains normal runtime behavior. Installation and data migration belong to separate lifecycle codeunits, which the next unit examines.
Learn more in Interfaces in AL.