You can use this legacy pattern to retrieve only the database objects that match a value on the current ViewModel object, then expose the returned collection in a grid; use it when maintaining an existing ViewModel that calls selfVM.ExecutePS.
Note: selfVM.ExecutePS is replaced for new development by <Class>.PSEval, which is available in the context of every class. Keep this page as a reference for existing solutions. For the operator definition and current guidance, see Documentation:OCLOperators ExecutePS.
When to use this pattern
Use a persistent-search expression when the candidate set is large and you need a small matching subset. For example, if the database contains one million Thing objects, and you need the objects whose Attribute1 value matches the selected Thing, do not load every Thing into the ViewModel and filter it there.
ExecutePS evaluates its configured expression through the persistence service. The expression is translated for database execution and the result collection is returned to the ViewModel. This makes the pattern appropriate for checks such as finding other objects with the same value, or preparing a filtered collection for a grid.
Use ordinary ViewModel fetching when the relevant objects are already available or when you need to optimize how related data is fetched. See Documentation:Efficient ViewModel fetch. Use PSExpression_Refresh when you need database-computed scalar values such as a count, sum, minimum, or maximum rather than a collection of objects.
Example goal
Assume a model class named Thing with an attribute named Attribute1. The ViewModel starts with one Thing as its root object.
When the user runs the action, the ViewModel must:
- Copy the root
Thing.Attribute1value tovValueToMatch. - Evaluate a persistence-search expression that finds
Thingobjects with that value. - Store the returned collection in
vMyList. - Use
vMyListas the root for a grid.
For example, if the root Thing has Attribute1 = 'Blue', the result list contains the Thing objects found in the database whose Attribute1 is also 'Blue'.
ViewModel structure
Create or verify the following ViewModel elements. The names below follow the legacy example and make the roles clear.
| Element | Purpose | Example |
|---|---|---|
Root Thing
|
Supplies the value to match. | The currently displayed Thing has Attribute1 = 'Blue'.
|
vValueToMatch
|
ViewModel variable that holds the root value while the search is evaluated. | 'Blue'
|
OutOfLoopToAvoidStdFetch
|
ViewModel area containing the persistence-search column. Keep this column out of the UI so the expression is not evaluated as normal ViewModel fetching. | Contains col1.
|
OutOfLoopToAvoidStdFetch.col1
|
The column whose expression returns the matching Thing objects through the persistence service.
|
Returns the Thing objects whose Attribute1 equals vValueToMatch.
|
vMyList
|
ViewModel list variable that receives the collection returned by ExecutePS.
|
The matching Thing objects.
|
Grid rooted at vMyList
|
Displays the result to the user. | A grid with columns for Attribute1 and other relevant Thing data.
|
Configure the persistence-search column
- Add the non-UI ViewModel area named
OutOfLoopToAvoidStdFetch. - Add the column
col1beneath it. - Define
col1so that it returns the collection ofThingobjects with anAttribute1value equal tovValueToMatch. - Do not bind this column to a visible control. Its purpose is to hold the expression that
ExecutePSevaluates.
The expression must return objects of the type that the result list expects. In this example, it returns Thing objects, so vMyList must be a list that can hold Thing objects.
Add the action
Add an EAL action to the ViewModel. The action first captures the value from the current root object, then calls ExecutePS for the ViewModel column that contains the persistence-search expression, and finally stores the returned collection in vMyList.
The legacy operator takes the ViewModel class and ViewModel column that identify the expression to execute. In this example, those identify OutOfLoopToAvoidStdFetch.col1.
The action sequence is:
- Assign
vValueToMatchfrom the rootThing.Attribute1. - Call
selfVM.ExecutePSforOutOfLoopToAvoidStdFetch.col1. - Assign or collect the returned
Thingobjects intovMyList.
A conceptual action flow is:
vValueToMatch := <current Thing>.Attribute1;
vMyList := selfVM.ExecutePS(<ViewModel class>, <OutOfLoopToAvoidStdFetch.col1>)
Replace the placeholders with the names in your ViewModel. The important part is the ordering: set vValueToMatch before evaluating the persistence-search column, because that column uses the variable as its match value.
Display the result
Use vMyList as the root of a grid in the UI. When the action completes, the grid shows the returned Thing objects.
For the 'Blue' example, the grid should contain only Thing objects returned by the database search for Attribute1 = 'Blue'. It should not require all Thing objects to be loaded first.
Limit result size
ExecutePS checks for a MaxFetch tagged value on the root-level column and limits the returned values accordingly. Set an appropriate limit for any result that can grow large.
A uniqueness check is a common case: you usually need only enough matches to determine whether another object has the same value. Do not configure an unbounded result list when the matching value can occur on many objects.
Important considerations
- This is a legacy
selfVM.ExecutePSpattern. For new work, use<Class>.PSEvalinstead. - Keep the persistence-search expression in its dedicated, non-UI column. The column exists to be evaluated by
ExecutePS, not to drive normal ViewModel fetching. - Ensure that the expression result type and
vMyListelement type match. This example expects a collection ofThingobjects. - The returned list is a subset selected by the database expression. It is not a replacement for configuring ordinary related-object fetching where that is what the UI needs.
- Use PSExpression_Refresh for database-calculated fields such as totals and counts. It discovers columns named with the
PSExpression_prefix and refreshes their scalar results.
