bcquality/microsoft/knowledge/events/prefer-reusing-or-extending-existing-events.md
Jesper Schulz-Wedde faeacb2484 Add 12 general AL event-design articles to events domain
Add 12 atomic knowledge articles under microsoft/knowledge/events covering
general AL event-design best practices: IsHandled initialization and OnAfter
preservation, appending new event parameters, position-based event naming,
reusing/extending events, avoiding per-iteration publishing, Temp-prefixing
temporary record parameters, unabbreviated parameter names, preferring the
this keyword over IncludeSender, avoiding loosely typed parameters, not
mutating existing event contracts, and not bypassing critical operations
with IsHandled. Each article ships a .good.al and .bad.al demonstration
sample (object IDs 50240-50296; not compiled by CI). Extend the
al-events-review leaf Worklist with one targeted check per new rule.

Additive only; no contract or wiring change (events leaf already wired).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-25 11:58:59 +02:00

1.7 KiB

bc-version domain keywords technologies countries application-area
all
events
event-reuse
duplication
consecutive-events
extension-point
onbefore
integration-event
maintainability
al
w1
all

Prefer reusing or extending existing events

Description

Before adding a publisher, check whether an event already fires at that point in the code. Two related smells signal that you should reuse or extend instead of adding one. The first is a brand-new event placed directly next to an existing one — two consecutive event raises with no logic between them, which gives subscribers two seams where one belongs. The second is a near-duplicate event that differs from an existing one only by an extra parameter. Both bloat the publisher surface and leave subscribers unsure which event to pick. Prefer subscribing to the existing event, or extending it by appending the parameter you need, over introducing a parallel one.

Best Practice

When the data you need is already exposed at an existing event, subscribe to it. When the event lacks a parameter, extend that event by appending the parameter at the end — one publisher, one raise — rather than adding a second event beside it.

See sample: prefer-reusing-or-extending-existing-events.good.al.

Anti Pattern

Adding a second event raise immediately after an existing one, or creating OnBeforeProcessOrderWithCustomer next to OnBeforeProcessOrder just to add a single parameter. Detection: two consecutive OnBefore…/OnAfter… raises with no logic between them, or near-duplicate event names differing only by a parameter-describing suffix.

See sample: prefer-reusing-or-extending-existing-events.bad.al.