Breaking news from around the world
Get the Bing + MSN extension
Now Available in Community - MBAS 2019 Presentation Videos
Catch the most popular sessions on demand and learn how Dynamics 365, Power BI, Power Apps, Power Automate, and Excel are powering major transformations around the globe. | View Gallery
2019 release wave 2 Discover the latest updates and new features to Dynamics 365 planned through March 2020
Release overview guides and videos Release Plan | View virtual launch event
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants. | Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements | ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance TechTalks | Customer Engagement TechTalks | Talent TechTalks | Upcoming TechTalks
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)
delegate void methodA_Delegate(int _test, EventHandlerResult _Result)
Now in model B I will put the following:
[SubscribesTo(classStr(methodA), delegateStr(methodA, methodA_delegate))]
public static void methodA_methodA_Delegate(i(int _test, EventHandlerResult _Result)
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?
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.
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.
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:
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?
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.
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:
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.
Business Applications communities