Click here to Skip to main content
12,395,203 members (70,284 online)
Click here to Skip to main content

Stats

29.1K views
277 downloads
46 bookmarked
Posted

MVP Dependency Chaining Framework

, 14 Oct 2007
An MVP framework that generates an intercepting filter layer consisting of a linked list of presenters by parsing a set of dependency rules.
MVPChainingFactory_demo
App_Data
Bin
MVPChainingFactory.dll
MVPChainingFactory.pdb
MVPChainingFactory.Web.dll
MVPChainingFactory.Web.dll.refresh
MVPChainingFactory.Web.pdb
StubPresentersUsecase1.dll
StubPresentersUsecase1.dll.refresh
StubPresentersUsecase1.pdb
SampleWebsite.suo
MVPChainingFactory_src
MVPChainingFactory.gpState
MVPChainingFactory.suo
MVPChainingFactory.Tests
bin
Debug
Rhino.Mocks.dll
Release
MVPChainingFactory.Tests.csproj.user
obj
Debug
Refactor
TempPE
Release
build.force
Refactor
TempPE
Properties
StubPresentersUsecase1
bin
Debug
Release
MVPChainingFactory.dll
MVPChainingFactory.pdb
StubPresentersUsecase1.dll
StubPresentersUsecase1.pdb
obj
Debug
Refactor
StubPresentersUsecase1.dll
TempPE
Release
build.force
Refactor
StubPresentersUsecase1.dll
ResolveAssemblyReference.cache
StubPresentersUsecase1.dll
StubPresentersUsecase1.pdb
TempPE
Properties
StubPresentersUsecase2
bin
Debug
Release
obj
Debug
Refactor
StubPresentersUsecase2.dll
TempPE
Release
build.force
Refactor
StubPresentersUsecase2.dll
TempPE
Properties
StubPresentersUsecase3
bin
Debug
Release
obj
Debug
Refactor
StubPresentersUsecase3.dll
TempPE
Release
build.force
Refactor
StubPresentersUsecase3.dll
TempPE
Properties
MVPChainingFactory.Web
bin
Debug
Release
obj
Debug
Refactor
TempPE
Release
TempPE
Properties
MVPChainingFactory
bin
Release
Caching
ClassDiagram.cd
Factory
MVPChainingFactory.csproj.user
Presenters
Properties
View
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

Share

About the Author

Rohit Gadagkar
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Oct 2007
Article Copyright 2007 by Rohit Gadagkar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid