Click here to Skip to main content
13,146,739 members (81,200 online)
Click here to Skip to main content

Tagged as

Stats

19.8K views
317 downloads
32 bookmarked
Posted 25 Apr 2008

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
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
using System;
using System.Text;
using NUnit.Framework;
using MVCSharp.Core;
using MVCSharp.Core.Tasks;
using MVCSharp.Core.Configuration.Tasks;
using MVCSharp.Core.Configuration.Views;
using MVCSharp.Core.Views;

namespace MVCSharp.Tests.Core
{
    [TestFixture]
    public class TestNavigator
    {
        private Navigator n = new Navigator();
        private StubTask stubTask = new StubTask();
        private TaskInfo ti = new TaskInfo();
        private InteractionPointInfo ipFirstInf = new InteractionPointInfo();
        private InteractionPointInfo ipSecondInf = new InteractionPointInfo();

        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            n.Task = stubTask;
            n.TaskInfo = ti;
            n.ViewsManager = new StubViewsManager();
            ti.InteractionPoints["Some View"] = ipFirstInf;
            ti.InteractionPoints["Some Other View"] = ipSecondInf;
            ipFirstInf.ViewName = "Some View";
            ipSecondInf.ViewName = "Some Other View";
            ipFirstInf.ControllerType = typeof(StubFirstController);
            ipSecondInf.ControllerType = typeof(StubSecondController);
            ipFirstInf.NavTargets["Next"] = ipSecondInf;
            ipFirstInf.NavTargets["Self"] = ipFirstInf;
        }

        [SetUp]
        public void SetUp()
        {
            StubViewsManager.viewActivationCount = 0;
            StubViewsManager.lastViewNameActivated = string.Empty;
            stubTask.viewNameIsFixed = false;
            stubTask.CurrViewName = "Some View";
        }

        [Test]
        public void TestNavigate()
        {
            n.Navigate("Next");

            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);

            stubTask.CurrViewName = "Some View";
            stubTask.viewNameIsFixed = true;
            n.Navigate("Next");

            Assert.AreEqual(2, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some View", StubViewsManager.lastViewNameActivated);
        }

        [Test]
        public void TestNavigateToSameView()
        {
            n.Navigate("Self");

            Assert.AreEqual(0, StubViewsManager.viewActivationCount);
        }

        [Test]
        public void TestTryNavigateToView()
        {
            n.TryNavigateToView("Some Other View");

            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);

            n.TryNavigateToView("Some View");

            Assert.AreEqual(2, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);
        }

        [Test]
        public void TestNavigateDirectly()
        {
            stubTask.CurrViewName = "Some Other View";
            n.NavigateDirectly("Some View");
            
            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
        }

        [Test]
        public void TestGetController()
        {
            IController firstC = n.GetController("Some View");
            IController secondC = n.GetController("Some Other View");
            
            Assert.IsInstanceOfType(typeof(StubFirstController), firstC);
            Assert.IsInstanceOfType(typeof(StubSecondController), secondC);
            Assert.AreSame(stubTask, firstC.Task);
            Assert.AreSame(firstC, n.GetController("Some View"));
        }

        #region Stubs implementations

        class StubTask : ITask
        {
            private Navigator nav;
            private TasksManager tMan;
            private string currViewName;

            public bool viewNameIsFixed;

            public void OnStart(object param)
            {
            }

            public Navigator Navigator
            {
                get { return nav; }
                set { nav = value; }
            }

            public TasksManager TasksManager
            {
                get { return tMan; }
                set { tMan = value; }
            }

            public string CurrViewName
            {
                get { return currViewName; }
                set { if (!viewNameIsFixed) currViewName = value; }
            }
	
        }

        class StubViewsManager : IViewsManager
        {
            private Navigator navigator;
            public static int viewActivationCount = 0;
            public static string lastViewNameActivated;

            public void ActivateView(string viewName)
            {
                viewActivationCount++;
                lastViewNameActivated = viewName;
            }

            public Navigator Navigator
            {
                get { return navigator; }
                set { navigator = value; }
            }

            public ViewInfoCollection ViewInfos
            {
                get { return null; }
                set { }
            }

            public IView GetView(string viewName)
            { return null; }
        }

        class StubFirstController : StubController
        { }

        class StubSecondController : StubController
        { }

        class StubController : IController
        {
            private ITask task;
            private IView view;

            public ITask Task
            {
                get { return task; }
                set { task = value; }
            }

            public IView View
            {
                get { return view; }
                set { view = value; }
            }
	
        }

        #endregion
    }
}

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
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Oleg Zhukov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid