Click here to Skip to main content
11,496,146 members (589 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

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

, 25 Apr 2008 Ms-PL 16.4K 287 32
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.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
mvcsharp-0.7.1.zip
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
Settings.settings
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
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.

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