Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / Windows Forms

A Practical Use of the MVC Pattern

Rate me:
Please Sign up or sign in to vote.
4.55/5 (41 votes)
8 Jan 2007CPOL5 min read 178.8K   2.7K   145  
Another approach to the MVC pattern
using System;
using System.Data;
using MvcExample.EventHandlers;
using WinFormsEx;

namespace MvcExample.Models
{
	public abstract class BaseModel
	{
		/// <summary>
		/// Event fired when the model is finished calculating/processing the data.
		/// </summary>
		public event EventHandler ModelChanged;

		/// <summary>
		/// Event fired when the model is calculating the data to notify the view
		/// about its progress.
		/// </summary>
		public event ProgressEventHandler ModelProgress;

		#region protected members
		protected BackgroundWorker backgroundWorker;
		protected DataSet ds;
		#endregion protected members

		#region constructor
		protected BaseModel()
		{
			ds = new DataSet();
			backgroundWorker = new BackgroundWorker();

			backgroundWorker.WorkerReportsProgress = false;
			backgroundWorker.WorkerSupportsCancellation = true;
		}
		#endregion constructor

		#region public abstract methods
		public abstract void GenerateReport();
		#endregion public abstract methods

		#region public methods
		/// <summary>
		/// Returns the report data.
		/// </summary>
		public DataSet QueryModel()
		{
			return ds;
		}

		public void CancelBackgroundWorker()
		{
			backgroundWorker.CancelAsync();
		}
		#endregion public methods

		#region event firing methods
		protected void Fire_ModelChanged(object sender, RunWorkerCompletedEventArgs ea)
		{
			BackgroundWorker bw = sender as BackgroundWorker;
			if (bw != null)
				bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(Fire_ModelChanged);

			if (ModelChanged != null)
				ModelChanged(this, EventArgs.Empty);
		}

		protected void Fire_ModelProgress(object sender, int percent)
		{
			if (ModelProgress != null)
				ModelProgress(this, percent);
		}
		#endregion event firing methods
	}
}

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
Software Developer
Romania Romania
I've did some programming for Macintosh a few years back and working with Microsoft technologies since then.

Comments and Discussions