MDrivenWiki
Log in

Acting on object changes

From MDrivenWiki

Background

In OCL you can’t use assignment because MDriven needs to know that OCL-statements can’t change things. You 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() metod on 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 has to have IsQuery set to false, thus being EAL methods to act on the model objects.

Template for

  • Time stamp all new and updated objects
  • Setting an 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.