Click here to Skip to main content
15,893,588 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.5K   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.
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)


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