You can define behavior on a class by adding a method, implementing it in its Body property, and choosing whether the method is a side-effect-free query or an action that changes model data.
Methods on classes
A method defines behavior that an instance of a class can perform. Add methods when the behavior belongs to the model object rather than to a ViewModel.
For example, a Thing class can define MyMethod and callers can invoke it for each Thing instance. A method may accept values as parameters and may return a value or a collection.
Choose query or action behavior
The IsQuery flag determines how MDriven treats the method body.
| Method setting | Body language and behavior | Where you can use it |
|---|---|---|
| IsQuery = true | The Body is OCL. It must be side-effect free: it must not intentionally change model data. | In other OCL expressions, including derivations and collection expressions. |
| IsQuery = false | The Body is EAL (the action language). It may change model objects. | In actions and other contexts that perform work on model objects. It is not recognized as an OCL query. |
Set IsQuery to true only when you promise that the method has no intentional side effects. This lets MDriven evaluate the method as part of an OCL expression.
For example, if MyMethod is marked IsQuery, and it returns a Boolean value, you can use it as the predicate in a selection:
Thing.allinstances->select(x|x.MyMethod(x.SomeInt))
Here, Thing.allinstances supplies all Thing instances. The select expression keeps each instance for which MyMethod(x.SomeInt) evaluates to true.
If the same method is not a query, MDriven treats its Body as EAL because it may have side effects. It cannot be used in this OCL expression.
Define a method
- Add the method to the class that owns the behavior.
- Enter the method name, parameters, and return type in the method signature.
- Set IsQuery according to whether the method must remain side-effect free.
- Enter the implementation in the method's Body property.
- Call a query method from OCL when its return value is needed. Call a non-query method from an action when the method must change model data.
A method that changes attributes, creates objects, or otherwise acts on model objects must have IsQuery = false. For examples of action-language methods that respond to object lifecycle events, see Documentation:Acting on object changes. In particular, OnCreate, OnUpdate, and OnDelete are action methods and must not be queries.
Method signatures
Write the method name followed by its parameters in parentheses. Add a colon and the return type when the method returns a value.
| Purpose | Signature example |
|---|---|
| One String parameter; returns a String | SomeMethod(myparam:String):String
|
| A String and an Integer parameter; returns a String | SomeMethod(myparam:String,SomeOtherParam:Integer):String
|
| Accepts a collection of String values; returns a String | SomeMethod(myparam:String,SomeOtherParam:Integer,aListOfValue:Collection(String)):String
|
| Accepts String and Integer values plus a collection of String values; returns a collection of String values | SomeMethodThatReturnsList(myparam:String,SomeOtherParam:Integer,aListOfValue:Collection(String)):Collection(String)
|
Accepts a collection of instances of SomeClass; returns a collection of SomeClass instances
|
SomeMethodThatReturnsList(myparam:String,SomeOtherParam:Integer,aListOfValue:Collection(SomeClass)):Collection(SomeClass)
|
Use Collection(TypeName) when a parameter or return value contains multiple values. For example, Collection(String) represents a collection of strings, while Collection(SomeClass) represents a collection of objects of SomeClass.
Calling methods
Call an instance method on the object that owns it. In the earlier selection example, x is the current Thing object, so the call is written x.MyMethod(x.SomeInt).
Keep the method's return type aligned with how you call it:
- Use a Boolean-returning query where OCL requires a condition, such as the predicate supplied to
select. - Use a scalar return type, such as
String, when the caller needs one value. - Use
Collection(TypeName)when the caller needs multiple values or objects.
Related method implementations
MDriven also supports methods supplied by other mechanisms:
- Documentation:Pattern supported methods lists model-pattern methods with built-in server support.
- Documentation:Calling your own c - sharp .net things from Turnkeyâserver side explains how to route a declared method to your own server-side .NET implementation in Turnkey. Such a method can be used in a derivation only when it is marked as a query.
- Documentation:Application actions covers actions initiated from the application and ViewModel context. Keep class-method behavior focused on the model object it belongs to.
