Пример #1
0
        /// <summary>
        /// Starts traversal of the chain provided the chain has linked nodes, the dispatch is valid,
        /// and the dispatch hasn't already been consumed.
        /// </summary>
        /// <param name="Dispatch"><typeparamref name="T"/> dispatch to send along chain.</param>
        /// <param name="Sender">Optional reference to entity that started traversal, default is assigned to this <see cref="ChainHelper{T}"/> instance.</param>
        /// <returns>A <see cref="ReturnHelper{T}"/> instance with extra state information.</returns>
        public ReturnHelper <DispatchBase> Traverse(DispatchBase Dispatch, object Sender = null)
        {
            ReturnHelper <DispatchBase> Ret = new ReturnHelper <DispatchBase>();

            if (this._Nodes.Count < 1)
            {
                Ret.SetMessage("No nodes linked to chain.");
            }
            else if (!Dispatch.IsValid)
            {
                Ret.SetMessage("Invalid dispatch.");
            }
            else if (Dispatch.IsConsumed)
            {
                Ret.SetMessage("Process attempt on dispatch that is already consumed.");
            }
            else
            {
                if (Sender == null)
                {
                    Sender = this;
                }

                bool IsConsumable = Dispatch.IsConsumable;

                if (this._Event)
                {
                    this._Nodes[0].Process(Sender, Dispatch);

                    this.Log("Sending dispatch to " + this._Nodes[0].Key + " (v" + this._Nodes[0].Version + ") node in chain.");
                    Ret.SetMessage("Dispatch sent to " + this._Nodes[0].Key + " node.");
                }
                else
                {
                    foreach (var N in this._Nodes)
                    {
                        this.Log("Sending dispatch to " + N.Key + " (v" + N.Version + ") node in chain.");
                        Ret.SetMessage("Dispatch sent to " + N.Key + " node.");

                        N.Process(Sender, Dispatch);

                        if (IsConsumable && Dispatch.IsConsumed)
                        {
                            this.Log("Chain traversal stopped by " + N.Key + " (v" + N.Version + ") node.");
                            Ret.SetMessage("Dispatch consumed by " + N.Key + " node.");

                            break;
                        }
                    }
                }
            }

            Ret.SetResult(Dispatch);
            Ret.SetGud();

            return(Ret);
        }
Пример #2
0
 /// <summary>
 /// Entry point for dispatches being passed along a chain.
 /// </summary>
 /// <param name="Sender">Object which initiated chain traversal.</param>
 /// <param name="Dispatch">The <see cref="DispatchBase"/> to process.</param>
 public abstract void Process(object Sender, DispatchBase Dispatch);