Click here to Skip to main content
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
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-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.Web;
using System.Reflection;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI;
using MVCSharp.Core.Views;
using MVCSharp.Webforms.Configuration;
using MVCSharp.Core;
using MVCSharp.Core.Configuration;
using MVCSharp.Core.Tasks;

namespace MVCSharp.Webforms
{
    #region Documentation
    /// <summary>
    /// Works with views represented as web pages.
    /// </summary>
    /// <remarks>
    /// <see cref="MVCSharp.Core.Configuration.Views.ViewInfo"/> objects for this views manager should be of
    /// <see cref="WebformsViewInfo"/> type. That is why the WinformsViewsManager
    /// is often used in combination with <see cref="MVCConfiguration.ViewInfosProviderType"/>
    /// set to <see cref="WebformsViewInfosProvider"/>.
    /// </remarks>
    #endregion
    public class WebformsViewsManager : ViewsManagerBase
    {
        private const string currTaskKey = "currTask";

        static WebformsViewsManager()
        {
            RegisterViewRequestHandler();
        }

        private static void RegisterViewRequestHandler()
        {
            System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration(
                                                                HttpContext.Current.Request.ApplicationPath);
            SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)configuration.GetSectionGroup("system.web");
            if (viewRequestHandlerRegisteredAlready(systemWeb)) return;

            systemWeb.HttpModules.Modules.Add(new HttpModuleAction("WebformsViewRequestHandler",
                                                    typeof(WebformsViewRequestHandler).FullName));
            configuration.Save();
        }

        private static bool viewRequestHandlerRegisteredAlready(SystemWebSectionGroup systemWeb)
        {
            foreach (HttpModuleAction httpMod in systemWeb.HttpModules.Modules)
                if (httpMod.Name == "WebformsViewRequestHandler")
                    return true;
            return false;
        }

        #region Documentation
        /// <summary>
        /// This http module handles page requests and links pages (which implement
        /// IView interface) to controllers. It also fulfils navigation to the requested
        /// view if neccessary.
        /// </summary>
        #endregion
        public class WebformsViewRequestHandler : IHttpModule
        {
            #region Documentation
            /// <summary>
            /// IHttpModule.Init implementation.
            /// </summary>
            #endregion
            public void Init(HttpApplication context)
            {
                context.PostAcquireRequestState += new EventHandler(App_PostAcquireRequestState);
            }

            void App_PostAcquireRequestState(object sender, EventArgs e)
            {
                HttpContext httpContext = HttpContext.Current;
                Page handlerPage = httpContext.Handler as Page;
                if (handlerPage == null) return;

                IView view = handlerPage as IView;
                if (view == null) return;
                handlerPage.Init += new EventHandler(WebformsView_Init);
            }

            void WebformsView_Init(object sender, EventArgs e)
            {
                IView view = sender as IView;
                (view as Page).Init -= new EventHandler(WebformsView_Init);
                HttpContext httpContext = HttpContext.Current;
                ITask currTask = httpContext.Session[currTaskKey] as ITask;
                if (currTask == null) return;
                WebformsViewInfo requestedViewInf = (currTask.Navigator.ViewsManager.ViewInfos
                    as WebformsViewInfoCollection).GetByUrl(httpContext.Request.AppRelativeCurrentExecutionFilePath);
                currTask.Navigator.TryNavigateToView(requestedViewInf.ViewName);
                IController requestedController = currTask.Navigator.GetController(requestedViewInf.ViewName);
                view.ViewName = requestedViewInf.ViewName;
                view.Controller = requestedController;
                if (view.Controller != null)
                    view.Controller.View = view;
            }

            #region Documentation
            /// <summary>
            /// IHttpModule.Dispose implementation.
            /// </summary>
            #endregion
            public void Dispose() { }
        }

        #region Documentation
        /// <summary>
        /// IViewsManager.ActivateView implementation.
        /// </summary>
        #endregion
        public override void ActivateView(string viewName)
        {
            HttpContext httpContext = HttpContext.Current;
            WebformsViewInfo currViewInf = ViewInfos.GetByUrl(httpContext.Request.
                                              AppRelativeCurrentExecutionFilePath);
            if ((currViewInf != null) && (currViewInf.ViewName == viewName)) return;
            httpContext.Session[currTaskKey] = Navigator.Task;
            httpContext.Response.Redirect(ViewInfos[viewName].ViewUrl, true);
        }

        new private WebformsViewInfoCollection ViewInfos
        {
            get { return base.ViewInfos as WebformsViewInfoCollection; }
        }

        #region Documentation
        /// <summary>
        /// IViewsManager.Navigator implementation.
        /// </summary>
        #endregion
        public override Navigator Navigator
        {
            get { return base.Navigator; }
            set
            {
                base.Navigator = value;
                HttpContext.Current.Session[currTaskKey] = Navigator.Task;
            }
        }

        #region Documentation
        /// <summary>
        /// IViewsManager.GetView implementation.
        /// </summary>
        #endregion
        public override IView GetView(string viewName)
        {
            return Navigator.GetController(viewName).View;
        }

        #region Documentation
        /// <summary>
        /// Returns the default MVCConfiguration instance (obtained via
        /// <see cref="MVCConfiguration.GetDefault">MVCConfiguration.GetDefault</see>) with
        /// <see cref="MVCConfiguration.ViewInfosProviderType"/> set to
        /// <see cref="WebformsViewInfosProvider"/> and
        /// <see cref="MVCConfiguration.ViewsManagerType"/> set to <see cref="WebformsViewsManager"/>.
        /// </summary>
        #endregion
        public static MVCConfiguration GetDefaultConfig()
        {
            MVCConfiguration defaultConf = MVCConfiguration.GetDefault();
            defaultConf.ViewsAssembly = Assembly.GetCallingAssembly();
            defaultConf.ViewInfosProviderType = typeof(WebformsViewInfosProvider);
            defaultConf.ViewsManagerType = typeof(WebformsViewsManager);
            return defaultConf;
        }
    }
}

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 | Mobile
Web04 | 2.8.140821.2 | Last Updated 25 Apr 2008
Article Copyright 2008 by Oleg Zhukov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid