Acting on object changes
No edit summary
(Automatically adding template at the end of the page.)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==== Background ====
==== 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.
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.


All viewmodel expressions are side effect free and therefore OCL. Only actions and methods work on the object and classes of the model, not the viewmodel expressions, which defines a projection.
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 ====
==== 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.
In the default model of the designer, you will find the methods OnCreate() and OnUpdate() with the content from the template below.


[[File:OnCreate and OnUpdate methods.png|frameless|778x778px]]
[[File:OnCreate and OnUpdate methods.png|frameless|778x778px]]


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.
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.
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.
OnCreate, OnUpdate, and OnDelete all need to have IsQuery set to false, thus becoming EAL methods to act on the model objects.


==== Template for ====
==== Template For ====
* Time stamp all new and updated objects
* Time stamp all new and updated objects
* Setting an unique identifier on all objects
* Setting a unique identifier on all objects
* Logging deleted objects to an "archive class" holding the Guid of the deleted object
* Logging deleted objects to an "archive class" holding the Guid of the deleted object
In the method body for OnCreate set this ocl:
In the method body for OnCreate, set this OCL:
  CreateTime:=DateTime.Now;
  CreateTime:=DateTime.Now;
  self.Guid.newGuid()  
  self.Guid.newGuid()  
In the method body for OnUpdate set this ocl:
In the method body for OnUpdate, set this OCL:
  self.ChangeTime:=DateTime.Now
  if self.existing then -- This avoids touching the object if it has been deleted
In the method body of OnDelete set this ocl:
  self.ChangeTime:=DateTime.Now; false
else
  false
endif
In the method body of OnDelete, set this OCL:
  let deleted = DeletedObj.Create in
  let deleted = DeletedObj.Create in
  (
  (
    deleted.DeletedGuid := self.Guid
    deleted.DeletedGuid := self.Guid
  )  
  )  
'''Note!''' OnCreate, OnUpdate and OnDelete are hard-coded names, you have to use these function names.
'''Note!''' OnCreate, OnUpdate, and OnDelete are hard-coded names, you have to use these function names.
[[Category:Actions]]
[[Category:OCL]]
[[Category:OCL]]
[[Category:EAL]]
[[Category:EAL]]
{{Edited|July|12|2024}}

Latest revision as of 15:26, 10 February 2024

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 91 days ago on 02/10/2024. What links here