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 :
Microsoft Dynamics AX (Archived)

Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

(0) ShareShare
ReportReport
Posted on by 705

Hi All,

Suppose we have a standard AX class "Class Base" which is having a method "run()". I have created a child class "Class Child" by extending "Class Base" in my own model and overridden method "run()".

Is there a way, where ever in the standard AX, base class method "run()" is being called, AX should skip this call and instead it should call my child class "run()" method.

Thanks & Regards,

Nishant Srivastava

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    235,876 Most Valuable Professional on at
    RE: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    Yes, dependency injection would allow you to instantiate another class instead of the base class, but it must be done in the code using the base class. If it's not there, you would have to change the code, which (I assume) you can't. You would have to ask the owner (Microsoft, in most cases) to make the code extensible for you.

    Here is a simple example using dependency injection:

    public void methodUsingClassBase(ClassBase _runnable)
    {
        ... do stuff ...
        _runnable.run();
        ... do stuff ...
    }


    The method doesn't create an instance of the class itself; the instance is injected to it from outside. Then you can call it with your child class:

    methodUsingClassBase(new ClassChild());

    Don't pay too much attention to the blog post if you don't understand it; it's about something called IoC containers, which is a layer above this kind of things. With IoC containers, you wouldn't explicitly create an instance of ClassChild by yourself; you would merely configur a container to use ClassChild instead of ClassBase. But we don't have that in AX.

  • Verified answer
    Nishant2408 Profile Picture
    705 on at
    RE: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    Hello Martin,

    I have read one of your blog about dependency injection.

    [View:http://dev.goshoom.net/en/tag/dependency-injection/:750:50]

    Is it possible in this way? I am bit confused on some part of the code that you have mentioned in the blog.

    I am mentioning the piece of your blog which I am not able to understand below. Can you please elaborate more on this part.

    0434.Capture123.PNG

    Please share some more information on plug-ins as well. How we can use it in D365FO.

    Regards,

    Nishant Srivastava

  • Verified answer
    Nishant2408 Profile Picture
    705 on at
    RE: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    Hi Jain,

    Thanks for the response.

    But in this perticular case suppose your Base class structure is something like -

    class Base

    {

          public void run()

          {

          <------

               //standard code

          ------->

          <------

              //Here I want to put my custom code

              //Which I don't want to achieve through overlayering thats why I have created a child class

          ------->

          <------

              // Standard Code

           ------->

          }

    }

    Child Class structure is like-

    class Child extends Base

    {

           public void run()

           {

                   //Standard code from base class

                   //My custom code

                   //Standard code from base class

           }

    }

    This particular scenario I am not able to achieve through COC. That is why looking for some other way.

    Thanks!

    Nishant

  • Suggested answer
    Martin Dráb Profile Picture
    235,876 Most Valuable Professional on at
    RE: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    If you call run() on an instance of ClassBase, it will call ClassBase.run(). Whether child classes exist or not is irrelevant.

    If you want to call ClassChild.run(), you need an instance of ClassChild (or one of its children).

    Therefore it's all about which instance gets created.

    If you have code like this:

    ClassBase c = new ClassBase();
    c.run();


    it will always call run() of ClassBase and there is nothing you can do about it (without overlayering). But there are possible designs how you could provide your own instance, e.g. if the code was using dependency injection or plugins. This is all pure object-oriented programming.

    What's special in AX is that you can change the behavior of ClassBase itself by using chain of command (or method events), which allows you to add logic to run() (but not skipping the content of run()).

  • Gautam Profile Picture
    3,945 on at
    RE: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    Hi Nishant,

    Can you try this -

       create a Base class reference Variable to derived class something like this-

    Base baseObj = new Derived();

    Thanks

    Gautam

  • Xainu Khan Profile Picture
    165 on at
    RE: Dyn365FO: Is there a way to use your child class (which is extending a standard AX Base class and overriding one of it's method) method ?

    Hi Nishant,

    You can try Chain-of-command(COC) introduced in D365 for Operations Platform upgrade 9 onward.

    Please, refer this blog to see in detail

    https://community.dynamics.com/enterprise/b/sertansdynamicsax/archive/2017/11/15/ax7-d365-chain-of-command-with-example


    Thanks
    Zain

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Mea_ Profile Picture

Mea_ 4

#3
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans