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

Building an MVP Framework for .NET. Part 4: Strongly Typed Associations

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
25 Apr 2008Ms-PL2 min read 27.4K   339   31  
In this article we continue developing a Model-View-Presenter framework for .NET platform. The new features we are implementing here are strongly typed asscoiations between controllers, views and tasks for higher convenience and type safety.
//===========================================
// MVC# Framework | www.MVCSharp.org        |
// ------------------------------------------
// Copyright (C) 2008 www.MVCSharp.org      |
// All rights reserved.                     |
//===========================================

using System;
using System.Text;
using System.Reflection;
using System.Collections;

namespace MVCSharp.Core.Configuration.Tasks
{
    #region Documentation
    /// <summary>
    /// Implementation of <see cref="ITaskInfoProvider"/> interface.
    /// Extracts task information from <see cref="InteractionPointAttribute"/>,
    /// <see cref="NavTargetAttribute"/> and <see cref="AdjacentPointsAttribute"/>
    /// attributes applied to the task type and its members.
    /// </summary>
    #endregion
    public class TaskInfoByAttributesProvider : ITaskInfoProvider
    {
        #region Documentation
        /// <summary>
        /// Extracts task information from <see cref="InteractionPointAttribute"/>,
        /// <see cref="NavTargetAttribute"/>, <see cref="AdjacentPointsAttribute"/>
        /// attributes applied to the task type and its members.
        /// </summary>
        #endregion
        public TaskInfo GetTaskInfo(Type taskType)
        {
            TaskInfo result = new TaskInfo();

            IList iPointInfWrappers = CreateIPointInfos(taskType, result.InteractionPoints);
            FillNavTargets(taskType, iPointInfWrappers, result.InteractionPoints);

            return result;
        }

        private IList CreateIPointInfos(Type taskType, InteractionPointInfoCollection iPointInfos)
        {
            FieldInfo[] fields = taskType.GetFields(BindingFlags.Public | BindingFlags.Static);
            ArrayList iPointInfWrappers = new ArrayList();
            foreach (FieldInfo field in fields)
                if (field.IsLiteral && field.FieldType == typeof(string))
                {
                    InteractionPointAttribute[] ipAttributes = field.GetCustomAttributes(
                        typeof(InteractionPointAttribute), false) as InteractionPointAttribute[];
                    if (ipAttributes.Length > 0)
                    {
                        string viewName = field.GetValue(null) as string;
                        NavTargetAttribute[] navTargAttributes = field.GetCustomAttributes(
                                typeof(NavTargetAttribute), false) as NavTargetAttribute[];
                        InteractionPointInfo iPointInf = CreateInteractionPointInfo(
                                                            viewName, ipAttributes[0]);
                        iPointInfos[viewName] = iPointInf;
                        iPointInfWrappers.Add(new InteractionPointInfoWrapper(iPointInf,
                                                  ipAttributes[0], navTargAttributes));
                    }
                }
            return iPointInfWrappers;
        }

        private InteractionPointInfo CreateInteractionPointInfo(string viewName,
                                            InteractionPointAttribute ipAttribute)
        {
            InteractionPointInfo result = new InteractionPointInfo();
            result.ViewName = viewName;
            result.ControllerType = ipAttribute.ControllerType;
            result.IsCommonTarget = ipAttribute.IsCommonTarget;
            return result;
        }

        private void FillNavTargets(Type taskType, IList iPointInfWrappers,
                                    InteractionPointInfoCollection ipInfos)
        {
            AdjacentPointsAttribute[] adjPointAttributes = taskType.GetCustomAttributes(
                      typeof(AdjacentPointsAttribute), false) as AdjacentPointsAttribute[];
            foreach (InteractionPointInfoWrapper ipWrapper in iPointInfWrappers)
            {
                InteractionPointInfo iPointInf = ipWrapper.iPointInf;

                foreach (string targetName in ipWrapper.ipInfAttribute.NavTargets)
                    iPointInf.NavTargets[targetName] = ipInfos[targetName];
                foreach (NavTargetAttribute trgAttr in ipWrapper.navTargAttributes)
                    iPointInf.NavTargets[trgAttr.TriggerName] = ipInfos[trgAttr.ViewName];
                foreach (AdjacentPointsAttribute containingAdjPointAttr in
                         GetContainingAdjPointAttrs(iPointInf.ViewName, adjPointAttributes))
                    foreach (string adjPointName in containingAdjPointAttr.AdjacentViewNames)
                        if (adjPointName != iPointInf.ViewName)
                            iPointInf.NavTargets[adjPointName] = ipInfos[adjPointName];
            }
        }

        private IList GetContainingAdjPointAttrs(string iPointName,
                        AdjacentPointsAttribute[] adjPointAttributes)
        {
            ArrayList result = new ArrayList();
            foreach (AdjacentPointsAttribute adjPointAttr in adjPointAttributes)
                if ((adjPointAttr.AdjacentViewNames as IList).Contains(iPointName))
                    result.Add(adjPointAttr);
            return result;
        }

        private class InteractionPointInfoWrapper
        {
            public readonly InteractionPointInfo iPointInf;
            public readonly InteractionPointAttribute ipInfAttribute;
            public readonly NavTargetAttribute[] navTargAttributes;

            public InteractionPointInfoWrapper(InteractionPointInfo iPointInf,
                   InteractionPointAttribute ipInfAttribute, NavTargetAttribute[] navTargAttributes)
            {
                this.iPointInf = iPointInf;
                this.ipInfAttribute = ipInfAttribute;
                this.navTargAttributes = navTargAttributes;
            }
        }
    }
}

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 Microsoft Public License (Ms-PL)


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