Click here to Skip to main content
15,881,413 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;

namespace MVCSharp.Core.Configuration.Tasks
{
    #region Documentation
    /// <summary>
    /// Attribute for describing interaction points within a task.
    /// </summary>
    /// <remarks>Should be used with <see cref="TaskInfoByAttributesProvider"/> or
    /// <see cref="DefaultTaskInfoProvider"/> task info providers. They
    /// convert this attribute occurrences into <see cref="TaskInfo"/>
    /// objects.</remarks>
    /// <example>
    /// Here is an example of how this attribute may be used:
    /// <code>
    /// class MyTask
    /// {
    ///     [InteractionPoint(typeof(MyController1))]
    ///     public const string iPoint1 = "View1";
    /// 
    ///     [IPoint(typeof(MyController2), true, iPoint1)]
    ///     public const string iPoint2 = "View2";
    /// 
    ///     [InteractionPoint(typeof(MyController1), iPoint1, iPoint2)]
    ///     public const string iPoint3 = "View3";
    /// 
    ///     [InteractionPoint(typeof(MyController1), true)]
    ///     [NavTarget("Next", iPoint3)]
    ///     [NavTarget("Previous", iPoint2)]
    ///     public const string iPoint4 = "View4";
    /// }
    /// </code>
    /// </example>
    #endregion
    [AttributeUsage(AttributeTargets.Field)]
    public class InteractionPointAttribute : Attribute
    {
        private bool isCommonTarget;
        private Type controllerType;
        private string[] navTargets;

        #region Documentation
        /// <summary>
        /// Constructor taking the controller type and an array of
        /// navigation targets for this interation point.
        /// </summary>
        #endregion
        public InteractionPointAttribute(Type controllerType,
                                         params string[] navTargets)
                : this (controllerType, false, navTargets){ }

        #region Documentation
        /// <summary>
        /// Constructor taking the controller type and a parameter specifying
        /// whether the interaction point is a common target for other points.
        /// </summary>
        #endregion
        public InteractionPointAttribute(Type controllerType,
                                         bool isCommonTarget)
                : this(controllerType, isCommonTarget, new string[0]) { }

        #region Documentation
        /// <summary>
        /// Constructor taking the controller type, a parameter specifying
        /// whether the interaction point is a common target for other points
        /// and an array of navigation targets for this interation point.
        /// </summary>
        #endregion
        public InteractionPointAttribute(Type controllerType, bool isCommonTarget,
                                                        params string[] navTargets)
        {
            this.controllerType = controllerType;
            this.isCommonTarget = isCommonTarget;
            this.navTargets = navTargets;
        }

        #region Documentation
        /// <summary>
        /// Specifies the type of the controller for the interaction point.
        /// </summary>
        #endregion
        public Type ControllerType
        {
            get { return controllerType; }
            set { controllerType = value; }
        }

        #region Documentation
        /// <summary>
        /// Specifies whether the interaction point should be a common target
        /// (i.e. a navigation can be done from any other interaction point to this one).
        /// </summary>
        #endregion
        public bool IsCommonTarget
        {
            get { return isCommonTarget; }
            set { isCommonTarget = value; }
        }

        #region Documentation
        /// <summary>
        /// Gets or sets an array of target navigation points represented by
        /// their view names. Any of these interaction points can be navigated
        /// to with the trigger name equal to the target view name.
        /// </summary>
        #endregion
        public string[] NavTargets
        {
            get { return navTargets; }
            set { navTargets = value; }
        }
    }

    #region Documentation
    /// <summary>
    /// Equivalent to the <see cref="InteractionPointAttribute"/> attribute.
    /// </summary>
    #endregion
    [AttributeUsage(AttributeTargets.Field)]
    public class IPointAttribute : InteractionPointAttribute
    {
        #region Documentation
        /// <summary>
        /// See <see cref="InteractionPointAttribute"/> equivalent constructor.
        /// </summary>
        #endregion
        public IPointAttribute(Type controllerType, params string[] navTargets)
            : base(controllerType, navTargets)
        { }

        #region Documentation
        /// <summary>
        /// See <see cref="InteractionPointAttribute"/> equivalent constructor.
        /// </summary>
        #endregion
        public IPointAttribute(Type controllerType,
                               bool isCommonTarget)
            : base(controllerType, isCommonTarget) { }

        #region Documentation
        /// <summary>
        /// See <see cref="InteractionPointAttribute"/> equivalent constructor.
        /// </summary>
        #endregion
        public IPointAttribute(Type controllerType, bool isCommonTarget,
            params string[] navTargets) : base(controllerType, isCommonTarget, navTargets)
        { }
    }
}

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