Click here to Skip to main content
12,455,701 members (81,979 online)
Click here to Skip to main content

Tagged as

Stats

17.8K views
304 downloads
32 bookmarked
Posted

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

, 25 Apr 2008 Ms-PL
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.
MVCSharp
API Docs
CleanUpAll.proj
Examples
ASP.NET MVC Store
ApplicationLogic
Model
NorthwindDataSet.xsc
NorthwindDataSet.xss
Presentation
App_Data
Nwind.mdb
Global.asax
Properties
Tests
Properties
TestApplicationLogic
Tests.csproj.user
Basics
Basics (generics used)
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
SimpleFormsViewsManager
Properties
TestSimpleFormsViewsManager
ApplicationLogic
Presentation
Properties
TasksInteraction
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
WindowsFormsExample
ApplicationLogic
Presentation
Properties
MVCSharp
MVCSharp.Tests
Core
Configuration
Tasks
Views
Tasks
MVCSharp.Tests.csproj.user
Properties
Webforms
Configuration
Winforms
Configuration
Core
Configuration
Tasks
Views
Tasks
Views
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 Microsoft Public License (Ms-PL)

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.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Oleg Zhukov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid