Custom Apply

One of the use cases is to design a Numerator in the Application Model and then to apply it in code. This topic describes this scenario.

Note

The complete sample project is available in BusinessNumerators. You can view the code used in this lesson in the BusinessNumerators.Module|Controllers|NumerateByActionViewController.cs file.

Thus, we assume that Updater creates 15 objects as you can see in the code snippet below:

  • c#
  • VB

public class Updater : ModuleUpdater
{
  //...
  private void CreateNumerateByActionObject()
  {
    if (this.ObjectSpace.GetObjects<NumerateByAction>().Any())
      return;
    for (var i = 1; i <= 15; i++)
    {
      var obj = ObjectSpace.CreateObject<NumerateByAction>();
      obj.Name = "Object_" + i;
    }
  }
}

Public Class Updater
  Inherits ModuleUpdater
  '...
  Private Sub CreateNumerateByActionObject()
    If Me.ObjectSpace.GetObjects(Of NumerateByAction)().Any() Then
      Return
    End If
    Dim i = 1
    While i <= 15
      Dim obj = ObjectSpace.CreateObject(Of NumerateByAction)()
      obj.Name = "Object_" + i
      i += 1
    End While
  End Sub
End Class

These objects contain the Number property, the value of which is generated by the Action using the Numerators service.

numerator_36

First, design the Numerator via the Model Editor in Visual Studio. To do it, navigate to the Numerators node and set the AutoApply property's value to "False".

numerator_37

Then, go to the Xafari|Services|Numerators node, add a new item, and set its properties as shown in the picture below.

numerator_33

Link the Numerator to the required object as follows.

numerator_34

The Numerators service will process the required object when the Apply method is called. For instance, it may be done be the Action's ..._Execute event handler. The code snippet below demonstrates the numerateSelectedObjects_Execute event handler that is meant to enumerate all selected objects on the List View.

  • c#
  • VB

private void numerateSelectedObjects_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  var selected = this.View.SelectedObjects;
  if (selected == null || selected.Count == 0)
    return;
  foreach (var obj in selected)
    this.View.ObjectSpace.Xafari().Numerators().Apply(obj);
  this.View.ObjectSpace.CommitChanges();
}

Private Sub numerateSelectedObjects_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  Dim selected = Me.View.SelectedObjects
  If selected Is Nothing OrElse selected.Count = 0 Then
    Return
  End If
  For Each obj In selected
    Me.View.ObjectSpace.Xafari().Numerators().Apply(obj)
  Next
  Me.View.ObjectSpace.CommitChanges()
End Sub

The images below show an objects collection before and after executing the Action:

numerator_35