In the field of .Net development, are you and your company troubled by the following problems?
1. DataRow in DataSet can only obtain data in the form of DataTable[0].Rows[0]["Name"]
2. Sql configuration The Sql is not dynamic and clear.
3. Use JAVA's ORM to design .Net and use the object layer. Every modification will affect the reflected object.
Then please continue to read the following article
Object/DataSet Relational Mapping
(Object/Data Set Relational Mapping)
NickLee.ODRM mode can solve the above problems well
1. DataRow in DataSet can only be DataTable[0].Rows[0][" Name"] method to obtain data
solution: DataRow is converted into a serialized object, but the serialized object is only used as a clear object operation entity to process business judgment and data
2. The Sql configuration is not dynamic and unclear Sql patchwork
solution: using IBatisNet data Layer architecture, using IBatisNet's dynamic SQL syntax, but returning DataSet
3. Use JAVA's ORM to design .Net and use the object layer. Every modification will affect the reflected object.
Solution: Clear O (object layer), no reflection mechanism, avoid changes to the object layer with every modification.
Detailed explanation is given below.
The following code demonstration will be included in NickLee.Framework.2.0.1.2 and above. Of course, we are only providing an idea and a flexible solution, which only represents the thinking of TheFallAngel team.
The dll that must be referenced
IBatisNet (Version 1.5 and later) with NickLee.Framework modified version
NickLee.Web.UI (Version 2006.2.1447 and later)
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.Serialization
/// </summary>
[Serializable]
public class cXTM_User
{
private int _dID;
private string _userName;
public cXTM_User()
{
//
// TODO: Add here Constructor logic
//
}
public int DID
{
get
{
return _dID;
}
set
{
_dID = value;
}
}
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
/// <summary>
// / Please define the properties in this class for the array in the Propertylist, and ensure that the name is unique
/// and consistent with the query name in SelectXTM_UserByKey_Test in XTM_User
/// </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(Object/DataSet Relationship 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",UIhashtable);
DataTable table1 = ConvertDataTable(set11, " ");
//Here is the self-defined serialization class
cXTM_User[] objModel = new cXTM_User[table1.Rows.Count];
//DataTable is converted into a serialization class array
for (int y = 0; y < table1.Rows. Count; y++)
{
objModel[y] = new cXTM_User();
DataTableReturnOO(table1.Rows[y], objModel[y]);
}
//BindExDataGrid1.DataSource = table1
in DataSet mode
;//Serialize the object Mode binding
//ExDataGrid1.DataSource = objModel;
ExDataGrid1.DataBind();
}
}
protected void ExDataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
/*
* The application scope of this part
* To query the modification of a piece of data, you can use objModel.UserName
* instead of using DataTable[0].Rows[0]["UserName"] Pattern
* Improve the degree of object-oriented and reduce part of the business process coding
*/
if (e.Item.ItemIndex != -1)
{
cXTM_User objModel = new cXTM_User();
//If the DataGrid is filled for the DataSet
if (e.Item.DataItem.GetType().FullName == "System.Data.DataRowView")
{
DataTableReturnOO((DataRow)((DataRowView)e.Item.DataItem).Row, objModel);
}
//Otherwise it is considered to be a serialized object filled
else
{
objModel = (cXTM_User)e.Item.DataItem;
}
}
}
/// <summary>
/// DataSet is converted into a serialization class function, the class definition refers to 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.Propertylist[j]], null );
}
}
/// <summary>
/// Convert to DataTable
/// </summary>
/// <param name="Source">Data source</param>
/// <param name="DataMember">Data table name </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>
/// Return DataTable as hash table key-value pair
/// </summary>
/// <param name="SourceTable">Data row object</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 is required
//namespace Framework.Web.UIProcess
//{
/// <summary>
/// Summary description of PageBase
/// </summary>
/// <summary>
/// Page layer ( Presentation layer) base class, all pages inherit this page
/// </summary>
public class PageBase : System.Web.UI.Page
{
#region The entire system has some
private string _baseselect;
/// <summary>
/// Query field
/// </summary>
protected string baseselect
{
get
{
// TODO: Add BaseRule.OperationCode getter to implement
return _baseselect;
}
set
{
// TODO: Add BaseRule.OperationCode setter to implement
_baseselect = value;
}
}
/// <summary>
/// Base class hash table, exists in the entire system
/// </summary>
protected Hashtable baseHashtable = new Hashtable( );
/// <summary>
/// Interface hash table, obtains controls and control values obtained from UI factory
/// </summary>
protected Hashtable UIhashtable = new Hashtable();
/// <summary>
/// Error prompt, default value ""
/// </summary>
protected string errMsg = "";
/// <summary>
/// Error status, default value false
/// </summary>
protected bool errState = false;
/ // <summary>
/// Private variable _UISet
/// </summary>
private DataSet _UISet = new DataSet();
/// <summary>
/// Interface layer data set
/// </summary>
protected DataSet UISet
{
get
{
// TODO: Add BaseRule.OperationCode getter to implement
return _UISet;
}
set
{
// TODO: Add BaseRule.OperationCode setter to implement
_UISet = value;
}
}
private DataTable _UITable = new DataTable();
/// <summary >
/// Interface layer data table
/// </summary>
protected DataTable UITable
{
get
{
// TODO: Add BaseRule.OperationCode getter to implement
return _UITable;
}
set
{
// TODO: Add BaseRule.OperationCode setter to implement
_UITable = value ;
}
}
private string _pageTitle = "";
/// <summary>
/// Page title
/// </summary>
protected string pageTitle
{
get
{
// TODO: Add BaseRule.OperationCode getter to implement
return _pageTitle;
}
set
{
// TODO : Add BaseRule.OperationCode setter to implement
_pageTitle = value;
}
}
#endregion
#region Query the existing part of the page
/// <summary>
/// List page base class hash table
/// </summary>
protected Hashtable baseListHashtable = new Hashtable();
/// <summary>
/// Total number of pages. Variables. 1000w, 10000w data set usage
/// </summary>
protected int pageCount;
/// <summary>
/// Total number of records. Variables. Use of 1000w and 10000w data sets
/// </summary>
protected int recordCount;
/// <summary>
/// Total number of records. Properties. 1000w and 10000w data sets Use
/// </summary>
protected int RecordCount
{
get
{
return recordCount;
}
}
#endregion
#region The existing part of the edit page
/// <summary>
/// Edit page base class hash table
/// </summary>
protected Hashtable baseEditHashtable = new Hashtable();
/// <summary>
/// Edit page, Edit data hash table
/// </summary>
protected Hashtable baseEditFillHashtable = new Hashtable();
#endregion
/// <summary>
/// Constructor
/// </summary>
public PageBase()
{
this.Load += new EventHandler(PageBase_Load);
}
private void PageBase_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//The control part required for the entire process
if (Session["baseHashtable"] != null)
{
//Get the hash object list from Session
baseHashtable = (Hashtable)Session["baseHashtable"];
}
//Edit page access rights and access control are only valid when the page is loaded for the first time
if (Session["baseEditHashtable"] != null)
{
//Get the Edit page hash object list
baseEditHashtable = (Hashtable)Session[ "baseEditHashtable"];
//Release the corresponding Session object after acquisition
Session.Remove("baseEditHashtable");
}
else
{
//If it is the initial state, add whether it is safe to edit the state value, the default value is false, it is unsafe
baseEditHashtable.Add ("EditSafeState", false);
}
}
//Query page access control
if (Session["baseListHashtable"] != null)
{
//Get the Edit page hash object list
baseListHashtable = (Hashtable)Session["baseListHashtable"];
//Release the corresponding Session object after acquisition
. .Remove("baseListHashtable");
}
else
{
//If it is the initial state, add whether to refresh the query page, the default value is false,
baseListHashtable will not be refreshed.Add("IsRefresh", false);
}
}
#region UI general function
/// <summary>
/// Throw error message
/// </summary>
/// <param name="page">Page</param>
/// <param name=" errMsg">Error message</param>
protected void throwErrMsg(Page page, string errMsg)
{
page.Response.Write("<script>window.alert("" + errMsg.Replace(""", "'" ) + "");</script>");
}
/// <summary>
/// Refresh the List page that opens the edit form
/// </summary>
/// <param name="page">Page</param>
protected void parentPageRefresh(Page page)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
//Call refresh() in Function.js to refresh the parent form
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>
/// Reset page
/// </summary>
/// <param name="page">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>
/// Hashtable is generated after js interface factory is passed in
/// </summary>
/// <param name="splitStr">js interface factory passes in string</param>
/// < returns></returns>
public Hashtable AjaxUIFactory(string splitStr)
{
string[] fristStr = splitStr.Split(',');
Hashtable table = new Hashtable();
for (int x = 0; x < fristStr.Length; x++ )
{
string[] secondStr = fristStr[x].Split('|');
if (secondStr.Length == 3)
{
//Get the intercepted string and value
table.Add(secondStr[1], secondStr[2 ]);
}
}
return table;
}
#endregion
}
http://www.cnblogs.com/mail-ricklee/archive/2006/11/23/569270.html