agentleFS
emgreppiemgreppi/Business-Central-AI-Skill/implementing-al-interfaces/SKILL.md

implementing-al-interfaces

Skill1 starsChanged 9 months agoMITView on GitHub

Defines AL interfaces and interface patterns with implementing codeunits, creates extensible enums to switch providers, and enables dependency injection for unit testing AL code with mocks. Use when building plugin architectures, adding loose coupling, replacing conditional logic with polymorphism, or creating Business Central extensions with swappable implementations.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Create additional codeunits for each provider (<Provider2>, <Provider3>, etc.) implementing the same interface.

    line 75
  2. Very specific:

    Mock for tests: Create codeunit implementing interface with configurable behavior (SetShouldSucceed, GetCallCount).

    line 153
  3. Very specific:

    Limitations: Only procedures (no properties/events/variables), only codeunits implement, cannot serialize to records.

    line 169

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Skill: AL Interfaces

Validation Gates

  1. After Step 2: Interface compiles, implementing codeunit compiles with all methods
  2. After Step 3: Enum converts to interface, method calls work polymorphically
  3. Final: Mock codeunit works in tests, is/as operators behave correctly (BC25+)

Note: BC16+ for basic interfaces. BC25+ for is/as operators and extensible interfaces.

Output: Interface declaration, implementing codeunits, extensible enum, and usage pattern.

Procedure

Step 1: Declare Interface

interface "<PREFIX> I<ServiceType> Provider"
{
    procedure Execute<Operation>(InputData: <InputType>): <ReturnType>;
    procedure Cancel<Operation>(TransactionId: Text; InputData: <InputType>): <ReturnType>;
    procedure GetProviderName(): Text;
}

Placeholder Reference:

PlaceholderDescriptionExample
<ServiceType>Type of servicePayment, Shipping, Notification
<Provider1>, <Provider2>Provider implementationsStripe, DHL, Email
<Operation>Main operation namePayment, Shipment, Message
<InputType>Input parameter typeDecimal, Record "Sales Header"
<ReturnType>Return value typeBoolean, Text

Step 2: Create Implementing Codeunits

codeunit <ID> "<PREFIX> <Provider1> <ServiceType>" implements "<PREFIX> I<ServiceType> Provider"
{
    procedure Execute<Operation>(InputData: <InputType>): <ReturnType>
    begin
        exit(Call<Provider1>API(InputData));
    end;

    procedure Cancel<Operation>(TransactionId: Text; InputData: <InputType>): <ReturnType>
    begin
        exit(Call<Provider1>CancelAPI(TransactionId, InputData));
    end;

    procedure GetProviderName(): Text
    begin
        exit('<Provider1>');
    end;

    local procedure Call<Provider1>API(InputData: <InputType>): <ReturnType>
    begin
        // Provider-specific logic
    end;

    local procedure Call<Provider1>CancelAPI(TransactionId: Text; InputData: <InputType>): <ReturnType>
    begin
        // Provider-specific logic
    end;
}

Create additional codeunits for each provider (<Provider2>, <Provider3>, etc.) implementing the same interface.

Step 3: Create Enum with Implements

enum <ID> "<PREFIX> <ServiceType> Provider" implements "<PREFIX> I<ServiceType> Provider"
{
    Extensible = true;

    value(0; <Provider1>)
    {
        Implementation = "<PREFIX> I<ServiceType> Provider" = "<PREFIX> <Provider1> <ServiceType>";
    }
    value(1; <Provider2>)
    {
        Implementation = "<PREFIX> I<ServiceType> Provider" = "<PREFIX> <Provider2> <ServiceType>";
    }
}

Step 4: Use Interface Polymorphically

codeunit <ID> "<PREFIX> <ServiceType> Manager"
{
    procedure Process<Operation>(var SourceRecord: Record <SourceTable>): Boolean
    var
        ProviderType: Enum "<PREFIX> <ServiceType> Provider";
        IProvider: Interface "<PREFIX> I<ServiceType> Provider";
    begin
        ProviderType := SourceRecord."<PREFIX> <ServiceType> Provider";
        IProvider := ProviderType;  // Implicit conversion

        if IProvider.Execute<Operation>(SourceRecord.<InputField>) then begin
            SourceRecord."<PREFIX> <Operation> Processed" := true;
            SourceRecord.Modify();
            exit(true);
        end;
        exit(false);
    end;
}

BC25+ Features: is/as Operators

// Check if interface supports specific type, then convert
if IProvider is "<PREFIX> IAdvanced<ServiceType>" then begin
    IAdvanced := IProvider as "<PREFIX> IAdvanced<ServiceType>";
    exit(IAdvanced.Partial<Operation>(TransactionId, InputData, '<Reason>'));
end;
exit(IProvider.Cancel<Operation>(TransactionId, InputData));

Multiple interfaces: codeunit <ID> "..." implements "I<ServiceType>", "IAdvanced<ServiceType>", "I<ServiceType>Status"

Dependency Injection for Testing

Pattern: Store interface as codeunit variable, inject via Initialize(Provider) procedure.

codeunit <ID> "<PREFIX> <Entity> Processor"
{
    var
        ServiceProvider: Interface "<PREFIX> I<ServiceType> Provider";

    procedure Initialize(Provider: Interface "<PREFIX> I<ServiceType> Provider")
    begin
        ServiceProvider := Provider;
    end;

    procedure Process<Entity>(var SourceRecord: Record <SourceTable>): Boolean
    begin
        exit(ServiceProvider.Execute<Operation>(SourceRecord.<InputField>));
    end;
}

Mock for tests: Create codeunit implementing interface with configurable behavior (SetShouldSucceed, GetCallCount).

See references/interface-patterns.md for complete mock implementation example.

Common Patterns

PatternDescription
Provider/StrategyMultiple implementations for same operation (payment, shipping)
Factoryexit(ProviderType) - enum implicitly converts to interface
DecoratorWrap interface to add logging, validation, caching

Important Notes

Best practices: Small focused interfaces, Extensible = true for partner extensions, use enums for selection.

Limitations: Only procedures (no properties/events/variables), only codeunits implement, cannot serialize to records.

Troubleshooting

IssueFix
"does not implement interface"Verify all interface methods exist with exact signatures
Enum doesn't convert to interfaceCheck implements clause on enum declaration
is/as operators not availableRequires BC25+ (2024 Wave 2)

Feedback loop: Fix signature → Re-compile → Verify method calls work polymorphically.

References

See references/ folder for:

  • interface-patterns.md - Additional code examples

External Documentation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.