public class Handler
Public Class Handler
type Handler = class end
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,");
}
}
}
Parameters | |
Scope | The scope of this handler |
BindEnabled | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) |
RegisterCalledTasktaskType(CachedTasktaskType) | Associates a task with the |
RegisterCalledTasktaskType(CachedTasktaskType, FuncBoolean) | Associates a task with the |
SendObjectColumnEventName | |
ToString | Returns a string that represents the current object. (Inherited from Object) |
Invokes | The Action that will be Executed when the [!:CommandBase] is invoked and all conditions are met |