Typically, a business object of enterprise application is accompanied by a large number of View controllers. These controllers are implementing some specific functionality of the entity. Often it is the handlers of the following events:

  • OnDeactivated;
  • OnActivated;
  • OnViewControlsCreated.

The handlers, in turn, are subscribed to other events, and thus implement some logic, when these events occur. When using a significant amount of View controllers, there are certain difficulties:

  • When the form of the persistent object opens, all instances of ViewController will be created.
  • All forms in the application will open slowly because it takes the time to create a large number of View Controllers.
  • When you open a form, excessive consumption of RAM occurs as a large number of View Controllers are loaded into the memory.

The problem with a large number of View Controllers can be solved using Logic Controllers. Logic Controllers occupy an intermediate position between the business object and View Controllers. Instead of using a set of View Controllers, you are encouraged to use set of Logic Controllers, In order to handle events, all Logic Controllers require only one View Controller.

Such behavior can be implemented using the special service and View Controller. Service shall register Logic Controllers and manage their set, and a special View Controller routes occurred events to Logic Controllers associated with some type.

Implementation

Let's become familiar with LogicControllerSample solution. To use Logic Controller execute the following steps:

Add Xafari Module to LogicControllersSample.Module.

Implement EmployeeLogicController class derived from LogicControllerBase< T >. The following code demonstrates this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Xafari.LogicController;
 
namespace LogicControllersSample.Module.LogicControllers
{
    public class EmployeeLogicController : LogicControllerBase
    {
        public override void OnDeactivated(LogicControllerViewController controller)
        {
            ShowLogicWork("OnDeactivated");
        }
 
        public override void OnActivated(LogicControllerViewController controller)
        {
            ShowLogicWork("OnActivated");
        }
 
        public override void OnViewControlsCreated(LogicControllerViewController controller)
        {
            ShowLogicWork("OnViewControlsCreated");
        }
 
        private void ShowLogicWork(string msg)
        {
            Tracing.Tracer.LogValue("EmployeeLogicController", msg);
        }
    }
}

To register Logic Controllers, override Setup method as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using Xafari.LogicController;
 
namespace LogicControllersSample.Module 
{
    public sealed partial class LogicControllersSampleModule : ModuleBase {
        public LogicControllersSampleModule() {
            InitializeComponent();
        }
 
        ...
 
        public override void Setup(XafApplication application) {
            base.Setup(application);
            LogicControllerService.Register(typeof(EmployeeLogicController), () => new EmployeeLogicController());
            LogicControllerService.Register(typeof(EmployeeTwoLogicController), () => new EmployeeTwoLogicController());
        }
    }
}

As you can see, we use LogicControllerService static class. Register method receives two parameters:

  • Logic Controller type
  • delegate returns an instance of the specified type.

Demonstration of work

Now you can build the application and start debugging. Add Employee object.

Xafari Logic Controller

Let’s examine Trace records when Employee List View opens:

1
2
3
4
5
6
7
12.02.15 16:05:04.923    EmployeeLogicController: OnActivated
 
12.02.15 16:05:04.924    EmployeeTwoLogicController: OnActivated
 
12.02.15 16:05:04.988    EmployeeLogicController: OnViewControlsCreated
 
12.02.15 16:05:04.989    EmployeeTwoLogicController: OnViewControlsCreated

Open-Close Employee Detail View and see Trace:

1
2
3
4
5
6
7
12.02.15 16:10:11.618    Window closing: Ivan - Employee
 
12.02.15 16:10:11.622    EmployeeLogicController: OnDeactivated
 
12.02.15 16:10:11.623    EmployeeTwoLogicController: OnDeactivated
 
12.02.15 16:10:11.624    Window closed: Ivan – Employee

CallStack contains:

1
2
3
LogicControllersSample.Module.dll!LogicControllersSample.Module.LogicControllers.EmployeeTwoLogicController.OnDeactivated(Xafari.LogicController.LogicControllerViewController controller)
 
Xafari.dll!Xafari.LogicController.LogicControllerViewController.OnDeactivated()

I.e. the methods are called by a special controller supplied with Xafari. Thus, it is possible to control the behavior of the object via the events using only one View Controller. Logic Controllers should be divided according to their intended use.

Write US