Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Practical Use of the MVC Pattern

, 8 Jan 2007 CPOL
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)

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.

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