/// <summary> /// Exit a scope for gated code /// </summary> /// <param name="context">the gate context</param> /// <param name="mode">mode that code should be performed (e.g. scoped or not)</param> /// <param name="gate">the gate to exit scope for</param> public static void ExitScope(this IGateContext context, GatedCode.Modes mode, IGate gate) { if (context.ShouldBeScoped((mode & GatedCode.Modes.ExitScope), gate)) { context.ExitScope(gate); } }
/// <summary> /// Executes the given function in the scope of the gates associated with it /// </summary> /// <remarks> /// All code invoked by the function will consider the gates active. /// </remarks> /// <typeparam name="T">type of the function's result</typeparam> /// <param name="gateContext">gate context</param> /// <param name="gatedFunction">gated function to execute</param> /// <returns>result returned by the function</returns> public static T PerformGatedFunctionInScope <T>(this IGateContext gateContext, GatedFunc <T> gatedFunction) { foreach (IGate gate in gatedFunction.Gates.Gates) { gateContext.EnterScope(gate); } try { return(gatedFunction.Func == null ? default(T) : gatedFunction.Func()); } finally { foreach (IGate gate in gatedFunction.Gates.Gates) { gateContext.ExitScope(gate); } } }
/// <summary> /// Runs the given action in the scope of the gates associated with it /// </summary> /// <remarks> /// All code invoked by the action will consider the gates active. /// </remarks> /// <param name="gateContext">gate context</param> /// <param name="gatedAction">gated action to run</param> public static void PerformGatedActionInScope(this IGateContext gateContext, GatedAction gatedAction) { foreach (IGate gate in gatedAction.Gates.Gates) { gateContext.EnterScope(gate); } try { gatedAction.Action?.Invoke(); } finally { foreach (IGate gate in gatedAction.Gates.Gates) { gateContext.ExitScope(gate); } } }