web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Why it is not recommended to put event handlers in an extension class?

(0) ShareShare
ReportReport
Posted on by 115

Hi. I've created an extension class of PriceDiscAdmCheckPost class (decorated by [ExtensionOf]) and I have placed there my custom methods, chain of commands methods and event handlers. However, as I compiled it, the best practice checker shows a warning message that says:

"Adding event handlers in extension classes is not recommended, please use instead a regular X++ class for that purpose."

I'm wondering why. I thought the goal was place every piece of custom code for one object in one extension class as much as possible. Due to this, I had to create another class that is not decorated by [ExtensionOf] just for my event handlers for that particular standard class.

If that's the case then, I'd like to ask how I would name my objects then? For my extension class, I named it "PriceDiscAdmCheckPostClassABC_Extension' where ABC is a custom model initials. But how about for my new regular X++ class that has my event handlers? I'm thinking of "PriceDiscAdmCheckPostClassEventsABC_Extension", but I'm not sure if that's the best way to name it. Please help, I can't find any other sources online regarding this matter.

Thanks.

I have the same question (0)
  • iridescence Profile Picture
    115 on at
    RE: Why it is not recommended to put event handlers in an extension class?

    Thanks for the answer. Cheers!

  • Verified answer
    Blue Wang Profile Picture
    on at
    RE: Why it is not recommended to put event handlers in an extension class?

    Hi Harvey,

    Extension of classes are named by adding ABC_Extension suffixes to the original class name.

    For implementing event handlers we should use EventHandler_ABC suffix, accompanying original name:

    Please read this Doc:https://zakharov.com/naming-conventions/application-objects/classes/

    -

    A similar thread: community.dynamics.com/.../adding-event-handlers-in-extension-classes-is-not-recommended-please-use-instead-a-regular-x-class-for-that-purpose

  • iridescence Profile Picture
    115 on at
    RE: Why it is not recommended to put event handlers in an extension class?

    Thanks for sharing. I somehow understand what he was trying to tell. Event handlers can be accidentally considered as extended methods by the compiler. I guess that answers my main question.

    But considering all of these, how would I name my new class that has event handlers only in it? Can you suggest best practices for it? Thanks in advance.

  • Verified answer
    Mea_ Profile Picture
    60,284 on at
    RE: Why it is not recommended to put event handlers in an extension class?

    Here is a blog post that explains why you should not do it https://daxmusings.codecrib.com/2018/06/accidental-code-extensions.html

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 789 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 607 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 497 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans