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.
In this repository12 other files it gives its agents.
- AGENTS.mdAGENTS.md
- authenticating-with-oauthSkill
- binding-event-subscribersSkill
- building-cue-rolecentersSkill
- consuming-bc-standard-apiSkill
- controlling-al-accessSkill
- creating-bc-custom-apiSkill
- handling-bc-filesSkill
- implementing-telemetrySkill
- integrating-azure-servicesSkill
- optimizing-bc-performanceSkill
- writing-git-commitsSkill
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
- Very specific: line 75
Create additional codeunits for each provider (<Provider2>, <Provider3>, etc.) implementing the same interface.
- Very specific: line 153
Mock for tests: Create codeunit implementing interface with configurable behavior (SetShouldSucceed, GetCallCount).
- Very specific: line 169
Limitations: Only procedures (no properties/events/variables), only codeunits implement, cannot serialize to records.
≈ “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.
Files
Skill: AL Interfaces
Validation Gates
- After Step 2: Interface compiles, implementing codeunit compiles with all methods
- After Step 3: Enum converts to interface, method calls work polymorphically
- Final: Mock codeunit works in tests,
is/asoperators 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:
| Placeholder | Description | Example |
<ServiceType> | Type of service | Payment, Shipping, Notification |
<Provider1>, <Provider2> | Provider implementations | Stripe, DHL, Email |
<Operation> | Main operation name | Payment, Shipment, Message |
<InputType> | Input parameter type | Decimal, Record "Sales Header" |
<ReturnType> | Return value type | Boolean, 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
| Pattern | Description |
| Provider/Strategy | Multiple implementations for same operation (payment, shipping) |
| Factory | exit(ProviderType) - enum implicitly converts to interface |
| Decorator | Wrap 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
| Issue | Fix |
| "does not implement interface" | Verify all interface methods exist with exact signatures |
| Enum doesn't convert to interface | Check implements clause on enum declaration |
is/as operators not available | Requires 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
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
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.

