ASP.NET can finally let web developers get out of the ASP's noodle code, build a web site in a new way, just like Windows Application, we can also use object-oriented, multi-storey ways to build Web Application. The following is a very simple message this app, which is aimed to disclose ASP.NET powerful and brand new development. If only relative to the message program itself, everyone may doubt that such a simple force is worth it, but I said that the example is just to explain the problem and describe the solution. In fact, I think that from the perspective of maintenance and expansion, even if a simple program is simple, it is also very worthwhile. The message will be built in a multi-layer manner. The following introduction is in order to facilitate everyone understanding, the data entity (CMessageData class) CMessageData is derived from the DataSet, used to maintain message data, in the constructor, Call CREATATATABLES () to add a DataTable used to save message data and add it to its own DataTable collection. Static property TableMapping is used to describe the mapping relationship of the physical field in the DataTable DataColumn and the database, and the data access layer will use this property to populate the data into the CMessageData object.
public class CMessageData: DataSet {public CMessageData () {this.CreateDataTables ();} public static DataTableMapping TableMapping {get {DataTableMapping result = new DataTableMapping ( "t_gbook_postinfo", "MessageTable"); result.ColumnMappings.Add ( "id", "Id"); result.ColumnMappings.Add ( "last_reply_time", "LastReplyTime"); // ... .. return result;}} private void CreateDataTables () {DataTable dt = new DataTable ( "MessageTable"); dt.Columns .Add ("id", typeof (int32)); dt.columns.add ("lastremetime", typeof (datetime)); // ... .. dt.columns ["id"]. Autoincrement = true; dt.columns ["ID"]. AutoincrementSeed = 0; DT.COLUMNS ["ID"]. Autoincrementstep = -1; dt.primarykey = new datacolumn [] {dt.columns ["id"]}; this.tables.add (DT " );} Addednewrow attribute returns a newly added data sheet, Empty DataRow for filled with logical layers. Two overloads of FillDataFormDatabase () call the data access layer to populate a new CMessageData object and return. UpdateToDatabase is used to talk to your own data change update back to the database. public DataRow AddedNewRow () ... public static CMessageData FillDataFromDatabase (Int32 startRecord, Int32 maxRecord) ... public static CMessageData FillDataFromDatabase (Int32 id) ... public void UpdateToDatabase () ... Second, the data access layer (CDataAccess the like) connection to the database, for SIUD (Select , INSERT, UPDATE, DELETE operation. Data connection information is placed in the AppParameters.xml file.
FillMessageData () is overloaded to create two new objects CMessageData, padding data, and then return: public static CMessageData FillMessageData (Int32 startRecord, Int32 maxRecord) public static CMessageData FillMessageData (Int32 id) UpdateMessageData () made by the target parameter CMessageData the changes update back to the database: public static Int32 UpdateMessageData (CMessageData messageData) {OleDbConnection conn = new OleDbConnection (CAppParameters.OleDbConnectionString); OleDbCommand cmdSelect = new OleDbCommand ( "Select username, last_reply_time, guest_name, guest_email, guest_website_name, guest_website_url, guest_oicq, guest_ip, guest_post_time, guest_text, reply_data From t_gbook_postinfo ", conn); OleDbCommand cmdInsert = new OleDbCommand (); cmdInsert.Connection = conn; cmdInsert.CommandText =" Insert Into t_gbook_postinfo (last_reply_time, guest_name, guest_email, guest_website_name, guest_website_url, guest_oicq, guest_ip, guest_post_time , guest_text, reply_data) VALUES (@ last_reply_time, @ guest_name, @ Guest_Email, @ Guest_Website_Name, @ Guest_ website_url, @ guest_oicq, @ guest_ip, @ guest_post_time, @ guest_text, @ reply_data) "; cmdInsert.Parameters.Add (" @ last_reply_time ", OleDbType.DBDate, 0," last_reply_time "); cmdInsert.Parameters.Add (" @ guest_name ", OleDbType.VarWChar, 255," guest_name "); // ... OleDbCommand cmdUpdate = new OleDbCommand (); cmdUpdate.Connection = conn; cmdUpdate.CommandText =" Update t_gbook_postinfo Set last_reply_time = @ last_reply_time, guest_name = @ guest_name, guest_email = @ Guest_Email, Guest_Website_name = @
guest_website_name, guest_website_url = @ guest_website_url, guest_oicq = @ guest_oicq, guest_ip = @ guest_ip, guest_post_time = @ guest_post_time, guest_text = @ guest_text, reply_data = @ reply_data Where (id = @ Original_id) "; cmdUpdate.Parameters.Add (" @ last_reply_time " , OleDbType.DBDate, 0, "last_reply_time"); cmdUpdate.Parameters.Add ( "@ guest_name", OleDbType.VarWChar, 255, "guest_name"); // ... OleDbCommand cmdDelete = new OleDbCommand (); cmdDelete.Connection = conn ; cmdDelete.CommandText = @ "Delete From t_gbook_postinfo where (id = @Original_id)"; cmdDelete.Parameters.Add ( "@ Original_id", OleDbType.Integer, 0, "id") SourceVersion = DataRowVersion.Original;. OleDbDataAdapter ada = New oledbdataadapter (cmdselect); ada.insertcommand = cmdinsert; ada.updateCommand = cmdupdate; ada.dleteCommand = cmddelete; ada.tablemappings.add (cMessageData.tablemapping); return; Return Ada.Update (MessageData, "T_Gbook_Postinfo");} The benefits extracted separately from the data access layer is that the other layers will be dedupted directly with the database. If we change the database from Access to SQL Server, you only need to replace with a new CDataAccess class. Now you can now. In the source code, there is a CODBCDataAccess that uses the ODBC.NET. This replacement CDataAccess does not have any impact on other parts of the program, we can use ODBC.NET access capabilities, change the database to Oracle, Forpro, etc. . Third, the logical layer of the logical layer This message is very simple. It consists of three classes. CMessage provides an overloaded constructor: public cMessage (DataRow Row) We can initialize a CMessage object with a DATAROW data in CMessageData. Public Void FillDataRow (DATAROW ROW) This function fills its own data into the DATAROW object in the parameter.
We use the code like: getMessage (). FillDataRow (MessageData.AddedNewrow ()) can add a new message content to a cMessageData object, where getMessage () is the data that collects users on the page and returns A method for a cMessage. Public CREPLYCOLLECTION Replys This property is used to publicly reply to itself. Fourth, the interface layer - User Control For your convenience, we bind a message on a cMessage object and page, and bind a CREPLY object with a reply displayed on the page, we make two UserControl. The MessageBlock control is used to display a message. It passes the property: Public CMessage Message Objects public CMessage interface, we only need to assign a CMessage object to this property, allowing this control to display the message of the message represented by the CMessage object. The ReplyBlock control is used to display a reply, which is equally attribute: public Creply reply to disclose a CREPLY type interface. In the MessageBlock control, we are dynamically load ReplyBlock controls through the loadControl () method based on the reply included in the replys property of the corresponding CMessage object, and placed in a PlaceHolder type web control. V. The interface layer-page now is very simple, we create a CMessageData object in the main page (Default.aspx.cs), populate data, and use the loadControl () method to load the MessageBlock control to display the message. . CMessageData messageData = CMessageData.FillDataFromDatabase ((iPage - 1) * iPageSize, iPageSize); for (Int32 i = 0; i