Build Data Interface implementation classes
To meet the requirements of the client, the operation of the interface implementation class is divided into two parts: transaction-related operations and data maintenance-related operations.
Class reservationremoteserviceimpl: marshalbyrefobject, ireservationremoteservice {Private Static list <reservation> list; // stores all flight information. Private hashtable transactionbackuphashtable; // The key is the serial number of tansaction, value is list private Boolean m_hascontrolled = false; // No one occupies this data class public Boolean hascontrolled {get {return m_hascontrolled;} set {m_hascontrolled = value ;}}
Data declaration structure of the interface implementation class
In the interface implementation class, the public label must be exposed to the client, and other private labels should be used. This effectively protects the internal structure of the interface implementation class and improves the code security.
Data entities are stored by using a generic list <t>, while hashtable uses the key and value features to back up data sets. In this design, the key is the transaction ID, the list <t> is stored in the value to facilitate storage and search.
# Region transaction related operations // <summary> /// start a transaction // </Summary> /// <Param name = "Xid"> </param> public void begintransaction (string Xid) {If (! Transactionbackuphashtable. containskey (Xid) // if the transaction under this ID is not included, backup (Xid );} /// <summary> /// submit the transaction // </Summary> /// <Param name = "Xid"> </param> Public void committransaction (string Xid) {If (transactionbackuphashtable. containskey (Xid) // if the transaction override (Xid) contains this ID );} /// <summary> /// roll back the transaction /// </Summary> /// <Param name = "Xid"> </param> Public void rollback (string Xid) {If (transactionbackuphashtable. containskey (Xid) // if the transaction recover (Xid) that contains this ID;} // <summary> // back up data, back up each transaction /// </Summary> /// <Param name = "Xid"> transaction id </param> private void backup (string Xid) {transactionbackuphashtable. add (Xid, list );} /// <summary> /// recover data based on the transaction ID /// </Summary> /// <Param name = "Xid"> </param> private void recover (string Xid) {list = (list <reservation>) transactionbackuphashtable [Xid] ;}/// <summary> /// if the execution is successful, overwrite the data by transaction ID /// </Summary> /// <Param name = "Xid"> </param> private void override (string Xid) {transactionbackuphashtable. clear () ;}# endregion
Implementation of related transaction operations
This design only implements simple operations such as adding, deleting, modifying, and querying data sets. The specific methods are as follows:
Public reservation searchreservation (string custname) // search the customer's reservation information {for (INT I = 0; I <list. count; I ++) // traverse all the data in the car if (list [I]. custname = custname) // you can find the required data {return list [I];} return NULL ;}
Implementation of search methods
/// <Summary> /// Insert new data /// </Summary> /// <Param name = "locationstring"> place name </param> /// <param name = "price"> price </param> /// <Param name = "numrooms"> Number of vehicles </param> /// <returns> </returns> Public boolean insertcar (string locationstring, int price, int numcars) {for (INT I = 0; I <listcar. count; I ++) // traverse all the data in the hotel if (listcar [I]. location = locationstring) // you can find the required data {return false; // if there is a duplicate, cancel this operation} Car enetity = new car (); enetity. location = locationstring; enetity. price = price; enetity. numcars = numcars; enetity. numavail = 0; listcar. add (enetity); Return true ;}
Implementation of the insert method
The insertion method first needs to traverse the list <t> to check whether there are repeated data master keywords. If there is already a primary keyword, the insertion failure is returned and data insertion is not allowed. Add data by creating a new data type and inserting it into the list <t>.
/// <Summary> /// Delete Vehicle Information Based on the place name. // </Summary> /// <Param name = "locationstring"> </param> // /<returns> </returns> Public Boolean deletecar (string locationstring) {for (INT I = 0; I <listcar. count; I ++) // traverse all the data in the car if (listcar [I]. location = locationstring) // you can find the required data {listcar. removeat (I); Return true; // deletion successful} return false; // not found, deletion failed}
Data deletion structure
The delete operation cyclically traverses the list <t> to find fields with the same object data fields to complete the deletion.
// Create a unique key value in the following format: 001,002,003 Public String generateid () {string idstring = NULL; If (list. count + 1 <10) idstring = "00" + (list. count + 1 ). tostring (); else if (list. count + 1 <100 & list. count + 1> = 10) idstring = "0" + (list. count + 1 ). tostring (); else idstring = (list. count + 1 ). tostring (); int flag = 1; // flag int increment = List. count + 1; // the original amount created by the key value while (true) {for (INT I = 0; I <list. count; I ++) if (list [I]. resvkey = idstring) {flag = 0; // The data break with the same key value is found;} If (flag = 0) {// If data with the same key value is found, you need to recalculate the key value increment + = 1; if (increment + 1 <10) idstring = "00" + (increment + 1 ). tostring (); else if (list. count + 1 <100 & list. count + 1> = 10) idstring = "0" + (increment + 1 ). tostring (); else idstring = (increment + 1 ). tostring () ;}else // the key value creation does not generate repeated bounce break; flag = 1; // restore} return idstring ;}
Method structure for generating primary data keywords
In some tables, the data needs to be maintained by the primary key word. Adding the value of the primary key word by the customer may result in duplicate keywords and damage data uniqueness. If you create a primary keyword by creating a primary keyword number to generate a function, duplicate data is not generated. The specific implementation process is as follows:
In this design, the primary keyword is in the format of three digits, and less than three digits are filled with zeros. For example, 6 represents, and 17 represents 017. The basic principle of this design is the incremental addition. during initialization, the data master keyword is first added according to the total number of sets in the dataset plus 1. If the data already exists in the database, then, add the value to 1 and change it to the standard format to search in the dataset, so that no repetition will be generated until the loop is repeated, so this is the legal primary keyword.