Click here to Skip to main content
15,896,453 members
Articles / Web Development / ASP.NET

MVP Dependency Chaining Framework

Rate me:
Please Sign up or sign in to vote.
4.45/5 (9 votes)
14 Oct 200718 min read 46K   373   47  
An MVP framework that generates an intercepting filter layer consisting of a linked list of presenters by parsing a set of dependency rules.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

/************************************************************************************
*
* Copyright � 2007 Rohit Gadagkar
*
* This software is provided 'as-is', without any explicit or implied warranty. 
*
* Permission is granted to anyone to use this software for any purpose, including
* commercial applications, and to alter it and redistribute it freely, subject to the
* following restrictions:
*
* 1. The origin of this software must not be misrepresented. You must not claim that
* you wrote the original software. If you use this software in a product, 
* the following acknowledgement in the product documentation is required.
*
* "Portions Copyright � 2007 Rohit Gadagkar"
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
************************************************************************************/

namespace MVPChainingFactory.Factory
{
    public class PresenterSkeletonChain : LinkedList<PresenterSkeleton>
    {
        public IEnumerable<LinkedListNode<PresenterSkeleton>> Nodes
        {
            get {
                LinkedListNode<PresenterSkeleton> curr = base.First;
                while (curr != null) {
                    yield return curr;
                    curr = curr.Next;
                }
            }
        }

        /// <summary>
        /// Inserts newNode relative to the referenceNode after checking binary dependencies
        /// and chain position metadata
        /// </summary>
        /// <param name="referenceNode">usually the head or another node used as reference</param>
        /// <param name="newNode">The node to be inserted into the chain</param>
        /// <param name="newNodeBeforeReference">Initial chain positioning preference hint</param>
        public virtual void InsertPresenter(
                LinkedListNode<PresenterSkeleton> referenceNode,
                LinkedListNode<PresenterSkeleton> newNode,
                bool newNodeBeforeReference)
        {
            if(newNode.Value.PresenterPosition == Position.Head) {
                base.AddFirst(newNode);
                return;
            }
            if (newNode.Value.PresenterPosition == Position.Tail)   {
                base.AddLast(newNode);
                return;
            }
            LinkedListNode<PresenterSkeleton> target = newNodeBeforeReference
                ? referenceNode.Previous : referenceNode.Next;
            Dependency d = Dependency.NotDefined;
            if (target != null && ((d = PresenterFactory.IsRDependent(target, newNode, false)) 
                == Dependency.NotDefined))  {
                // ND => L and R nodes have the same presenter type. So move to the next
                target = target.Next;
                d = PresenterFactory.IsRDependent(target, newNode, false);
            }
            // Recurse till the correct slot is found for the new node
            if (target != null && d == Dependency.ComesAfter) {
                InsertPresenter(target, newNode, false);
                return;
            }
            if (target != null && d == Dependency.ComesBefore)  {
                base.AddBefore(target, newNode);
                AdjustLinks(newNode, target);
                return;
            }
            // If the presenter chain boundary has been reached
            // No need to check dependency for remainder next node set as it has already been done
            if(newNodeBeforeReference) base.AddBefore(referenceNode, newNode); 
            else base.AddAfter(referenceNode, newNode);
        }

        /// <summary>
        /// lNode is a new node that has been added before a rNode.
        /// This function checks the remainder nodes lying to the right of rNode
        /// to see if there are any binary relationships defined w.r.t lNode
        /// and re-adjusts pointers if there is a ComesAfter relationship.
        /// </summary>
        /// <param name="rNode">Reference node before which lNode has just been inserted</param>
        /// <param name="lNode">A new node that has just been inserted to the left of rNode</param>
        public void AdjustLinks(LinkedListNode<PresenterSkeleton> lNode,
            LinkedListNode<PresenterSkeleton> rNode)
        {
            // Head or Tail definitions override binary relationships
            if (lNode.Value.PresenterPosition != Position.Middle) return;
            LinkedListNode<PresenterSkeleton> curr = rNode.Next;
            while (curr != null) {
                // If there is no binary relationship then there is no change in the list
                // when isRAlreadyPresentInList = true as IsRDependent returns
                // ComesBefore
                if (PresenterFactory.IsRDependent(curr, lNode, true) == Dependency.ComesAfter) {
                    base.Remove(curr);
                    base.AddBefore(lNode, curr);
                    // no need to continue checking other right nodes as it is a binary relationship
                    break;
                }
                curr = curr.Next;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a tech lead working for Cap Gemini. Although I primarily work with the Microsoft technology stack (including .NET and legacy technologies) I like to keep myself informed about developments in the Java world.

Comments and Discussions