Click here to Skip to main content
15,892,965 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.82/5 (25 votes)
11 Feb 2008CPOL12 min read 90.4K   1.3K   89  
Basing on the concepts introduced in the first part, this article proceeds to implement the core MVP Framework funtionality.
//===========================================
// MVC# Framework | www.MVCSharp.org        |
// ------------------------------------------
// Copyright (C) 2008 www.MVCSharp.org      |
// All rights reserved.                     |
//===========================================

using System;
using System.Text;
using System.Reflection;
using MVCSharp.Core.Configuration.Views;
using MVCSharp.Core.Configuration.Tasks;

namespace MVCSharp.Core.Configuration
{
    #region Documentation
    /// <summary>
    /// Represents configuration data for an MVC# application. It
    /// contains descriptions for all tasks and views (
    /// <see cref="MVCConfiguration.TaskInfos"/> and <see cref="MVCConfiguration.ViewInfosByTask"/>
    /// properties) and other configuration data.
    /// </summary>
    /// <remarks>Every <see cref="MVCSharp.Core.Tasks.TasksManager"/> instance has its
    /// own MVCConfiguration object and starts tasks accordingly
    /// to this configuration.
    /// </remarks>
    #endregion
    public class MVCConfiguration
    {
        private Type viewInfosProviderType;
        private Type taskInfoProviderType;
        private Type viewsManagerType;
        private Assembly viewsAssembly;
        private ViewInfosByTaskCollection viewInfosByTask;
        private ITaskInfoProvider taskInfoProvider;
        private TaskInfoCollection taskInfos;

        #region Documentation
        /// <summary>
        /// Parameterless constructor.
        /// </summary>
        #endregion
        public MVCConfiguration()
        {
            TaskInfos = new TaskInfoCollection();;
            TaskInfos.MVCConfig = this;
        }

        #region Documentation
        /// <summary>
        /// Returns the default MVCConfiguration instance with
        /// <see cref="MVCConfiguration.ViewInfosProviderType"/> set to <see cref="DefaultViewInfosProvider"/>
        /// and <see cref="MVCConfiguration.TaskInfoProviderType"/> set to <see cref="DefaultTaskInfoProvider"/>
        /// and <see cref="MVCConfiguration.ViewsAssembly"/> pointing to the calling assembly.
        /// </summary>
        #endregion
        public static MVCConfiguration GetDefault()
        {
            MVCConfiguration result = new MVCConfiguration();
            result.viewInfosProviderType = typeof(DefaultViewInfosProvider);
            result.taskInfoProviderType = typeof(DefaultTaskInfoProvider);
            result.ViewsAssembly = Assembly.GetCallingAssembly();
            return result;
        }

        #region Documentation
        /// <summary>
        /// Specifies the type of the <see cref="IViewInfosProvider"/> implementation
        /// used to populate view descriptions (
        /// <see cref="MVCConfiguration.ViewInfosByTask"/> property).
        /// </summary>
        #endregion
        public Type ViewInfosProviderType
        {
            get { return viewInfosProviderType; }
            set { viewInfosProviderType = value; }
        }

        #region Documentation
        /// <summary>
        /// Specifies the type of the <see cref="ITaskInfoProvider"/> implementation
        /// used to populate task descriptions (
        /// <see cref="MVCConfiguration.TaskInfos">MVCConfiguration.TaskInfos</see> property).
        /// </summary>
        #endregion
        public Type TaskInfoProviderType
        {
            get { return taskInfoProviderType; }
            set { taskInfoProviderType = value; }
        }

        #region Documentation
        /// <summary>
        /// Specifies a views manager type (<see cref="MVCSharp.Core.Views.IViewsManager"/> implementation)
        /// to be used.
        /// </summary>
        #endregion
        public Type ViewsManagerType
        {
            get { return viewsManagerType; }
            set { viewsManagerType = value; }
        }

        #region Documentation
        /// <summary>
        /// Gets or sets an assembly containing view descriptions. These
        /// descriptions are converted by the <see cref="MVCConfiguration.ViewInfosProviderType"/> class
        /// to <see cref="ViewInfo"/> objects and stored in <see cref="MVCConfiguration.ViewInfosByTask">
        /// MVCConfiguration.ViewInfosByTask</see> collections.</summary>
        #endregion
        public Assembly ViewsAssembly
        {
            get { return viewsAssembly; }
            set { viewsAssembly = value; }
        }

        #region Documentation
        /// <summary>
        /// Gets a set of <see cref="ViewInfoCollection"/> objects for
        /// every task. These objects are produced by an instance of the
        /// <see cref="MVCConfiguration.ViewInfosProviderType">
        /// MVCConfiguration.ViewInfosProviderType</see> type</summary>
        #endregion
        public ViewInfosByTaskCollection ViewInfosByTask
        {
            get
            {
                if (viewInfosByTask == null)
                {
                    IViewInfosProvider viewInfosProvider = CreateHelper.Create(
                                        ViewInfosProviderType) as IViewInfosProvider;
                    viewInfosByTask = viewInfosProvider.GetFromAssembly(ViewsAssembly);
                }
                return viewInfosByTask;
            }
        }

        #region Documentation
        /// <summary>
        /// Gets or sets an associated <see cref="ITaskInfoProvider"/> instance used
        /// to convert task types to <see cref="TaskInfo"/> objects.
        /// </summary>
        #endregion
        public ITaskInfoProvider TaskInfoProvider
        {
            get
            {
                if (taskInfoProvider == null)
                    taskInfoProvider = CreateHelper.Create(TaskInfoProviderType)
                                                            as ITaskInfoProvider;
                return taskInfoProvider;
            }
            set
            {
                taskInfoProvider = value;
            }
        }

        #region Documentation
        /// <summary>
        /// Associates MVCConfiguration to task descriptions.
        /// </summary>
        #endregion
        public TaskInfoCollection TaskInfos
        {
            get { return taskInfos; }
            set { taskInfos = value; }
        }

    }
}

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)


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