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, ...
Suggested answer

Delegates Explanation? D365FO

(0) ShareShare
ReportReport
Posted on by 1,552

So I know that one of the benefits for delegates is to put code in the middle of a method or anywhere depending where the delegate method is called.
But I also read that it is beneficial to access code in higher model from lower model. If what I understood is right please let me know:

For example let's say I have Model A that references model B (which means the higher model here is A) then from what I understood I can use delegates to access methods in model A from model B.

so let's say model A has the following methods:

public void class methodA(int test)

{

      info("hi");

      EventHandlerResult result;

      this.methodA_delegate(test,result);

      info ("Bye");

}

delegate void methodA_Delegate(int _test, EventHandlerResult _Result)

{

}

Now in model B I will put the following:

class methodB()

{

    [SubscribesTo(classStr(methodA), delegateStr(methodA, methodA_delegate))]

    public static void methodA_methodA_Delegate(i(int _test, EventHandlerResult _Result)

    {

        _Result.result(10);

        info(strFmt("Test is %1", _Result.result()));

    }

}

Here I will get an error because I'm not referencing model A in model B ( so if I make model B references model A it will work) but they mentioned that I can access higher models from lower models so why it is not working?

I have the same question (0)
  • Evaldas Profile Picture
    1,800 on at
    RE: Delegates Explanation? D365FO

    Thanks for the link. This was probably true when extensions were weak and customizations were still allowed by over-layering, until standard code was completely sealed. Today I would not agree with this statement. You should be critical about old information and try to find a more up to date resource.

    Check this link where it is clearly stated in terms of delegates:

    docs.microsoft.com/.../extensible-code-delegates

    I can think of only one case of introducing delegates in your solution and it would be acceptable short term. I assume, you create an ISV solution and not just some functionality for the end-user.

    The case is that you have a solution and you have a complex code where you have been requested an extension point and you need it fast. Restructuring code could introduce a lot of risk and require extensive testing so a possible 'quick fix' could be a delegate.

    Other than that, follow the recommendation in the link above.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Delegates Explanation? D365FO

    If you think about it, you notice that your lower layer code can call (indirectly) the higher level code and utilize business logic from higher layer. But you are right that this works also with event handlers and CoC, and is not specific to delegates.

    Lower layer code can never call higher layer code directly since it can't see it.

    And yes, you can still write delegates if you want to offer extension point in the middle of a method. But you can also restructure your methods so that they can be extended via CoC instead. CoC is easier for the developer so it's better if your code supports CoC extensions properly.

    A rule of thumb is that one method should do exactly one task. Then it's less likely that someone would need an extension point in the middle of it.

  • junior AX Profile Picture
    1,552 on at
    RE: Delegates Explanation? D365FO

    Thanks Nicolas and Evaldas.

    Here is the link that said that and i read it in another places as well.
    https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/migration-upgrade/delegates-migration

    Here's a screenshot:

    pastedimage1571558263617v3.png

    But now if you say that i should use it to access methods from lower model? then I think there is no use for it, as i can do this using CoC or event handlers. Unless i want to put the code in a specific place in the middle of the method not just before the super or after the super.
    But Evaldas you just said that i should not be introducing delegates in my code? why is that? what if someone really wants to add code in a certain place not just before or after the super?

  • Evaldas Profile Picture
    1,800 on at
    RE: Delegates Explanation? D365FO

    Where do you read that that you can access the code in the 'higher' model? Please give a link.

    There is no point in accessing 'higher' model.

    The point of a delegate is to provide a possibility to add a custom code somewhere in the code when an event happens (delegate represents and event).

    In your case the definition of a delegate would be in Model B and if you would like to add a custom code, you create an event handler method which subscribes to the delegate in Model A (the other way around from your code, as pointed out by Nikolaos)

    The only thing, we can say Model B would access in Model A is event handler method, as it would hold a reference to it as soon as it is subscribed to the delegate.

    Other than that there is no other use of it.

    It is important to note that you should not be introducing new delegates in your code, as CoC can be much more helpful and easier.

    If your code needs a delegate, that probably means, you did it wrong and you should rethink your code. Don't mix with event handlers.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Delegates Explanation? D365FO

    You can never call higher package from lower package since there's no visibility in that direction.

    But you can put a delegate in model B (lower model). Then subscribe to it in model A (higher model). Your method in model A can now actually provide information to the delegate in model B. You have done it the other way around.

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 756 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 514 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans