Click here to Skip to main content
15,886,810 members
Articles / Desktop Programming / Windows Forms

Three-tier .NET Application Utilizing Three ORM Technologies

Rate me:
Please Sign up or sign in to vote.
4.95/5 (118 votes)
30 Jan 2010CPOL109 min read 163.9K   4.4K   437  
LINQ to SQL, Entity Framework, and NHibernate used in a parallel fashion in a three-tier WinForms application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using s = System;
using u = Util;
using ns = WinUI;
using ui=UIInterface.TransactionItems;

namespace WinUI
{
	public partial class TransactionItemsForm : Form, ui.IView
	{
		#region Data
		ui.IPresenter _Prsntr;
		string[] _collDltAllocAccntNames;
		bool _bCloakUIHandlers;
		bool _bInEdit;
		bool _bEditValidated;
		#endregion 

		#region Columns
		DGTBColumn _dgvtcDescrip;
		DGTBColumn _dgvtcAmountOrMultiplier;
		System.Windows.Forms.DataGridViewComboBoxColumn _dgvcbcAccount;
		System.Windows.Forms.DataGridViewTextBoxColumn[] _arr_dgvtcShares;
		#endregion

		#region Private Functions
		void CloakUIHandlers(bool b)
		{
			_bCloakUIHandlers = b;
		}
		bool IsUICloaked(){return _bCloakUIHandlers;}
		void SetupColumns(string[] arrParticipants)
		{
			this._dgv.AutoGenerateColumns = false;
			System.Windows.Forms.DataGridViewCellStyle oNumberColumnStyle = Misc.CreateDataGridViewCellStyleForAmountDisplay();

			_dgvtcDescrip	= new DGTBColumn();
			_dgvtcAmountOrMultiplier	= new DGTBColumn();
			_dgvcbcAccount	= new DataGridViewComboBoxColumn();

			_dgvtcDescrip				.HeaderText = "Descrip";
			_dgvtcAmountOrMultiplier	.HeaderText = "Amount";
			_dgvcbcAccount				.HeaderText = "Accnt";
			
			_dgvcbcAccount.DataSource = _collDltAllocAccntNames;

			_dgvtcAmountOrMultiplier.DefaultCellStyle = oNumberColumnStyle;

			this._dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
				_dgvtcDescrip		,
				_dgvtcAmountOrMultiplier		,
				_dgvcbcAccount		});

			_arr_dgvtcShares = new DataGridViewTextBoxColumn[arrParticipants.Count()];
			for (int i = 0; i < arrParticipants.Count(); i++)
			{
				var c = new DataGridViewTextBoxColumn();
				c.HeaderText = arrParticipants[i];
				c.Width = 60;
				_arr_dgvtcShares[i] = c;
			}
			this._dgv.Columns.AddRange(_arr_dgvtcShares);
		}
		void HandleDownArrowPressed(bool bAdjustOnlyIfOnLast)
		{
			bool bOnLast = _dgv.CurrentCell.RowIndex == _dgv.RowCount - 1;
			if (bOnLast)
			{
				if (this._dgv.ReadOnly)
					return;
				_Prsntr.NewRecordDesired();
			}
			if(bOnLast || !bAdjustOnlyIfOnLast)
				_dgv.CurrentCell = _dgv.Rows[_dgv.CurrentCell.RowIndex+1].Cells[_dgv.CurrentCell.ColumnIndex];
		}
		#endregion

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
		}
		#region Event Handlers
		void tb_KeyDown(object sender, KeyEventArgs e)
		{
		//	s.Diagnostics.Trace.WriteLine(string.Format("Caught keydown:{0}-{1}-{2}-{3}-{4}-{5} ",e.KeyCode, e.Shift, e.Alt, e.Control, e.KeyData, e.KeyValue));
			if (e.KeyCode != Keys.Down)
				return;
			HandleDownArrowPressed(/*bAdjustOnlyIfOnLast=*/false);
		}
		void _btnSaveAndClose_Click(object sender, EventArgs e)
		{
			_Prsntr.CloseAndAcceptDesired();
		}
		void _dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if(this.IsUICloaked())
				return;
			if ((e.Control is s.Windows.Forms.TextBox))
			{			
				var tb = e.Control as s.Windows.Forms.TextBox;
				tb.KeyDown -= new KeyEventHandler(tb_KeyDown);
				tb.KeyDown += new KeyEventHandler(tb_KeyDown);
			}
			
			if(_dgv.CurrentCell.ColumnIndex == _dgvcbcAccount.Index)
			{
				var cb = e.Control as s.Windows.Forms.ComboBox;
				AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
				acsc.AddRange(_collDltAllocAccntNames);
				cb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
				cb.AutoCompleteSource = AutoCompleteSource.ListItems;
				cb.AutoCompleteCustomSource = acsc;
			}
		}
		int GetParticipantIndexFromColumnIndex(int i)
		{
			int iParticipantIndex = -1;
			foreach(DataGridViewTextBoxColumn dgvtbc in _arr_dgvtcShares)
			{
				iParticipantIndex++;
				if(i == dgvtbc.Index)
					return iParticipantIndex;
			}
			throw new s.Exception();
		}
		bool LLBeginEditCell(int iRow, int iCol)
		{
			bool bOK = false;
			if(		iCol == this._dgvcbcAccount				.Index)bOK = _Prsntr.EditAccountDesired(	iRow);
			else if(iCol == this._dgvtcAmountOrMultiplier	.Index)bOK = _Prsntr.EditAmountDesired(		iRow);
			else if(iCol == this._dgvtcDescrip				.Index)bOK = _Prsntr.EditDescripDesired(	iRow);
			else
			{
				int iParticipantIndex = GetParticipantIndexFromColumnIndex(iCol);
				bOK = _Prsntr.EditShareDesired(iRow, iParticipantIndex);
			}
			return bOK;
		}
		private void _dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if(this.IsUICloaked())
				return;
			bool bOK = LLBeginEditCell(e.RowIndex, e.ColumnIndex);

			if(!bOK)		e.Cancel = true;
			else	
			{
				_bInEdit = true;
				_bEditValidated = false;
			}
		}
		void _dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if(this.IsUICloaked())
				return;
			if(!_bEditValidated)
				_Prsntr.CancelEdit();
			_bInEdit = false;
			_bEditValidated = false;
			_dgv.Rows[e.RowIndex].ErrorText = String.Empty;
		}
		bool EditCell(int iRow, int iCol, string strVal)
		{
			bool bOK = LLBeginEditCell(iRow, iCol);
			if(!bOK)
				return false;

			string strError = _Prsntr.SetValueAndEndEditOrReturnError(strVal);
			if(strError != null)
				return false;
			return true;
		}
		void _dgv_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Down)
			{
				HandleDownArrowPressed(/*bAdjustOnlyIfOnLast=*/true);
				return;
			}
			if (e.Control && e.KeyCode == Keys.V)
			{
				_Prsntr.BeginBatchUpdates();
				bool bHasUnhandledErrors;
				ns.Misc.PasteClipboardDataToGrid(_dgv, null, EditCell, out bHasUnhandledErrors);
				_Prsntr.EndBatchUpdates();
				if(bHasUnhandledErrors)
					s.Windows.Forms.MessageBox.Show("Not all the values could be updated.");
				return;
			}
		}
		void _cbReverseSign_CheckedChanged(object sender, EventArgs e)
		{
			if(this.IsUICloaked())
				return;
			_Prsntr.DisplayWithReverseSignDesired(_cbReverseSign.Checked);
		}
		void _cbCents_CheckedChanged(object sender, EventArgs e)
		{
			if(this.IsUICloaked())
				return;
			_Prsntr.DisplayAsCentsDesired(_cbCents.Checked);
		}
		void _dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			if(this.IsUICloaked())
				return;
			if(!_bInEdit)
				return;

			string strError = _Prsntr.SetValueAndEndEditOrReturnError(e.FormattedValue.ToString());
			if(strError !=null)
			{
				_dgv.Rows[e.RowIndex].ErrorText = strError;
				e.Cancel = true;
			}
			else
				_bEditValidated = true;
		}
		#endregion

		#region IView functions
		public void SetPresenter(		ui.IPresenter aPresenter){_Prsntr = aPresenter					;}
		public void ShowBusyIndicator(							){Misc.ShowBusyIndicator()				;}
		public void ClearBusyIndicator(							){Misc.ClearBusyIndicator()				;}
		public void ShowSimpleMessage(	string str				){Misc.ShowSimpleMessage(str)			;}
		public void ShowException(		System.Exception ex		){Misc.ShowException(ex)				;}

		public void Init(string[] arrParticipants, string[] arrAccountOptions, bool bReadOnly)
		{
			_dgv.ReadOnly = bReadOnly;
			_btnSaveAndClose.Enabled = !bReadOnly;
			_collDltAllocAccntNames = arrAccountOptions;
			SetupColumns(arrParticipants);
	
		}
		public void SetRowCount(int iRows)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				_dgv.RowCount = iRows;
			}
		}
		public void SetDescrip(string str, int iRow)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[this._dgvtcDescrip.Index].Value = str;
			}
		}
		public void SetAmount(string str, int iRow)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[this._dgvtcAmountOrMultiplier.Index].Value = str;
			}
		}
		public void SetAccount(string str, int iRow)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[_dgvcbcAccount.Index].Value = str;
			}
		}
		public void SetShare(string str, int iRow, int iParticipantIndex)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[_arr_dgvtcShares[iParticipantIndex].Index].Value = str;
			}
		}
		public void IndicateCentsBeingDisplayed(bool b)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._cbCents.Checked = b;
			}
		}
		public void IndicateSignReversed(bool b)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._cbReverseSign.Checked = b;
			}
		}
		public void SetBalance(string str, bool bInBalance)
		{
			if(bInBalance)
			{
				this._tbBalance.BackColor = s.Drawing.SystemColors.Window;
				this._tbBalance.ForeColor = s.Drawing.SystemColors.WindowText;
			}
			else
			{
				this._tbBalance.BackColor = s.Drawing.Color.White;
				this._tbBalance.ForeColor = s.Drawing.Color.Red;
			}
			this._tbBalance.Text = str;
		}
		public void FreezeDisplay()
		{
		}
		public void UpdateDisplay()
		{
		}
		public void ShowUntilUserDoneAndThenDestroy()
		{
			this.ShowDialog();
			this.Dispose();
		}
		#endregion

		#region Other Public Functions
		public TransactionItemsForm()
		{
			InitializeComponent();
		}

		#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 Code Project Open License (CPOL)


Written By
Software Developer (Senior) Austin Regional Clinic
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