Click here to Skip to main content
15,885,743 members
Articles / Web Development / ASP.NET

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.5K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
using System;
using System.Data;
using Model;
using Model.Data.Interfaces;
using NUnit.Framework;
using Presentation.Interfaces;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace Presentation.Tests
{
	[TestFixture]
	public class EmployeesPresenterFixture
	{
		MockRepository mocks = null;
		IDataReader dataReader = null;
		IEmployeesView view = null;
		IDomainDAO<IEmployee> model = null;
		EmployeesPresenter presenter = null;
		private int employeeID = 1;
		private int eventProductID = 0;

		[SetUp]
		public void SetUp()
		{
			mocks = new MockRepository();
			view = mocks.DynamicMock<IEmployeesView>();
			model = mocks.DynamicMock<IDomainDAO<IEmployee>>();
			dataReader = mocks.DynamicMock<IDataReader>();

			presenter = new EmployeesPresenter(view, model);
		}

		[TearDown]
		public void TestCleanup()
		{
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void NullViewShouldThrowArgumentNullException()
		{
			presenter = new EmployeesPresenter(null, model);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void NullModelShouldThrowArgumentNullException()
		{
			presenter = new EmployeesPresenter(view, null);
		}


		[Test]
		public void InitViewShouldSetEmployeesAndRefreshTimeOnViewWhenIsPostBackIsFalse()
		{
			SetUpInitView();

			presenter.InitView(false);

			mocks.VerifyAll();
		}

		[Test]
		public void InitViewShouldAlwaysSetRefreshTimeOnView()
		{
			SetUpInitView();

			presenter.InitView(false);
			presenter.InitView(true);

			mocks.VerifyAll();
		}

		private void SetUpInitView()
		{
			view.SetEmployees(dataReader);
			LastCall.Constraints(Is.TypeOf(typeof(IDataReader)) && Is.NotNull());
			SetupResult.For(model.FetchAllByName(true)).Return(dataReader);

			view.SetRefreshTime(DateTime.Now);
			LastCall.Constraints(Is.TypeOf(typeof(DateTime)) && Is.NotNull());

			mocks.ReplayAll();
		}

		[Test]
		public void UpdateEmployeeDetailsShouldSetEmployeeDetailsOnView()
		{
			IEmployee employee = mocks.DynamicMock<IEmployee>();
			SetupResult.For(model.FetchByID(employeeID.ToString())).Return(employee);
			view.SetEmployeeDetails(employee);
			LastCall.Constraints(Is.TypeOf(typeof(IEmployee)) && Is.NotNull());

			mocks.ReplayAll();

			presenter.UpdateEmployeeDetails(employeeID.ToString());

			mocks.VerifyAll();
		}

		[Test]
		public void EditEmployeeShouldRaiseRequestEmployeeEditEvent()
		{
			presenter.RequestEmployeeEdit += new EventHandler<SingleValueEventArgs<int>>(RequestEmployeeEditListener);

			Assert.AreEqual(0, eventProductID);
			presenter.EditEmployee(employeeID);
			Assert.AreEqual(1, eventProductID);
		}

		private void RequestEmployeeEditListener(object sender, SingleValueEventArgs<int> e)
		{
			eventProductID = e.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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions