該演示程序主要代碼為
/// <summary>
/// 連接數 據庫,創建快速ORM框架對象
/// </summary>
/// <returns>ORM 框架對象</returns>
private MyFastORMFramework CreateFramework()
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath , "demomdb.mdb" );
conn.Open();
return new MyFastORMFramework( conn );
}
// 刷新按鈕事件處理
private void cmdRefresh_Click(object sender, System.EventArgs e)
{
using( MyFastORMFramework myWork = this.CreateFramework())
{
RefreshList( myWork );
}
}
// 用戶名列表當前項 目改變事件處理
private void lstName_SelectedIndExchanged(object sender, System.EventArgs e)
{
DB_Employees obj = lstName.SelectedItem as DB_Employees ;
if( obj != null )
{
this.txtID.Text = obj.EmployeeID.ToString () ;
this.txtName.Text = obj.FullName ;
this.txtTitleOfCourtesy.Text = obj.TitleOfCourtesy ;
this.txtAddress.Text = obj.Address ;
this.txtNotes.Text = obj.Notes ;
}
else
{
this.txtID.Text = "";
this.txtName.Text = "";
this.txtTitleOfCourtesy.Text = "";
this.txtNotes.Text = "" ;
this.txtAddress.Text = "";
}
}
// 新增按鈕事件 處理
private void cmdInsert_Click(object sender, System.EventArgs e)
{
try
{
using( dlgRecord dlg = new dlgRecord())
{
dlg.Employe = new DB_Employees();
if( dlg.ShowDialog( this ) == DialogResult.OK )
{
using( MyFastORMFramework myWork = this.CreateFramework())
{
if( myWork.InsertObject( dlg.Employe ) > 0 )
{
RefreshList( myWork );
}
}
}
}
}
catch( Exception ext )
{
MessageBox.Show( ext.ToString());
}
}
// 刪除按鈕事件處理
private void cmdDelete_Click(object sender, System.EventArgs e)
{
DB_Employees obj = this.lstName.SelectedItem as DB_Employees ;
if( obj != null )
{
if( MessageBox.Show(
this ,
"是否刪除 " + obj.FullName + " 的紀錄?",
"系統提示" ,
System.Windows.Forms.MessageBoxButtons.YesNo ) == DialogResult.Yes )
{
using( MyFastORMFramework myWork = this.CreateFramework())
{
myWork.DeleteObject( obj );
RefreshList( myWork );
}
}
}
}
// 刷新員工名稱列表
private void RefreshList( MyFastORMFramework myWork )
{
object[] obJS = myWork.ReadAllObjects(typeof( DB_Employees ));
System.Collections.ArrayList list = new ArrayList();
list.AddRange( obJS );
this.lstName.DataSource = list ;
this.lstName.DisplayMember = "FullName";
}
// 修改按 鈕事件處理
private void cmdEdit_Click(object sender, System.EventArgs e)
{
DB_Employees obj = this.lstName.SelectedItem as DB_Employees ;
if( obj == null )
return ;
using( dlgRecord dlg = new dlgRecord())
{
dlg.txtID.ReadOnly = true ;
dlg.Employe = obj ;
if( dlg.ShowDialog( this ) == DialogResult.OK )
{
using( MyFastORMFramework myWork = this.CreateFramework())
{
if( myWork.UpdateObject( obj ) > 0 )
{
RefreshList( myWork );
}
}
}
}
}
這段代碼是 比較簡單的,而實體類型DB_Employees的代碼可以很容易的使用代碼生成器生成出來。借助 於快速ORM框架,使得基本的數據庫記錄維護操作開發速度快,運行速度也快。
部署 快速ORM框架
這個快速ORM框架是輕量級的,你只需要將MyFastORMFramework.cs以及 BindTableAttribute和BindFIEldAttribute的代碼復制到你的C#工程即可,也可將它們編譯 成一個DLL,供VB.NET等其他非C#的.Net工程使用。
小結
在本課程中,我們使 用反射和動態編譯技術實現了一個快速ORM框架,詳細學習了一個比較簡單的動態編譯技術的 完整實現。動態編譯技術將自由靈活和運行速度結合在一起,是一個比較強大的軟件開發技 術,綜合利用反射和動態編譯技術使得我們有可能打造靈活而高速的程序框架。