Acting on object changes

Background

In OCL, you can’t use assignments because MDriven needs to know that OCL-statements can’t change things. Always use EAL to change the objects of your model.

Also, all ViewModel expressions are side effect free and are therefore OCL. Only actions and methods work on the object and classes of the model, not the ViewModel expressions, which defines a projection.

Acting on Object Events

In the default model of the designer, you will find the methods OnCreate() and OnUpdate() with the content from the template below.

OnCreate and OnUpdate methods.png

If you need a copy of something, for example, to log a previous state and therefore don’t want it derived, you can add an OnUpdate() method to the class.

Note that methods on classes can be “queries” or not. Methods marked as “IsQuery” are OCL, and therefore side-effect free and can be used in other OCL statements.

OnCreate, OnUpdate, and OnDelete all need to have IsQuery set to false, thus becoming EAL methods to act on the model objects.

Template For

  • Time stamp all new and updated objects
  • Setting a unique identifier on all objects
  • Logging deleted objects to an "archive class" holding the Guid of the deleted object

In the method body for OnCreate, set this OCL:

CreateTime:=DateTime.Now;
self.Guid.newGuid() 

In the method body for OnUpdate, set this OCL:

if self.existing then -- This avoids touching the object if it has been deleted
  self.ChangeTime:=DateTime.Now; false
else
  false
endif 

In the method body of OnDelete, set this OCL:

let deleted = DeletedObj.Create in
(
  deleted.DeletedGuid := self.Guid
) 

Note! OnCreate, OnUpdate, and OnDelete are hard-coded names, you have to use these function names.

This page was edited more than 6 months ago on 06/17/2024. What links here