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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Extension of AX class constructor

(0) ShareShare
ReportReport
Posted on by

Hello guys,

I would appreciate if someone can suggest a way of migrating to AX7 a code from AX2012 which overlayered class constructor. I mean new() method. In the constructor an initialization of existing class variable was changed. The class has public accessor method for the variable.

It appears that I can not implement Post event handler for the New() method as an error is raised: 'cannot be subscribed to by a Pre- or Post-method event handler. The target cannot be a constructor'.

Also I can not implement it in the constructor of the extension class as per the documentation there is no guarantee that extension constructor would be called if no extension method is used:  "It's guaranteed that the constructor that is provided in an extension class will be called one time before any instance method or the instance state on the extension class is accessed. However, if no such references are made, the constructor isn't called"

It seems to me that not supporting events for class constructor limits usage of extensions which suggested by Microsoft as main upgrade mechanism.

kind regards,

Ivan

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    The constructor in class extensions is intended for initialization of fields added in the extension. If you don't use any methods of the extension, there is no point to initialize the fields, as they wouldn't ever be used.

    Shouldn't you simply call a method to change the state of the class? If you can't do it because the it's not exposed in the public interface, it's quite a different problem.

    Also, consider if you can't use normal object-oriented techniques, such as inheritance (member variables are protected by default in AX, therefore you could change the value in an overridden constructor).

    It really depends on the exact context.

  • Community Member Profile Picture
    on at

    Hi Martin,

    thank you for the reply!

    I am trying to solve the issue using extension approach. The problem is that the class which has been mentioned, in AX7 belongs to ApplicationFoundation. So no way to do it even with customization. It actually clear that touching ApplicationFoundation is a bad idea but same situation is quite possible in ApplicationSuite, e.g.

    Using an inheritance is actually a good idea which can be used in some cases, sadly not in mine.

    Yes, the class has public accessor method for the variable. But I have no control on a code flow. And how simple is somehow to change the initialization done in constructor and be sure that no side-effects exists or will exists after the code upgrade, than track current AX code to find method(s) which in existing code flow is being invoked more or less right after constructor and then create Pre event handler(s) for those method in the extension class to initialize the variable. And also in this case usage of extension would not be safe against further code upgrades (like a new code path will be added, not considered by me and I got into a trouble).

    Still for me possibility of adding just Post method event for new() would be a good idea which enhance extension approach used for code upgrade.

    /regards,

    Ivan

  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    I'm sorry, but I can't help to deal with your particular scenario, because I have almost no information about it. You would have to tell me what exactly you're trying to achieve. There isn't a single universal answer how to redesign any initialization to extensions and whether it's possible at all or your business requirement can be achieved by some different technical solution must be evaluated on a case by case basis.

    If there truly isn't any way how you achieve your business scenario with the current implementation, you'll have to ask Microsoft to redesign the class to make it extensible. For example, if other classes currently call the constructor directly, Microsoft could replace it with a factory method (e.g. construct) and call a delegate which you can subscribe to and call the accessor method from your handler.

  • Community Member Profile Picture
    on at

    thank you!

  • Anjaney Profile Picture
    424 on at

    Hi IvanG,

    I am facing same issue while trying to implement Post event handler for New method, Please suggest if you got to resolve this issue. 

    Thanks in advance 

    Anjaneya chaturvedi

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans