Click here to Skip to main content
12,500,666 members (75,037 online)
Click here to Skip to main content

Stats

136.1K views
2.3K downloads
141 bookmarked
Posted

A Practical Use of the MVC Pattern

, 8 Jan 2007 CPOL
Another approach to the MVC pattern
using System;
using System.Windows.Forms;
using MvcExample.EventHandlers;
using MvcExample.Models;
using MvcExample.Views;

namespace MvcExample.Controllers
{
	public class RptController : BaseController
	{
		#region private members
		private BaseModel model;
		private BaseView view;
		#endregion private members

		#region constructor 
		/// <summary>
		/// Instanciates the view (the representation of the data) and 
		/// the model (the data). 
		/// </summary>
		public RptController(RptControllerType controllerType)
		{
			switch (controllerType)
			{
				case RptControllerType.List:
					view = new View_List_Income();
					model = new Model_Income();
					break;

				case RptControllerType.Graph:
					view = new View_Graph_Income();
					model = new Model_Income();
					break;

				default:
					MessageBox.Show("Type of report unimplemented!", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					return;
			}

			SubscribeControllerToView();
			SubsctribeModelToView();

			view.Show();
			model.GenerateReport();
		}
		#endregion constructor

		#region subscriber methods
		protected override void SubscribeControllerToView()
		{
			if (view == null)
				return;

			view.ViewClosed += new EventHandler(OnView_ViewClosed);
		}

		protected override void SubsctribeModelToView()
		{
			if (model == null)
				return;

			model.ModelChanged += new EventHandler(OnModel_ModelChanged);
			model.ModelProgress += new ProgressEventHandler(OnModel_ModelProgress);
		}
		#endregion subscriber methods

		#region view eventhandlers
		private void OnView_ViewClosed(object sender, EventArgs e)
		{
			if (model != null)
			{
				model.CancelBackgroundWorker();
			}
		}
		#endregion view eventhandlers

		#region model eventhandlers
		private void OnModel_ModelChanged(object sender, EventArgs ea)
		{
			if (view == null)
				return;

			if (view.InvokeRequired)
			{
				view.Invoke(
					new GenericEventHandler(view.UpdateView),
					new object[] {sender, ea});
			}
			else
			{
				view.UpdateView(sender, ea);
			}
		}

		private void OnModel_ModelProgress(object sender, int percent)
		{
			if (view == null)
				return;

			if (view.InvokeRequired)
			{
				view.Invoke(
					new ProgressEventHandler(view.UpdateProgress),
					new object[] {sender, percent});
			}
			else
			{
				view.UpdateProgress(sender, percent);
			}
		}
		#endregion model eventhandlers
	}	// RptController
}

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)

Share

About the Author

Zoltan Balazs
Software Developer
Romania Romania
I've did some programming for Macintosh a few years back and working with Microsoft technologies since then.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160919.1 | Last Updated 8 Jan 2007
Article Copyright 2007 by Zoltan Balazs
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid