🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators OnCreate
This page was created by Peter on 2019-11-18. Last edited by Wikiadmin on 2026-07-29.

You can use OnCreate to initialize values on each new instance of a class, for example creation timestamps and unique identifiers.

What OnCreate does

OnCreate() is a specially named method that MDriven calls when a new object has been created. Use it for values that must be set when the object is first created.

Typical uses include:

  • Setting a creation timestamp.
  • Assigning a unique identifier.
  • Initializing other values that belong to a new object.

OnCreate is similar in purpose to a .NET constructor, but it is an object event method implemented in the model.

Configure the method

To make an OnCreate method recognized as an object event:

  1. Add a method named exactly OnCreate to the class.
  2. Set IsQuery to false. This makes the method an EAL method that can change model objects.
  3. Add the initialization statements to the method body.

The name OnCreate is hard-coded. A method with another name is not treated as the create event.

Do not mark OnCreate as an IsQuery method. OCL is side-effect free, while OnCreate must use EAL to assign values to the object.

Example: set a timestamp and identifier

If the class has CreateTime and Guid attributes, use the following method body:

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

When a new instance is created, this example records the current date and time in CreateTime and assigns a new GUID to Guid. Add these attributes to the class before using this example.

Inherited OnCreate methods

A subclass can override an OnCreate method defined by its superclass. When the subclass must also perform the superclass initialization, call the inherited method with self.base.OnCreate().

For example, the subclass can run code before and after the superclass method:

self.OtherCallsBeforeCodeInOverridenClassIsDone();
self.base.OnCreate();
self.OtherCallsAfterCodeInOverridenClassIsDone()

Use the call to self.base.OnCreate() when the superclass initialization is required. Without that call, the overridden superclass OnCreate code is not included in the subclass method. See base for the inheritance call pattern.

Choose the right object event

Use OnCreate only for initialization of new objects. Use the event that matches the lifecycle point you need.

Requirement Use
Set CreateTime or a GUID when an object is created OnCreate()
Update a change timestamp before an object is saved OnUpdate
Handle deletion behavior or deletion logging OnDelete
Audit state-machine trigger calls; do not block transitions OnStateChange

See also