Wednesday, June 27, 2012

What would happen to my event handlers, if the sys-layer method disappeared?

The other day I explained to my colleagues, what the benefits of events and models, introduced in AX 2012, were.

That time, I was asked a question "What would happen to my event handlers, if the method they are attached to in the AOT was removed in the next release?" I could not give a precise answer, but I was sure that nothing terrible would happen in that case.

Let's check what would actually happen. First, let's create 2 models, Bands and Fans:


Now, let's add class TheBeatles to the Bands model, and class Fans to the Fans model. On top of that, we will create two event handers in the Fans model, so Fans would shout whenever TheBeatles gave a concert, and buy albums whenever TheBeatles recorded one:



More details:





If you run TheBeatles::main method now, the infolog will look like this:


Let's export the Fans model to save the original version of the Fans class:


OK. Let's assume now that TheBeatles stopped giving concerts (hell, no...). We have to delete the giveConcert method from TheBeatles class:


As we can see, the fansShoutEventHandler is gone, too. More than that, it does not exist in the model either:


But, we have our exported Fans model. Let's import it back and see what will happen:


The import failed, as the model file referenced a non-existing element, giveConcert method. Let's run the same import command, but with "/createparents" parameter, as it is proposed in the error message:


As we can see, a virtual model has been created to help the Fans model get imported properly. In the AOT, TheBeatles class looks like this now:


giveConcert method is there again, now in the "Fans (Virtual 1)" model.

In the end, we can decide what to do with the fansShoutEventHandler - remove it at all, move it somewhere else or maybe refactor.

For more information on how the events and models can save you time, I would recommend this blog.