Logic Controllers. Getting Started

The module, which contains the Logic Controller class should be the heir of the Xafari.BC.XafariModuleBase (instead of DevExpress.ExpressApp.ModuleBase). Be sure to refer to the Xafari.BC.dll assembly (Xafari BC Module).

Custom Logic Controller class is the descendant of the LogicControllerBase or one of its heirs. The recommended approach - this is the inheritance from the LogicControllerBase<T>, where the T - the type of business entity (similar to a standard TargetObjectType of XAF Controller). Another useful variant - it is the inheritance from the LogicControllerBase<T, TView>, where TView indicates the type of View, for which the Controller will be activated.

Logic Controller class exposes a number of members, completely similar to the standard View Controller, i.e. names, designation and signatures match completely. TargetType, TypeOfView, TargetViewId, TargetViewNesting properties specifies a View, for which a Logic Controller is activated (similar to the TargetObjectType).

The code snippet below implements EmployeeLogicController class:

  • c#
  • VB

using Xafari.BC.LogicControllers;
namespace LogicControllersSample.Module.LogicControllers
{
  public class EmployeeLogicController : LogicControllerBase<Employee>
  {
    protected override void OnDeactivated()
    {
      ShowLogicWork("OnDeactivated");
    }
    protected override void OnActivated()
    {
      ShowLogicWork("OnActivated");
    }
    protected override void OnViewControlsCreated()
    {
      ShowLogicWork("OnViewControlsCreated");
    }
    protected override void DisposeCore()
    {
      ShowLogicWork("Dispose");
      base.DisposeCore();
    }
    private void ShowLogicWork(string msg)
    {
      Tracing.Tracer.LogValue("EmployeeLogicController", msg);
    }
  }
}

Imports Xafari.BC.LogicControllers
Namespace LogicControllersSample.Module.LogicControllers
  Public Class EmployeeLogicController
    Inherits LogicControllerBase(Of Employee)
    Protected Overrides Sub OnDeactivated()
      ShowLogicWork("OnDeactivated")
    End Sub
    Protected Overrides Sub OnActivated()
      ShowLogicWork("OnActivated")
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
      ShowLogicWork("OnViewControlsCreated")
    End Sub
    Protected Overrides Sub DisposeCore()
      ShowLogicWork("Dispose")
      MyBase.DisposeCore()
    End Sub
    Private Sub ShowLogicWork(ByVal msg As String)
      Tracing.Tracer.LogValue("EmployeeLogicController", msg)
    End Sub
  End Class
End Namespace

Note

You can see the code demonstrated here in the LogicControllersSample.Module|LogicControllers|EmployeeLogicController.cs file of the the complete sample project is available at LogicControllerSample.

By default, Xafari uses reflection to find all classes of the Logic Controller in all modules referenced by the app. However, the developer has the ability to register these classes in the custom module explicitly. This way helps to optimize the initialization time because it does not use reflection to find the necessary classes.

To declare the Logic Controller explicitly, there is the important requirement the module class must satisfy: it should implement ITypesProvider<LogicControllerBase> interface. This interface declares IEnumerable<Type> GetTypes() method that must return an enumerable collection of types, each of which is heir of the Type specified as a generic argument. The code snippet below shows GetTypes() method which returns three Logic Controllers.

  • c#
  • VB

public sealed partial class LogicControllersSampleModule : ModuleBase, ITypesProvider<LogicControllerBase>
{
  /// <summary>
  /// Explicit declaration of logic controller types (not mandatory)
  /// </summary>
  IEnumerable<Type> ITypesProvider<LogicControllerBase>.GetTypes()
  {
    return new [] { typeof(EmployeeLogicController), typeof(EmployeeTwoLogicController), typeof(Employee2LogicController) };
  }
}

Public Partial NotInheritable Class LogicControllersSampleModule
  Inherits ModuleBase
  Implements ITypesProvider(Of LogicControllerBase)
  ''' <summary>
  ''' Explicit declaration of logic controller types (not mandatory)
  ''' </summary>
  Private Function GetTypes() As IEnumerable(Of Type) Implements ITypesProvider(Of LogicControllerBase).GetTypes
    Return New () { GetType(EmployeeLogicController),GetType(EmployeeTwoLogicController),GetType(Employee2LogicController) }
  End Function
End Class

To examine the Logic Controller's behavior at runtime, refer to the Logic Controllers in Application topic.