Click here to Skip to main content
15,893,508 members
Articles / Programming Languages / XML

Building a Middle Tier Component using NHibernate and Spring.NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (21 votes)
10 May 2006CPOL8 min read 159K   2.4K   109  
Building a highly pluggable middle-tier component with NHibernate and Spring.Net.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using RemoteClient.ProductService;

namespace RemoteClient
{
	/// <summary>
	/// Summary description for ProductForm.
	/// </summary>
	public class ProductForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button button5;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ProductForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			try
			{
				rpc = new RemoteClient.ProductService.RpcUProductServiceService();
			}
			catch(Exception Ex )
			{
				Console.WriteLine( Ex.Message );
			}
		}

		private RemoteClient.ProductService.RpcUProductServiceService rpc;
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.label4 = new System.Windows.Forms.Label();
			this.button5 = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.White;
			this.panel1.Controls.Add(this.label2);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(488, 56);
			this.panel1.TabIndex = 0;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(10, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(150, 24);
			this.label2.TabIndex = 0;
			this.label2.Text = "Product";
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(-26, 310);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(654, 2);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(396, 318);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 28);
			this.button1.TabIndex = 2;
			this.button1.Text = "Close";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(56, 84);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Name";
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(98, 80);
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(174, 21);
			this.txtName.TabIndex = 4;
			this.txtName.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(276, 80);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(64, 22);
			this.button2.TabIndex = 5;
			this.button2.Text = "Add";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(342, 80);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(64, 22);
			this.button3.TabIndex = 8;
			this.button3.Text = "Search";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(276, 104);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(64, 22);
			this.button4.TabIndex = 9;
			this.button4.Text = "Update";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(6, 130);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(150, 24);
			this.label3.TabIndex = 10;
			this.label3.Text = "Available";
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader3});
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(4, 152);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(470, 154);
			this.listView1.TabIndex = 11;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "ID";
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Name";
			this.columnHeader3.Width = 399;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(6, 320);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(262, 20);
			this.label4.TabIndex = 12;
			this.label4.Text = "Written by Moim Hossain. 2006";
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(342, 104);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(64, 22);
			this.button5.TabIndex = 13;
			this.button5.Text = "Show All";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// ProductForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(488, 352);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.txtName);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.panel1);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "ProductForm";
			this.Text = "ProductForm";
			this.Load += new System.EventHandler(this.ProductForm_Load);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main(string[] args)
		{
			Application.EnableVisualStyles();

			Application.Run( new ProductForm());
		}


		private void RefreshList()
		{
			listView1.Items.Clear();
			try
			{
				UProductDTO[]products = rpc.FindProducts( "select UProduct from UProduct in class UProductDTO where UProduct.ID != ?" , new object[]{0});

				foreach( UProductDTO product in products )
				{
					listView1.Items.Add( product.id.ToString());
					listView1.Items[ listView1.Items.Count - 1].SubItems.Add( product.name );

					listView1.Items[ listView1.Items.Count - 1].Tag = product;
				}

			}
			catch(Exception Ex )
			{
				System.Diagnostics.Trace.WriteLine( Ex.Message );
			}
		}


		private void button1_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			RefreshList();
		}

		private void ProductForm_Load(object sender, System.EventArgs e)
		{
			RefreshList();
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				listView1.Items.Clear();
				try
				{
					UProductDTO[]products = rpc.FindProducts( "select UProduct from UProduct in class UProductDTO where UProduct.Name like ?" , new object[]{"%"+txtName.Text+"%"});

					foreach( UProductDTO product in products )
					{
						listView1.Items.Add( product.id.ToString());
						listView1.Items[ listView1.Items.Count - 1].SubItems.Add( product.name );

						listView1.Items[ listView1.Items.Count - 1].Tag = product;
					}

				}
				catch(Exception Ex )
				{
					System.Diagnostics.Trace.WriteLine( Ex.Message );
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show( ex.Message );
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			try
			{
				UProductDTO product = new UProductDTO();

				product.name = txtName.Text;

				rpc.SaveProduct( product );

				RefreshList();
			}
			catch(Exception ex)
			{
				MessageBox.Show( ex.Message );
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			if( listView1.SelectedItems.Count == 1 )
			{
				try
				{
					UProductDTO product = listView1.SelectedItems[0].Tag as UProductDTO;

					product.name = txtName.Text;

					rpc.UpdateProduct( product );

					RefreshList();
				}
				catch(Exception ex)
				{
					MessageBox.Show( ex.Message );
				}
			}
		}
	}
}

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
Architect
Netherlands Netherlands
Engineer Powered by the Cloud

Comments and Discussions