Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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.zip
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.zip
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.Generic;
using System.Text;
using System.Reflection;
using MVPChainingFactory.Presenters;
using MVPChainingFactory.View;
using System.Web;
using MVPChainingFactory.Caching;

/************************************************************************************
*
* 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 static class PresenterFactory
    {
        internal const string CACHE_KEY_PRESENTER_METADATA = "__$PresenterMetadataKey";
        internal const string CACHE_KEY_PRESENTER_CHAIN = "__$PresenterChainKey";
        
        private static 
            ICacheItemManager<Dictionary<Type, ConstructorInfo>> _presenterMetadataCacheMgr;
        private static ICacheItemManager<PresenterSkeletonChain> _presenterChainCacheMgr;
        private static string _presenterAssembly;
        private static object _syncLock;

        static PresenterFactory()
        {
            _syncLock = new object();            
        }

        public static string PresenterAssembly
        {
            get { return _presenterAssembly; }
            set { _presenterAssembly = value; }
        }

        // CacheManager can be mocked with these properties

        public static 
            ICacheItemManager<Dictionary<Type, ConstructorInfo>> PresenterMetadataCacheManager
        {
            get { return _presenterMetadataCacheMgr; }
            set { 
                _presenterMetadataCacheMgr = value;
                _presenterMetadataCacheMgr.CacheDependencyAssembly = PresenterAssembly;
                _presenterMetadataCacheMgr.CacheInvalidated += OnCacheInvalidated<Dictionary<Type, ConstructorInfo>>;
            }
        }

        public static ICacheItemManager<PresenterSkeletonChain> PresenterChainCacheManager
        {
            get { return _presenterChainCacheMgr; }
            set { 
                _presenterChainCacheMgr = value;
                _presenterChainCacheMgr.CacheDependencyAssembly = PresenterAssembly;
                _presenterChainCacheMgr.CacheInvalidated += OnCacheInvalidated<PresenterSkeletonChain>;
            }
        }

        public static PresenterSkeletonChain PresenterSkeletonChain
        {
            get { return GetPresenterSkeletonChain(); }
        }

        public static void InitializeFactory(
            ICacheItemManager<Dictionary<Type, ConstructorInfo>> presenterMetadataCacheManager,
            ICacheItemManager<PresenterSkeletonChain> presenterChainCacheManager,
            string presenterAssemblyName)
        {
            PresenterAssembly = presenterAssemblyName;
            PresenterMetadataCacheManager = presenterMetadataCacheManager;
            PresenterChainCacheManager = presenterChainCacheManager;            
        }

        public static Presenter BuildPresenterChain(IView boundView, PresenterChainMode modeOfChaining)
        {
            if (_presenterChainCacheMgr == null 
                || _presenterMetadataCacheMgr == null
                || String.IsNullOrEmpty(_presenterAssembly))
                throw new InvalidOperationException(@"Either the (PresenterChainCacheManager,PresenterMetadataCacheManager, PresenterAssembly) properties must be set or the method InitializeFactory(..) must be called before invoking BuildPresenterChain.");
            PresenterSkeletonChain list = PresenterSkeletonChain;
            Presenter head = null, curr = null, prev = null;
            Dictionary<Type, ConstructorInfo> ctorInfo = GetPresenterMetadata();
            foreach (LinkedListNode<PresenterSkeleton> node in list.Nodes) {
                Type presenterType = node.Value.PresenterType;
                ConstructorInfo ctor = ctorInfo[presenterType];
                if (ctor != null)
                {
                    curr = ctor.Invoke(new object[] { boundView, modeOfChaining }) as Presenter;
                    if (head == null) { head = prev = curr; }
                    else { prev.Next = curr; prev = curr; }
                }
            }
            return head;
        }

        internal static void OnCacheInvalidated<T>(string key, T value)
        {
            switch (key)
            {
                case CACHE_KEY_PRESENTER_METADATA:
                    GetPresenterMetadata();
                    break;
                case CACHE_KEY_PRESENTER_CHAIN:
                    GetPresenterSkeletonChain();
                    break;
            }            
        }

        internal static Dictionary<Type, ConstructorInfo> GetPresenterMetadata()
        {
            Dictionary<Type, ConstructorInfo> ctorInfo = _presenterMetadataCacheMgr.Get();
            // Allow only the first thread to update the cache to avoid race condition
            // If a second thread is blocked over the double check null lock, it 
            // waits till the primary thread restores data into the cache 
            if (ctorInfo == null) {
                lock (_syncLock)
                {
                    if (_presenterMetadataCacheMgr.Get() == null) ScanPresenters();
                    ctorInfo = _presenterMetadataCacheMgr.Get();
                }
            }
            return ctorInfo;
        }

        internal static PresenterSkeletonChain GetPresenterSkeletonChain()
        {
            PresenterSkeletonChain list = _presenterChainCacheMgr.Get();
            // Allow only the first thread to update the cache to avoid race condition
            // If a second thread is blocked over the double check null lock, it 
            // waits till the primary thread restores data into the cache 
            if (list == null)
            {
                lock (_syncLock)
                {
                    if (_presenterChainCacheMgr.Get() == null) ScanPresenters();
                    list = _presenterChainCacheMgr.Get();
                }
            }
            return list;
        }

        /// <summary>
        /// Cache the Presenter's constructor
        /// Create a presenter skeleton node that stores the type of the underlying presenter
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ctorCache"></param>
        /// <returns></returns>
        internal static LinkedListNode<PresenterSkeleton> ConstructSkeletonNode(
                                Type presenterType, 
                                Dictionary<Type, ConstructorInfo> ctorCache)
        {
            // Get the overloaded constructor of the presenter while building the chain
            // the view is initialized later on at runtime using the View property
            ConstructorInfo ci = ctorCache.ContainsKey(presenterType)
                ? ctorCache[presenterType]
                : presenterType.GetConstructor(new Type[] { typeof(IView),typeof(PresenterChainMode)});
            if (ci == null) {
                string err = @"The presenter implementation {0} has an 
                                invalid ctor() signature";
                throw new InvalidOperationException(String.Format(
                    err, presenterType.FullName));
            }
            // update cache
            ctorCache[presenterType] = ci;
            return new LinkedListNode<PresenterSkeleton>
                (new PresenterSkeleton(presenterType));
        }

        internal static Position GetPresenterPositionFromAttrib(Type t)
        {
            PresenterPositionAttribute pp = GetPresenterPositionMetadata(t);
            return (pp != null ? pp.PresenterChainPosition : Position.Middle);
        }

        internal static PresenterPositionAttribute GetPresenterPositionMetadata(Type t)
        {
            object[] attrs = t.GetCustomAttributes(
                                typeof(PresenterPositionAttribute), false);
            if (attrs != null && attrs.Length > 0)
                return (PresenterPositionAttribute)attrs[0];
            return null;
        }

        internal static void ScanPresenters()
        {
            Dictionary<Type, ConstructorInfo>  metadata = new Dictionary<Type, ConstructorInfo>();
            PresenterSkeletonChain presenterList = new PresenterSkeletonChain();
            int headCount = 0, tailCount = 0;
            lock (_syncLock)
            {
                Assembly a = Assembly.Load(PresenterAssembly);
                foreach (Type t in a.GetTypes())
                {
                    Type baseType = t.BaseType;
                    if (t.IsClass)
                    {
                        while (baseType != typeof(Object) && baseType != null)
                        {
                            if (baseType == typeof(Presenter) && baseType.IsAbstract)
                            {
                                // Validate PresenterPositionAttribute definitions
                                Position p = GetPresenterPositionFromAttrib(t);
                                ValidateHeadOrTailDefinition(ref headCount, ref tailCount, p);
                                LinkedListNode<PresenterSkeleton> current = 
                                    ConstructSkeletonNode(t, metadata);

                                LinkedListNode<PresenterSkeleton> head = presenterList.First;
                                if (head == null) presenterList.AddFirst(current);
                                else
                                {
                                    if (IsRDependent(current, head, true) == Dependency.ComesAfter)
                                    {
                                        presenterList.AddBefore(head, current);
                                        // if current has ComesAfter relationship with nodes
                                        // which have no relationship w.r.t head
                                        presenterList.AdjustLinks(current, head);
                                        break;
                                    }
                                    else presenterList.InsertPresenter(head, current, false);
                                }
                                
                            }
                            baseType = baseType.BaseType;
                        }
                    }
                }
                // Cache the results
                _presenterChainCacheMgr.Put(presenterList);
                _presenterMetadataCacheMgr.Put(metadata);
            }
        }


        // If true, sets L as head of R
        // R is a new element introduced into the dependency chain
        // return false by default to queue up R if no explicit dependency has been defined
        internal static Dependency IsRDependent(PresenterSkeleton l, PresenterSkeleton r, bool isRAlreadyPresentInList)
        {
            // Cannot define dependencies between instances of the same type of Presenter
            if (l.PresenterType.FullName == r.PresenterType.FullName)
                return Dependency.NotDefined;
            Dependency d = InferRDependentFromPositionMetadata(r, true);
            if (d != Dependency.NotDefined) return d;
            d = InferRDependentFromPositionMetadata(l, false);
            if (d != Dependency.NotDefined) return d;

            PresenterPositionAttribute rAtr = GetPresenterPositionMetadata(r.PresenterType);
            PresenterPositionAttribute lAtr = GetPresenterPositionMetadata(l.PresenterType);
            Type rComesAfter = rAtr == null ? null : rAtr.ComesAfter;
            Type lComesAfter = lAtr == null ? null : lAtr.ComesAfter;
            if (rComesAfter != null && rComesAfter.FullName == l.PresenterType.FullName)
                return Dependency.ComesAfter;
            if (lComesAfter != null && lComesAfter.FullName == r.PresenterType.FullName)
                return Dependency.ComesBefore;

            return isRAlreadyPresentInList ? Dependency.ComesBefore : Dependency.ComesAfter;
        }

        internal static Dependency IsRDependent(
            LinkedListNode<PresenterSkeleton> l, LinkedListNode<PresenterSkeleton> r, bool isRAlreadyPresentInList)
        {
            return l.Value == null || r.Value == null 
                ? Dependency.NotDefined : IsRDependent(l.Value, r.Value, isRAlreadyPresentInList);
        }

        internal static Dependency InferRDependentFromPositionMetadata(PresenterSkeleton node, bool isRNode)
        {
            PresenterPositionAttribute atr = GetPresenterPositionMetadata(node.PresenterType);
            Position pos = atr == null
                ? Position.Middle : atr.PresenterChainPosition;
            if (pos == Position.Head)
            {
                node.PresenterPosition = Position.Head;
                // if L node is the head, R is a dependent
                return isRNode ? Dependency.ComesBefore : Dependency.ComesAfter;
            }
            else if (pos == Position.Tail)
            {
                node.PresenterPosition = Position.Tail;
                // if L node is the tail, R comes before L
                return isRNode ? Dependency.ComesAfter : Dependency.ComesBefore;
            }
            return Dependency.NotDefined;
        }

        internal static bool IsMarkedHeadOfChain(PresenterSkeleton p)
        {
            PresenterPositionAttribute atr = GetPresenterPositionMetadata(p.PresenterType);
            Position pos = atr == null
                ? Position.Middle : atr.PresenterChainPosition;
            return (pos == Position.Head);
        }

        private static void ValidateHeadOrTailDefinition(ref int headCount, ref int tailCount,
            Position p)
        {
            switch (p)  {
                case Position.Head:
                    headCount++;
                    break;
                case Position.Tail:
                    tailCount++;
                    break;
            }
            // There can be only 1 occurrence of Head or Tail 
            if (headCount > 1)
                throw new PresenterPositionException( Position.Head, headCount);
            if (tailCount > 1)
                throw new PresenterPositionException( Position.Tail, headCount);
        }
    }
}

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 14 Oct 2007
Article Copyright 2007 by Rohit Gadagkar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid