Click here to Skip to main content
11,476,994 members (73,783 online)
Click here to Skip to main content
Add your own
alternative version

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

, 11 Feb 2008 CPOL 62.4K 1K 87
Basing on the concepts introduced in the first part, this article proceeds to implement the core MVP Framework funtionality.
mvcsharp.zip
MVCSharp
API Docs
CleanUpAll.proj
Examples
Basics
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
Settings.settings
SimpleFormsViewsManager
Properties
TestSimpleFormsViewsManager
ApplicationLogic
Presentation
Properties
Settings.settings
TasksInteraction
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
Settings.settings
WindowsFormsExample
ApplicationLogic
Presentation
Properties
Settings.settings
MVCSharp
Core
Configuration
Tasks
Views
Tasks
Views
Properties
Webforms
Configuration
Winforms
Configuration
MVCSharp.Tests
Core
Configuration
Tasks
Views
Tasks
MVCSharp.Tests.csproj.user
Properties
Webforms
Configuration
Winforms
Configuration
//===========================================
// 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 Code Project Open License (CPOL)

Share

About the Author

Oleg Zhukov
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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 11 Feb 2008
Article Copyright 2008 by Oleg Zhukov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid