Galaktikasoft

Xafari Logic Controller

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:

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:

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.

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.

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:

Demonstration of work

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

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

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:

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:

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.