HandlerScope Enumeration

Determines the scope a specific Handler is valid

Definition

Namespace: Firefly.Box
Assembly: Firefly.Box (in Firefly.Box.dll) Version: debug-master-v:33791
C#
public enum HandlerScope

Example

This example demonstrates the usage of HandlerScopeDemo
This example is in the form of Unit Tests. It references the NUnit framework. This framework can be downloaded from www.NUnit.org. For more information about unit testing visit: www.NUnit.org.
C#
using System;
using System.Collections.Generic;
using System.Text;
using Firefly.Box.Advanced;
using NUnit.Framework;
using Firefly.Box;
using Firefly.Box.Testing;

namespace TestFirefly.Box.Documentation
{
    [TestFixture]
    public class HandlerScopeDemo
    {
        [Test]
        public void HandlerWithCurrentTaskOnlyScope()
        {
            var bp = new BusinessProcess();
            var command = new CustomCommand();
            bool hmoduleened = false;
            var handler = bp.Handlers.Add(command);
            handler.Invokes += e => hmoduleened = true;
            handler.Scope = HandlerScope.CurrentTaskOnly;
            bp.ForFirstRow(() =>
            {
                var childBp = new BusinessProcess();
                childBp.ForFirstRow(() =>
                {
                    childBp.Invoke(command);
                    hmoduleened.ShouldBe(false);//The handler did not execute, because the invoke was done
                    //by childBP and not by the bp, and the scope is CurrentTaskOnly
                });
                bp.Invoke(command);//This time the handler is executed, 
                //as the  invoking BusinessProcess Is the same as the handling BusinessProcess
                hmoduleened.ShouldBe(true);
            });
        }
        [Test]
        public void HandlerWithCurrentContextScope()
        {
            var bp = new BusinessProcess();
            var command = new CustomCommand();
            bool hmoduleened = false;
            var handler = bp.Handlers.Add(command);
            handler.Invokes += e => hmoduleened = true;
            handler.Scope = HandlerScope.CurrentContext;
            bp.ForFirstRow(() =>
            {
                var childBp = new BusinessProcess();
                childBp.ForFirstRow(() =>
                {
                    childBp.Invoke(command);
                    hmoduleened.ShouldBe(true);
                });
            });
        }
        [Test]
        public void HandlerWithUnhandledCustomCommandScope()
        {//This is a demonstration of an extreme situation, with two modules and many deferent scopes
            var module1 = new ModuleController();
            var module1Bp1 = new BusinessProcess()
            {
                Module = module1
            };
            var module1Bp2 = new BusinessProcess()
            {
                Module = module1
            };
            var module2 = new ModuleController();
            var module2Bp1 = new BusinessProcess
            {
                Module = module2
            };
            var module2Bp2 = new BusinessProcess
            {
                Module = module2
            };
            var command = new CustomCommand();
            string handlingSequence = "";

            #region define handlers for all modules and tasks
            {//module1
                var handler = module1.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module1CurrentContext,";
                };

            }
            {//module2
                var handler = module2.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2CurrentContext,";
                };
            }
            {
                var handler = module2.Handlers.Add(command);
                handler.Scope = HandlerScope.UnhandledCustomCommandInModule;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2UnhandledCustomCommand,";
                };
            }
            {//module1Bp1
                var handler = module1Bp1.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module1Bp1CurrentContext,";
                };
            }
            {
                var handler = module1Bp1.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentTaskOnly;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module1Bp1CurrentTaskOnly,";
                };
            }
            {//module1Bp2
                var handler = module1Bp2.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module1Bp2CurrentContext,";
                };
            }
            {
                var handler = module1Bp2.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentTaskOnly;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module1Bp2CurrentTaskOnly,";
                };
            }
            {//module2Bp1
                var handler = module2Bp1.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2Bp1CurrentContext,";
                };
            }
            {
                var handler = module2Bp1.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentTaskOnly;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2Bp1CurrentTaskOnly,";
                };
            }
            {//module2Bp2
                var handler = module2Bp2.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentContext;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2Bp2CurrentContext,";
                };
            }
            {
                var handler = module2Bp2.Handlers.Add(command);
                handler.Scope = HandlerScope.CurrentTaskOnly;
                handler.Invokes += e =>
                {
                    e.Handled = false;
                    handlingSequence += "module2Bp2CurrentTaskOnly,";
                };
            }
            #endregion

            module1Bp1.ForFirstRow(() =>
                module1Bp2.ForFirstRow(() =>
                    module2Bp1.ForFirstRow(() =>
                        module2Bp2.ForFirstRow(() =>
                            module2Bp2.Invoke(command)
                            ))));
            handlingSequence.ShouldBe(
                                      "module2Bp2CurrentContext," +
                                      "module2Bp2CurrentTaskOnly," +
                                      "module2Bp1CurrentContext," +
                                      "module2CurrentContext," +
                                      "module1Bp2CurrentContext," +
                                      "module1Bp1CurrentContext," +
                                      "module1CurrentContext," +
                                      "module2UnhandledCustomCommand,");


        }
    }
}

Members

CurrentTaskOnly0 Only commands invoked by the current task are handled
CurrentContext1 Any command invoked by the current task, or commands that were invoked by tasks that were called by the current task
UnhandledCustomCommandInModule2 Relevant for handlers that reside in a task that inherits from ModuleController. Any command that was triggered by a task that's associated to the current task (using the Moduleproperty or the Module property) And the action was not handled by any task and any other task that are associated from ModuleController. Typically used as a default behavior for a command.

See Also