Observing Model Item changes

One of the customers, recently reported an issue where he was not observing the changes he had just made through the model item on the activity.

This is what he was trying:

 void modelService_ModelChanged(object sender, ModelChangedEventArgs e)
{  
   activity.Properties["Id"].SetValue(1);
   int i = (int)activity.Properties["Id"].Value;
   if (i!=1)
   {
       throw new BuggerException();
   }
}

…and this was throwing the BuggerException for him.

Well, this is actually By Design.

One of the devs on the team beautifully summed it up:

You cannot observe a change still it is commited ( editingscope.complete).

When you are inside an editing scope (the one that triggered the modelService_ModelChanged), until the editing scope completes the model changes cannot be observed.
(think of it as model item is a language to create a set of changes that get applied on editingscope complete ( if you didn’t create one explicitly, you could be inside an outer editing scope).

Also in general we discourage against reacting to model changes and causing more model changes. A model change should always be a result of a direct user action ( like button clicked etc), not another model change.

for e.g. The code the customer has will not behave correctly when an undo happens, because the undo will also cause a model change.

Hope this helps!

Thanks,

Kushal.