Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Building an MVP Framework for .NET. Part 2: Implementing Core Functionality

Rate me:
Please Sign up or sign in to vote.
4.82/5 (25 votes)
11 Feb 2008CPOL12 min read 90.2K   1.3K   89  
Basing on the concepts introduced in the first part, this article proceeds to implement the core MVP Framework funtionality.
//===========================================
// MVC# Framework | www.MVCSharp.org        |
// ------------------------------------------
// Copyright (C) 2008 www.MVCSharp.org      |
// All rights reserved.                     |
//===========================================

using System;
using System.Text;
using System.Xml;
using System.Reflection;

namespace MVCSharp.Core.Configuration.Tasks
{
    #region Documentation
    /// <summary>
    /// Implementation of the <see cref="ITaskInfoProvider"/>
    /// interface. Generates task information from the <see cref="TaskAttribute"/>
    /// attribute applied to a task type.
    /// </summary>
    /// <seealso cref="ITaskInfoProvider"/>
    /// <seealso cref="TaskAttribute"/>
    #endregion
    public class TaskInfoByXmlAttributeProvider : ITaskInfoProvider
    {
        #region Documentation
        /// <summary>
        /// Generates task information from the <see cref="TaskAttribute"/>
        /// attribute applied to a task type.
        /// </summary>
        /// <seealso cref="ITaskInfoProvider"/>
        /// <seealso cref="TaskAttribute"/>
        #endregion
        public TaskInfo GetTaskInfo(Type taskType)
        {
            TaskInfo result = new TaskInfo();

            InteractionPointInfoWrapper[] iPointInfoWrappers = CreateInteractionPointInfos(
                                                        taskType, result.InteractionPoints);

            foreach (InteractionPointInfoWrapper iPointWrapper in iPointInfoWrappers)
                FillNavTargets(iPointWrapper, result.InteractionPoints);   

            return result;
        }

        private InteractionPointInfoWrapper[] CreateInteractionPointInfos(Type taskType,
                                                InteractionPointInfoCollection iPointInfos)
        {
            Assembly controllersDefaultAssembly = taskType.Assembly;
            XmlNodeList iPntNodes = GetTaskRootElement(taskType).SelectNodes(
                        "interactionPoints/*[self::iPoint or self::interactionPoint]");
            InteractionPointInfoWrapper[] iPointWrappers = new InteractionPointInfoWrapper[iPntNodes.Count];
            for (int i = 0; i < iPntNodes.Count; i++)
            {
                InteractionPointInfo ipInfo = CreateInteractionPointInfo(iPntNodes[i] as XmlElement, controllersDefaultAssembly);
                iPointInfos[ipInfo.ViewName] = ipInfo;
                iPointWrappers[i] = new InteractionPointInfoWrapper(ipInfo, iPntNodes[i] as XmlElement);
            }
            return iPointWrappers;
        }

        private XmlElement GetTaskRootElement(Type taskType)
        {
            object[] attributes = taskType.GetCustomAttributes(typeof(TaskAttribute), false);
            if (attributes.Length == 0) return null;

            TaskAttribute taskAttr = attributes[0] as TaskAttribute;
            XmlDocument taskXml = new XmlDocument();
            taskXml.LoadXml("<taskConfiguration></taskConfiguration>");
            taskXml.DocumentElement.InnerXml = taskAttr.Xml;
            return taskXml.DocumentElement;
        }

        private InteractionPointInfo CreateInteractionPointInfo(XmlElement interactionPointElement,
                                                                Assembly controllersDefaultAssembly)
        {
            InteractionPointInfo result = new InteractionPointInfo();
            result.ViewName = interactionPointElement.GetAttribute("view");
            result.IsCommonTarget = interactionPointElement.GetAttribute("isCommonTarget") == "true";
            string controllerTypeName = interactionPointElement.GetAttribute("controllerType");
            if (controllerTypeName.IndexOf(',') < 0)
                controllerTypeName = controllerTypeName + ", " + controllersDefaultAssembly.FullName;
            result.ControllerType = Type.GetType(controllerTypeName);

            return result;
        }

        private void FillNavTargets(InteractionPointInfoWrapper iPointInfWrapper,
                                    InteractionPointInfoCollection iPointInfos)
        {
            foreach (XmlElement navTargetElmt in iPointInfWrapper.iPointElmt.SelectNodes("navTarget"))
            {
                string triggerName = navTargetElmt.GetAttribute("trigger");
                InteractionPointInfo target = iPointInfos[navTargetElmt.GetAttribute("view")];
                if (triggerName == string.Empty)
                    triggerName = target.ViewName;
                iPointInfWrapper.iPointInf.NavTargets[triggerName] = target;
            }
            string quotedViewName = "\"" + iPointInfWrapper.iPointInf.ViewName + "\"";
            XmlNodeList adjacentPointElmts = iPointInfWrapper.iPointElmt.SelectNodes(
                           "/*/adjacentPoints[iPointRef/@view | interactionPointRef/@view = "
                           + quotedViewName + "]/*[self::iPointRef or self::interactionPointRef][@view!="
                           + quotedViewName + "]");
            foreach (XmlElement adjacentPointElmt in adjacentPointElmts)
            {
                string targetViewName = adjacentPointElmt.GetAttribute("view");
                if (   (iPointInfWrapper.iPointInf.NavTargets[targetViewName] != null)
                    && (iPointInfWrapper.iPointInf.NavTargets[targetViewName].ViewName == targetViewName)) continue;
                InteractionPointInfo target = iPointInfos[targetViewName];
                string triggerName = target.ViewName;
                iPointInfWrapper.iPointInf.NavTargets[targetViewName] = target;
            }
        }

        private struct InteractionPointInfoWrapper
        {
            public InteractionPointInfo iPointInf;
            public XmlElement iPointElmt;
            
            public InteractionPointInfoWrapper(InteractionPointInfo iPointInf, XmlElement iPointElmt)
            {
                this.iPointElmt = iPointElmt;
                this.iPointInf = iPointInf;
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Russian Federation Russian Federation
Oleg Zhukov, born and living in Russia is Lead Engineer and Project Manager in a company which provides business software solutions. He has graduated from Moscow Institute of Physics and Technology (MIPT) (department of system programming) and has got a M.S. degree in applied physics and mathematics. His research and development work concerns architectural patterns, domain-driven development and systems analysis. Being the adherent of agile methods he applies them extensively in the projects managed by him.

Comments and Discussions