Click here to Skip to main content
15,892,768 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 164.3K   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.TransactionNWAccntDlts;

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

		#region Columns
		System.Windows.Forms.DataGridViewComboBoxColumn _dgvcbcAccount;
		DGTBColumn _dgvtcAmount;
		#endregion

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

			_dgvcbcAccount	= new DataGridViewComboBoxColumn();
			_dgvtcAmount	= new DGTBColumn();

			_dgvcbcAccount				.HeaderText = "Accnt";
			_dgvtcAmount				.HeaderText = "Amount";
			
			_dgvtcAmount.DefaultCellStyle = oNumberColumnStyle;
			_dgvcbcAccount.DataSource = _collDltAllocAccntNames;
			this._dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
				_dgvcbcAccount				,
				_dgvtcAmount});

		}
		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;
			}
		}
		bool LLBeginEditCell(int iRow, int iCol)
		{
			bool bOK = false;
			if(		iCol == this._dgvcbcAccount	.Index)bOK = _Prsntr.EditAccountDesired(	iRow);
			else if(iCol == this._dgvtcAmount	.Index)bOK = _Prsntr.EditAmountDesired(		iRow);
			else
				throw new s.Exception();
			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[] arrAccountOptions, bool bReadOnly)
		{
			_dgv.ReadOnly = bReadOnly;
			_btnSaveAndClose.Enabled = !bReadOnly;
			_collDltAllocAccntNames = arrAccountOptions;
			SetupColumns();
	
		}
		public void SetRowCount(int iRows)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				_dgv.RowCount = iRows;
			}
		}
		public void SetAccount(string str, int iRow)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[_dgvcbcAccount.Index].Value = str;
			}
		}
		public void SetAmount(string str, int iRow)
		{
			using(Cloak aCloak = new Cloak(CloakUIHandlers))
			{
				this._dgv.Rows[iRow].Cells[this._dgvtcAmount.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 TransactionNWAccntDltsForm()
		{
			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