Click here to Skip to main content
15,897,371 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 166.2K   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 dal = LinqToSqlDAL;
//using sch = Schema;
//using ddal = DomainDAL;
//using cdmn = ClientDomain;
//using dmn=Domain;
using u = Util;
using ns = WinUI;
using ui=UIInterface.Transactions;
using uib=UIInterface;
using uitb=UIType;
using uit=UIType.Transactions;

namespace WinUI
{
	public partial class TransactionsForm : Form, ui.IView
	{

		string[] _strarrParticipants;
		ui.IPresenter _Prsntr;
		IList<uit.IDataDisplayObject> _oInitialData;
		uit.Sort _eSortInitial;
		string _strInitialWhereClause;
		bool _bCloakUIHandlers;
		s.Drawing.Color _colorRightClickArea = s.Drawing.Color.PowderBlue;

		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcID;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcPostID;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcParticipant;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcInstant;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcTotalDltAlloc;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcBudgetDelt;
		System.Windows.Forms.DataGridViewTextBoxColumn _dgvtcDescrip;

		System.Windows.Forms.DataGridViewTextBoxColumn[] _arr_dgvtcParicipantAmounts;

		void SetupColumns()
		{
			this._dgv.AutoGenerateColumns = false;
			System.Windows.Forms.DataGridViewCellStyle oNumberColumnStyle = Misc.CreateDataGridViewCellStyleForAmountDisplay();

			this._dgvtcID			= new DataGridViewTextBoxColumn();
			this._dgvtcPostID		= new DataGridViewTextBoxColumn();
			this._dgvtcParticipant	= new DataGridViewTextBoxColumn();
			this._dgvtcInstant		= new DataGridViewTextBoxColumn();
			this._dgvtcTotalDltAlloc= new DataGridViewTextBoxColumn();
			this._dgvtcBudgetDelt	= new DataGridViewTextBoxColumn();
			this._dgvtcDescrip		= new DataGridViewTextBoxColumn();

			this._dgvtcID			.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_ID;
			this._dgvtcPostID		.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_PostID;
			this._dgvtcParticipant	.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_Participant;
			this._dgvtcInstant		.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_Instant;
			this._dgvtcTotalDltAlloc.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_TotalDltAllocAsStr;
			this._dgvtcBudgetDelt	.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_BudgetDeltAsStr;
			this._dgvtcDescrip		.DataPropertyName	= uit.DataDisplayObjectHelper.FLD_Descrip;

			this._dgvtcID			.HeaderText			= uit.DataDisplayObjectHelper.FLD_ID;
			this._dgvtcPostID		.HeaderText			= uit.DataDisplayObjectHelper.FLD_PostID;
			this._dgvtcParticipant	.HeaderText			= uit.DataDisplayObjectHelper.FLD_Participant;
			this._dgvtcInstant		.HeaderText			= uit.DataDisplayObjectHelper.FLD_Instant;
			this._dgvtcTotalDltAlloc.HeaderText			= "Total Dlt Alloc";
			this._dgvtcBudgetDelt	.HeaderText			= "Budget Delt";
			this._dgvtcDescrip		.HeaderText			= uit.DataDisplayObjectHelper.FLD_Descrip;

			oNumberColumnStyle.BackColor = _colorRightClickArea;
			this._dgvtcID			.DefaultCellStyle.BackColor = _colorRightClickArea;
			this._dgvtcPostID		.DefaultCellStyle.BackColor = _colorRightClickArea;
			this._dgvtcParticipant	.DefaultCellStyle.BackColor = _colorRightClickArea;
			this._dgvtcInstant		.DefaultCellStyle.BackColor = _colorRightClickArea;
			this._dgvtcDescrip		.DefaultCellStyle.BackColor = _colorRightClickArea;

			_dgvtcTotalDltAlloc.DefaultCellStyle = oNumberColumnStyle;
			_dgvtcBudgetDelt.DefaultCellStyle = oNumberColumnStyle;

			this._dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
				this._dgvtcID			,
				this._dgvtcPostID		,
				this._dgvtcParticipant	,
				this._dgvtcInstant		,
				this._dgvtcTotalDltAlloc,
				this._dgvtcBudgetDelt	,
				this._dgvtcDescrip		});

			_arr_dgvtcParicipantAmounts = new DataGridViewTextBoxColumn[_strarrParticipants.Length];
			for (int i = 0; i < _strarrParticipants.Length; i++)
			{
				var c = new DataGridViewTextBoxColumn();
				c.DataPropertyName = uit.DataDisplayObjectHelper.GetFLDForParticipantAmount(i);
				c.HeaderText = _strarrParticipants[i];
				c.Width = 60;
				c.DefaultCellStyle = oNumberColumnStyle;
				_arr_dgvtcParicipantAmounts[i] = c;
			}
			this._dgv.Columns.AddRange(_arr_dgvtcParicipantAmounts);
		}
		void UpdateSortIfDifferent()
		{
			if(_rbTotalDltAlloc.Checked)_Prsntr.EnsureSort(uit.Sort.TotalDltAlloc);
			else if(_rbNaturalSort.Checked)_Prsntr.EnsureSort(uit.Sort.Natural);
			else if(_rbTransactionID.Checked)_Prsntr.EnsureSort(uit.Sort.TransactionID);
			else
				throw new Exception();
		}
		void LLResetData(IList<uit.IDataDisplayObject> data, string strWhereClause, uit.Sort eSort)
		{
			this._bCloakUIHandlers = true;
			_dgv.DataSource = data;
			_tbWhere.Text = strWhereClause;
			_rbNaturalSort.Checked = (eSort == uit.Sort.Natural);
			_rbTotalDltAlloc.Checked = (eSort == uit.Sort.TotalDltAlloc);
			_rbTransactionID.Checked = (eSort == uit.Sort.TransactionID);
			this._bCloakUIHandlers = false;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			SetupColumns();
			LLResetData(_oInitialData, _strInitialWhereClause, _eSortInitial);
		}

		void _btnNewTransaction_Click(	object sender, EventArgs e){_Prsntr.ShowNewTransactionDesired();}
		void _btnReload_Click(			object sender, EventArgs e){_Prsntr.RefreshEntitiesWithCurrentPredicate();}
		
		void _dgv_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button != System.Windows.Forms.MouseButtons.Right)
				return;
			DataGridView.HitTestInfo hit = _dgv.HitTest(e.X, e.Y);
			if (hit.RowIndex < 0)
				return;
			_Prsntr.DisplayOfTransactionActionOptionsDesired(hit.RowIndex, new Point(e.X, e.Y));
		}
		void _tbWhere_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button != System.Windows.Forms.MouseButtons.Right)
				return;

			_Prsntr.DisplayOfPredicateOptionsDesired(new Point(e.X, e.Y));
		}
		void _rbNaturalSort_CheckedChanged(object sender, EventArgs e)
		{
			if(_bCloakUIHandlers)
				return;
			UpdateSortIfDifferent();
		}
		void _rbTotalDltAlloc_CheckedChanged(object sender, EventArgs e)
		{
			if(_bCloakUIHandlers)
				return;
			UpdateSortIfDifferent();
		}
		private void _rbTransactionID_CheckedChanged(object sender, EventArgs e)
		{
			if(_bCloakUIHandlers)
				return;
			UpdateSortIfDifferent();
		}

		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 Show(IList<uit.IDataDisplayObject> data, string strInitialWhereClause, string[] strarrParticipants, uit.Sort eSortInitial)
		{
			_oInitialData = data;
			_eSortInitial = eSortInitial;
			_strInitialWhereClause = strInitialWhereClause;
			_strarrParticipants = strarrParticipants;

			this.Show();
		}
		public void ResetData(IList<uit.IDataDisplayObject> data, string strWhereClause, uit.Sort eSort)
		{
			LLResetData(data, strWhereClause, eSort);
		}
		public void DisplayTransactionActionOptions(IList<uitb.TransactionActionOption> lst, int iTransactionIndex, int iTransactionID, string strTransactionDescrip, object view_data)
		{
			Misc.DisplayTransactionActionOptions(_dgv, _Prsntr.ExecuteTransactionActionOption, lst, iTransactionIndex, iTransactionID, strTransactionDescrip, view_data);
		}
		public void DisplayPredicateOptions(List<uitb.PredicateOption> lstPredicateOptions, object view_data)
		{
			Misc.DisplayPredicateOptions(_tbWhere, _Prsntr.ExecutePredicateOption, lstPredicateOptions, view_data);
		}

		public TransactionsForm()
		{
			InitializeComponent();
			this._tbWhere.BackColor = _colorRightClickArea;
		}


	}
}

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