Tuesday, May 24, 2016

How to accelerate XAF Application. Part 2 Logic Controllers

This article describes how to reduce number of ViewControllers and to accelerate the XAF application.

Why to use Xafari LogicControllers

WindowController and ViewController are using to implement the behavior in the Views of XAF application. And XAF application is arranged so that all controllers are collected in a single list, and for each new Frame creates a complete copy of this list. At the same time for each controller runs its entire life cycle, from creation to deletion.

This pattern of behavior is simple and clear, but it leads to a marked slowdown in creating View, when a large number of controllers implemented in the application.

Among the set of application ViewControllers can be identified separately, which do not contain their own actions and are designed to customize the behavior of View.

Xafari Framework contains a component LogicController that is designed to replace the ViewController without actions and thus greatly reduce the number of controllers in the XAF application.

According to the possibilities of using Logic Controller fully coincides with the ViewController. Also Logic Controller can only be activated for a particular case: Target View, TargetObjectType, TargetViewId.

How it's work

To manage the life cycle of LogicController there is a special LogicControllerViewController. When this controller is activated it searches LogicControllers for a suitable environment. After that the LogicController objects are activated.
When you deactivate / delete LogicControllerViewController object also deactivated / deleted all found LogicController objects.
Detail description see in article Documentation Logic Controllers.

How to start to use LogicController

You can download trial version of Xafari Framework for latest Devexpress XAF version.

LogicController is implemented in assembly Xafari.BC.
  1. Add module Xafari.BC to your module or application.
  2. Switch on Diagnostic info like described in article Determine Why an Action, Controller or Editor is Inactive.
  3. Start application and get a small report to find all ViewControllers wich can be implemented like LogicController


    This is a sample of report:

  4. In this report you can see that you can create two LogicControllers - MainDemo.Module.Win.Controllers.WinNullTextEditorController and MainDemo.Module.Win.Controllers.WinTooltipController

How to create LogicController

1. Use Visual Studio template create new LogicController

2. Modify source code as you wish

// filter LogicController activation by TargetObjectType=Contact, TargetView=DetailView
public class WinNullTextEditorLogicController : LogicControllerBase<Contact, Detailview> 
{
 private void InitNullText(PropertyEditor propertyEditor)
 {
   ((BaseEdit)propertyEditor.Control).Properties.NullText = CaptionHelper.NullValueText;
 }

 protected override void OnActivated()
 {
   // Processing logic placed in this method when OnActivated() executing.
   // Sample logic for business object
   PropertyEditor propertyEditor = ((DetailView)View).FindItem("Anniversary") as PropertyEditor;
   if (propertyEditor != null)
   {
    if (propertyEditor.Control != null)
    {
      InitNullText(propertyEditor);
    }
    else
    {
     propertyEditor.ControlCreated += new propertyEditor_ControlCreated;
    }
  }
 }
 private void propertyEditor_ControlCreated(object sender, EventArgs e)
 {
  InitNullText((PropertyEditor)sender);
  }
}
3. Add code to module class to register created LogicController

public class MainDemoWinModule : ModuleBase 
{
 public override void Setup(XafApplication application)
 {
  base.Setup(application);
  application.SetupComplete += application_SetupComplete;
 }
 private void application_SetupComplete(object sender, EventArgs e)
 {
   LogicControllerService.Instance.Register(new WinNullTextEditorLogicController());
 }
}

Alternatively, use XafariModuleBase as parent class for your module. XafariModuleBase implements method to automatic discover all LogicControllers in current assembly.

public class MainDemoWinModule : XafariModuleBase 
{
}

4. If you replace existed ViewController do not remember to remove old code from compile process.
5. Start application to see how created LogicController works.