Action Executor. Getting Started

In this lesson we will consider at a simple example of applying ActionExecutor.

Let's look at the the following task:

There are two persistent types: FirstObjectType and SecondObjectType. Both types have FirstValue, SecondValue, and ThirdValue properties. There are also one Simple Action, named «Execute». After Simple Action executing in the FirstObjectType context the ThirdValue must be equal to FirstValue+SecondValue. After Simple Action executing in the SecondObjectType context the ThirdValue must be equal to FirstValue-SecondValue.

Firstly, let's design an application business model with Domain Components.

  • The basic Domain Component contains the common properties
  • c#
  • VB

[DomainComponent]
[DefaultClassOptions]
public interface BaseType : IActionExecutorProvider
{
  int FirstValue { get; set; }
  int SecondValue { get; set; }
  int ThirdValue { get; set; }
}

<DomainComponent> _
<DefaultClassOptions> _
Public Interface BaseType
  Inherits IActionExecutorProvider
  Property FirstValue As Integer
  Property SecondValue As Integer
  Property ThirdValue As Integer
End Interface

  • FirstObjectType implements IActionExecutorProvider interface
  • c#
  • VB

[DomainComponent]
[DefaultClassOptions]
[NavigationItem(false)]
public interface FirstObjectType : BaseType
{
}
[DomainLogic(typeof(FirstObjectType))]
public class FirstObjectTypeLogic
{
  public static IActionExecutor CreateActionExecutor(FirstObjectType instance)
  {
    return new FirstObjectTypeActionExecutor(instance);
  }
}

<DomainComponent> _
<DefaultClassOptions> _
<NavigationItem(False)> _
Public Interface FirstObjectType
  Inherits BaseType
End Interface
<DomainLogic(GetType(FirstObjectType))> _
Public Class FirstObjectTypeLogic
  Public Shared Function CreateActionExecutor(ByVal instance As FirstObjectType) As IActionExecutor
    Return New FirstObjectTypeActionExecutor(instance)
  End Function
End Class

  • SecondObjectType implements IActionExecutorProvider interface
  • c#
  • VB

[DomainComponent]
[DefaultClassOptions]
[NavigationItem(false)]
public interface SecondObjectType : BaseType
{
}
[DomainLogic(typeof(SecondObjectType))]
public class SecondObjectTypeLogic
{
  public static IActionExecutor CreateActionExecutor(SecondObjectType instance)
  {
    return new SecondObjectTypeActionExecutor(instance);
  }
}

<DomainComponent> _
<DefaultClassOptions> _
<NavigationItem(False)> _
Public Interface SecondObjectType
  Inherits BaseType
End Interface
<DomainLogic(GetType(SecondObjectType))> _
Public Class SecondObjectTypeLogic
  Public Shared Function CreateActionExecutor(ByVal instance As SecondObjectType) As IActionExecutor
    Return New SecondObjectTypeActionExecutor(instance)
  End Function
End Class

As you can see, the Domain Components have different implementations of CreateActionExecutor() method.

  • The logic of ThirdValue calculation will be implemented in the FirstObjectTypeActionExecutor and SecondObjectTypeActionExecutor  classes.
  • c#
  • VB

public class FirstObjectTypeActionExecutor : IActionExecutor
{
  private readonly IObjectSpace _objectSpace;
  private readonly FirstObjectType _context;
  public FirstObjectTypeActionExecutor(FirstObjectType context)
  {
    _objectSpace = Xafari.XafariModule.Application.CreateObjectSpace();
    _context = _objectSpace.GetObject(context);
  }
  public void OnFinalized(EventArgs e)
  {
    var handler = Finalized;
    if (handler != null)
      handler(this, e);
  }
  public void Execute(SimpleAction sender, SimpleActionExecuteEventArgs e)
  {
    _context.ThirdValue = _context.FirstValue + _context.SecondValue;
    _objectSpace.CommitChanges();
    e.ShowViewParameters.CreatedView = Xafari.XafariModule.Application.CreateDetailView(_objectSpace, _context, true);
    e.ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow;
    this.OnFinalized(new EventArgs());
  }
  public void DoExecute()
  {
    _context.ThirdValue = _context.FirstValue + _context.SecondValue;
    _objectSpace.CommitChanges();
    this.OnFinalized(new EventArgs());
  }
  public event EventHandler Finalized;
}
public class SecondObjectTypeActionExecutor : IActionExecutor
{
  private readonly IObjectSpace _objectSpace;
  private readonly SecondObjectType _context;
  public SecondObjectTypeActionExecutor(SecondObjectType context)
  {
    _objectSpace = Xafari.XafariModule.Application.CreateObjectSpace();
    _context = _objectSpace.GetObject(context);
  }
  public void OnFinalized(EventArgs e)
  {
    var handler = Finalized;
    if (handler != null)
      handler(this, e);
  }
  public void Execute(SimpleAction sender, SimpleActionExecuteEventArgs e)
  {
    _context.ThirdValue = _context.FirstValue - _context.SecondValue;
    _objectSpace.CommitChanges();
    e.ShowViewParameters.CreatedView = Xafari.XafariModule.Application.CreateDetailView(_objectSpace, _context, true);
    e.ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow;
    this.OnFinalized(new EventArgs());
  }
  public void DoExecute()
  {
    _context.ThirdValue = _context.FirstValue - _context.SecondValue;
    _objectSpace.CommitChanges();
    this.OnFinalized(new EventArgs());
  }
  public event EventHandler Finalized;
}

Public Class FirstObjectTypeActionExecutor
  Implements IActionExecutor
  Private ReadOnly __objectSpace As IObjectSpace
  Private ReadOnly __context As FirstObjectType
  Public Sub New(ByVal context As FirstObjectType)
    __objectSpace = Xafari.XafariModule.Application.CreateObjectSpace()
    __context = __objectSpace.GetObject(context)
  End Sub
  Public Sub OnFinalized(ByVal e As EventArgs)
    Dim handler = Finalized
    If handler IsNot Nothing Then
      handler(Me, e)
    End If
  End Sub
  Public Sub Execute(ByVal sender As SimpleAction, ByVal e As SimpleActionExecuteEventArgs)
    __context.ThirdValue = __context.FirstValue + __context.SecondValue
    __objectSpace.CommitChanges()
    e.ShowViewParameters.CreatedView = Xafari.XafariModule.Application.CreateDetailView(__objectSpace, __context, True)
    e.ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow
    Me.OnFinalized(New EventArgs())
  End Sub
  Public Sub DoExecute()
    __context.ThirdValue = __context.FirstValue + __context.SecondValue
    __objectSpace.CommitChanges()
    Me.OnFinalized(New EventArgs())
  End Sub
  Public Event Finalized As EventHandler
End Class
Public Class SecondObjectTypeActionExecutor
  Implements IActionExecutor
  Private ReadOnly __objectSpace As IObjectSpace
  Private ReadOnly __context As SecondObjectType
  Public Sub New(ByVal context As SecondObjectType)
    __objectSpace = Xafari.XafariModule.Application.CreateObjectSpace()
    __context = __objectSpace.GetObject(context)
  End Sub
  Public Sub OnFinalized(ByVal e As EventArgs)
    Dim handler = Finalized
    If handler IsNot Nothing Then
      handler(Me, e)
    End If
  End Sub
  Public Sub Execute(ByVal sender As SimpleAction, ByVal e As SimpleActionExecuteEventArgs)
    __context.ThirdValue = __context.FirstValue - __context.SecondValue
    __objectSpace.CommitChanges()
    e.ShowViewParameters.CreatedView = Xafari.XafariModule.Application.CreateDetailView(__objectSpace, __context, True)
    e.ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow
    Me.OnFinalized(New EventArgs())
  End Sub
  Public Sub DoExecute()
    __context.ThirdValue = __context.FirstValue - __context.SecondValue
    __objectSpace.CommitChanges()
    Me.OnFinalized(New EventArgs())
  End Sub
  Public Event Finalized As EventHandler
End Class

  • Add ObjectExecutorViewViewController derived from ActionExecutorViewController. Add the Simple Action to the ObjectExecutorViewViewController.
  • c#
  • VB

public ObjectExecutorViewViewController()
{
  InitializeComponent();
  this._execute = new DevExpress.ExpressApp.Actions.SimpleAction(this.components) { Caption = "Execute", ConfirmationMessage = null, Id = "Execute", ImageName = null, Shortcut = null, Tag = null, TargetObjectsCriteria = null, TargetViewId = null, ToolTip = null, TypeOfView = null };
  RegisterActions(components);
}
private DevExpress.ExpressApp.Actions.SimpleAction _execute;
protected override SimpleAction GetActionCore()
{
  return this._execute;
}

Public Sub New()
  InitializeComponent()
  Me._execute = New DevExpress.ExpressApp.Actions.SimpleAction(Me.components) With {.Caption = "Execute", .ConfirmationMessage = Nothing, .Id = "Execute", .ImageName = Nothing, .Shortcut = Nothing, .Tag = Nothing, .TargetObjectsCriteria = Nothing, .TargetViewId = Nothing, .ToolTip = Nothing, .TypeOfView = Nothing}
  RegisterActions(components)
End Sub
Private _execute As DevExpress.ExpressApp.Actions.SimpleAction
Protected Overrides Function GetActionCore() As SimpleAction
  Return Me._execute
End Function

  • To see the result, run the WinForms or ASP.NET application

You can download the solution used in this lesson.