在.Net的開發領域,您和您的公司是不是在為以下問題所困擾
1.DataSet中DataRow只能以DataTable[0].Rows[0][“Name”]的方式取得資料
2.Sql配置的不動態,不清晰的Sql拼湊
3.使用JAVA 的ORM設計.Net,使用物件層,每次修改都會影響反射的物件。
那麼請繼續看下面的文章
Object/DataSet Relational Mapping
(物件/資料集關係映射)
NickLee.ODRM模式可以很好的解決以上問題
1.DataSet中DataRow只能以DataTable[0].Rows[0][“ Name”]的方式取得資料
方案:DataRow轉化為序列化對象,但序列化對像只作為清晰的對像操作實體處理業務判斷和數據
2.Sql配置的不動態,不清晰的Sql拼湊
方案:採用IBatisNet數據層構架,採用IBatisNet動態的SQL語法,但返回DataSet
3.使用JAVA 的ORM設計.Net,使用物件層,每次修改都會影響反射的物件。
方案:清晰的O(物件層),無反射機制,避免每一次修改對物件層的改變。
以下做詳細的說明
以下程式碼示範將包含在NickLee.Framework.2.0.1.2以上版本中,當然,我們只是提供一種思想,靈活的方案,僅代表TheFallAngel團隊的思想。
必須引用的dll
IBatisNet(Version 1.5及其以後) with NickLee.Framework修改版本
NickLee.Web.UI(Version 2006.2.1447及其以後版本)
1. cXTM_User.cs
using System;
using System.Data;
using System.Configuration ;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// cXTM_User.序列化
/// </summary>
[Serializable]
public class cXTM_User
{
private int _dID;
private string _userName;
public cXTM_User()
{
//
// TODO: 在此處加入新增:建構子邏輯
//
}
public int DID
{
get
{
return _dID;
}
set
{
_dID = value;
}
}
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
/// <summary>
// / Propertylist中陣列請定義該類別中屬性,並確保名稱唯一性
/// 以及與XTM_User中SelectXTM_UserByKey_Test中查詢名稱統一
/// </summary>
public string[] Propertylist
{
get
{
return new string[] { " UserName", "DID" };
}
}
}
2. XTM_User.xml
<?xml version='1.0' encoding='UTF-8' ?>
<sqlMap namespace='XTM_User' xmlns=" http://ibatis.apache .org/mapping " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
<statements>
<select id='SelectXTM_UserByKey_Test' parameterClass='System.Collections.Hashtable'>
SELECT
Top 5
[DID],
[UserName],
[LoginName],
[PWD],
[LoginFlag],
[StopFlag],
[LoginTime],
[LASTUPDATE]
FROM [XTM_User]
</select>
</statements>
</sqlMap>
3. test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="ODRM_test" %>
<%@ Register Assembly="NickLee.Web.UI" Namespace= "NickLee.Web.UI" TagPrefix="NickLee" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD /xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml " >
<head runat="server">
<title>Object/DataSet Relational Mapping(物件/資料集關係映射)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<NickLee:ExDataGrid ID="ExDataGrid1" runat="server" OnItemDataBound="ExDataGrid1_ItemDataBound">
< /NickLee:ExDataGrid></div>
</form>
</body>
</html>
4. test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web ;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IBatisNet.DataMapper;
using System.Reflection;
public partial class ODRM_test : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet set11 = Mapper.Instance().QueryForDataSet("SelectXTM_UserByKey_Test", Data1Table
, 105 ");
//這裡為自己定義的序列化類別
cXTM_User[] objModel = new cXTM_User[table1.Rows.Count];
//DataTable轉換為序列化類別數組
for (int y = 0; y < table1.Rows. Count; y++)
{
objModel[y] = new cXTM_User();
DataTableReturnOO(table1.Rows[y], objModel[y]);
}
//以DataSet模式綁定
ExDataGrid1.DataSource = table1;
//以序列化物件模式綁定
//ExDataGrid1.DataSource = objModel;
ExDataGrid1.DataBind();
}
}
protected void ExDataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
/*
* 此部分套用範圍
* 查詢一筆資料的修改,可以用objModel.UserName
* 而不必再使用DataTable[0].Rows[0]["UserName"]的模式
* 提高物件導向的程度,並減少業務流程部分編碼
*/
if (e.Item.ItemIndex != -1)
{
cXTM_User objModel = new cXTM_User();
//如果填入DataSet
if (e.Item.DataItem.GetType().FullName == "System.Data.DataRowView")
{
DataTableReturnOO((DataRow)((DataRowView)e.Item.DataItem).Row, objModel);
}
//否則認為為序列化物件填入
else
{
objModel = (cXTM_User)e.Item.DataItem;
}
}
}
/// <summary>
/// DataSet轉換成序列化類別函數,類別定義參考cXTM_User
/// </summary>
private void DataTableReturnOO(DataRow row, cXTM_User objModel)
{
Hashtable hTable = new Hashtable();
hTable = ReturnHashtable (row);
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
for (int j = 0; j < objModel.Propertylist.Length; j++)
{
PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
propertyinfo.SetValue(objModel, hTable[objModel.Property nlist[j]], null );
}
}
/// <summary>
/// 轉換為DataTable
/// </summary>
/// <param name="Source">資料來源</param>
/// <param name="DataMember">資料表名稱</param>
public static DataTable ConvertDataTable(object Source, string DataMember)
{
DataTable baseTable = new DataTable();
if (Source is DataTable)
{
baseTable = (DataTable)Source;
return baseTable;
}
if (Source is DataSet)
{
DataSet set1 = (DataSet)Source;
if ((set1.Tables.Count > 1) && ((DataMember == null) || (DataMember == "")))
{
throw new Exception("If there is more than one table in your dataset, you must define the DataMember property to specify which table to use.");
}
if (set1.Tables.Count < 1)
{
throw new Exception("There are no tables in the datasource.");
}
if ((DataMember != null) && (DataMember != ""))
{
baseTable = set1.Tables[DataMember];
return baseTable;
}
else
{
baseTable = set1.Tables[0];
return baseTable;
}
}
return baseTable;
}
/// <summary>
/// 傳回DataTable為雜湊表鍵值對
/// </summary>
/// <param name="SourceTable">資料行物件</param>
/// <returns>< /returns>
public static Hashtable ReturnHashtable(DataRow SourceRow)
{
Hashtable hTable = new Hashtable();
IList list = SourceRow.ItemArray;
object[] tObj = new object[SourceRow.Table.Columns.Count];
for (int i = 0; i < SourceRow.Table.Columns.Count; i++)
{
tObj[SourceRow.Table.Columns.IndexOf(SourceRow.Table.Columns[i].ColumnName)] = SourceRow.Table.Columns[i ].ColumnName;
}
for (int x = 0; x < list.Count; x++)
{
hTable.Add(tObj[x].ToString(), list[x]);
}
return hTable;
}
}
5. PageBase.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System. Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Text;
using System.Collections;
//namespace可要不要
//namespace Framework.Web.UIProcess
//{
/// <summary>
/// PageBase 的摘要說明
/// </summary>
/// <summary>
/// 頁面層(表示層)基底類別,所有頁面繼承該頁面
/// </summary>
public class PageBase : System.Web.UI.Page
{
#region 整個系統存在部分
private string _baseselect;
/// <summary>
/// 查詢欄位
/// </summary>
protected string baseselect
{
get
{
// TODO: 新增BaseRule.OperationCode getter 實作
return _baseselect;
}
set
{select; } set{
// TODO: 新增BaseRule.OperationCode setter 實作
_baseselect = value;
}
}
/// <summary>
/// 基底類別雜湊表,在整個系統中存在
/// </summary>
protected Hashtable baseHashtable = new Hashtable( );
///
<summary>
/// 介面雜湊表,取得UI工廠取得的控制項與控制項值
/// </summary>
protected Hashtable UIhashtable = new Hashtable();
/// <summary>///出錯提示,預設值""
/// </summary>
protected string errMsg = "";
/// <summary>
/// 出錯狀態,預設值false
/// </summary>
protected bool errState = false;
/ // <summary>
/// 私人變數_UISet
/// </summary>
private DataSet _UISet = new DataSet();
/// <summary>
/// 介面層資料集
/// </summary>
protected DataSet UISet
{
get
{
// TODO: 新增BaseRule.OperationCode getter 實作
return _UISet;
}
set
{
// TODO:新增
BaseRule.OperationCode setter 實作
_UISet = value;
}
}
private DataTable _UITable = new DataTable();
>
/// 介面層資料表
/// </summary>
protected DataTable UITable
{
get
{
// TODO: 新增BaseRule.OperationCode getter 實作
return _UITable;
}
set
{
// TODO: 新增BaseRule.OperationCode setter 實作
_UITable = value ;
}
}
private string _pageTitle = "";
/// <summary>
/// 頁面標題
/// </summary>
protected string pageTitle
{
get
{
// TODO: 新增BaseRule.OperationCode getter 實作
return _pageTitle;
}
set
{
// TODO : 新增BaseRule.OperationCode setter 實作
_pageTitle = value;
}
}
#endregion
#region 查詢頁面存在部分
/// <summary>
/// List頁面基類雜湊表
/// </summary>
protected Hashtable baseListHashtable = new Hashtable();
/// <summary>
/// 頁數頁數.變數.1000w、10000w資料集使用
/// </summary>
protected int pageCount;
/// <summary>
/// 總記錄數.變數.1000w、10000w資料集使用
/// </summary>
protected int recordCount;
/// <summary>
/// 記錄總數.屬性.1000w、10000w資料集使用
/// </summary>
protected int RecordCount
{
get
{
return recordCount;
}
}
#endregion
#region 編輯頁面存在部分
/// <summary>
/// Edit頁面基類雜湊表
/// </summary>
protected Hashtable baseEditHashtable = new Hashtable();
/// <summary>
/// Edit頁面,編輯資料雜湊表
/// </summary>
protected Hashtable baseEditFillHashtable = new Hashtable();
#endregion
/// <summary>
/// 建構子
/// </summary>
public PageBase()
{
this.Load += new EventHandler(PageBase_Load);
}
private void PageBase_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//整個流程所需的控制部分
if (Session["baseHashtable"] != null)
{
//從Session取得雜湊物件清單
baseHashtable = (Hashtable)Session["baseHashtable"];
}
//編輯頁面存取權限和存取控制,只在第一次載入頁面的時候有效
if (Session["baseEditHashtable"] != null)
{
//取得Edit頁面雜湊物件清單
baseEditHashtable = (Hashtable)Session[ "baseEditHashtable"];
//取得完後釋放對應Session物件
Session.Remove("baseEditHashtable");
}
else
{
//如果為初始狀態,新增是否安全編輯狀態值,預設值false,不安全
baseEditHashtable.Add ("EditSafeState", false);
}
}
//查詢頁面存取控制
if (Session["baseListHashtable"] != null)
{
//取得Edit頁面雜湊物件清單
baseListHashtable = (Hashtable)Session["baseListHashtable"];
//取得完後釋放對應Session物件
Session .Remove("baseListHashtable");
}
else
{
//如果為初始狀態,新增是否刷新查詢頁面,預設值false,不刷新
baseListHashtable.Add("IsRefresh", false);
}
}
#region UI通用函數
/// <summary>
/// 拋出出錯訊息提示
/// </summary>
/// <param name="page">頁面</param>
/// <param name=" errMsg">出錯訊息</param>
protected void throwErrMsg(Page page, string errMsg)
{
page.Response.Write("<script>window.alert("" + errMsg.Replace(""", "'" ) + "");</script>");
}
/// <summary>
/// 重新整理開啟編輯窗體的List頁面
/// </summary>
/// <param name="page">頁面</param>
protected void parentPageRefresh(Page page)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
//呼叫Function.js中的refresh()刷新父窗體
scriptString.Append("window.opener.refresh(false," ");");
scriptString.Append(" window.focus();");
scriptString.Append(" window.opener=null;");
scriptString.Append(" window.close(); ");
scriptString.Append("</" + "script>");
page.Response.Write(scriptString.ToString());
}
/// <summary>
/// 重置頁面
/// </summary>
/// <param name="page">頁面</param>
protected void pageReset(Page page)
{
StringBuilder scriptString = new StringBuilder() ;
scriptString.Append("<script language = javascript>");
scriptString.Append(" this.location.reset(); ");
scriptString.Append("</" + "script>");
page.Response.Write(scriptString.ToString());
}
/// <summary>
/// js介面工廠傳入後產生Hashtable
/// </summary>
/// <param name="splitStr">js介面工廠傳入字串</param>
/// < returns></returns>
public Hashtable AjaxUIFactory(string splitStr)
{
string[] fristStr = splitStr.Split(',');
Hashtable table = new Hashtable();
for (int x = 0; x < fristStr.Length; x++Str.Length; x++ )
{
string[] secondStr = fristStr[x].Split('|');
if (secondStr.Length == 3)
{
//取截取後字串和值
table.Add(secondStr[1], secondStr[2 ]);
}
}
return table;
}
#endregion
}
http://www.cnblogs.com/mail-ricklee/archive/2006/11/23/569270.html