My nhib.pdf line (2): add, delete, modify, and delete

Source: Internet
Author: User

My nhib.pdf line (2): add, delete, modify, and delete
In the previous blog, we introduced the use of nhib.pdf through a small Demo. The most fundamental use of nhibloud is "CRUD" (add, delete, modify, and query ). We often call a work unit to execute one or more operations. You can submit or discard or roll back these operations. If you only query data and do not change its value, you do not need to submit (or roll back) to the database. There are a lot of content for the query operation, which is not involved this time. This article only introduces the addition, deletion, and modification operations.

Note: In this section, we create the SessionCRUD. cs class in the data access layer based on the source code of the previous section to write the operation method, and create a CRUDFixture. cs class in the test layer for testing.



New Object
Call the ISession. Save () method and synchronize the data to the database.
Example: Compile the CreateCustomer () method in the CRUD. cs class to save the passed Customer object to the database.
////// Create an object /////////
  Public int CreateCustomer (Customer customer) {int newid = (int) _ session. Save (Customer); _ session. Flush (); return newid ;}
Test this method. Create a new Customer object, call CreateCustomer () to return the newly inserted mermerid, and then query whether the database has this object based on CustomerId.
////// Create a test ///[Test] public void CreateCustomerTest () {var customer = new Customer () {Firstname = "Lianhai", Lastname = "Zhang"}; int newIdentity = _ crud. createCustomer (customer); var testCustomer = _ crud. getCustomerById (newIdentity); Assert. isNotNull (testCustomer );}
Take a look at the test:

Database results <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqO6PC9wPgo8L2Jsb2NrcXVvdGU + cjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140915/20140915093849119.png" alt = "\">



Delete object
Find an object, call ISession. Delete (), and synchronize it to the database.
However, this method also has a problem. This method first finds this record (SELECT), maps it to an object, and then transfers the object back to perform the DELETE operation ). This may solve a series of problems such as cache and database synchronization.
HQL is a unique operating language of nhibbench. It can understand that, since it can understand it, it can normally process the relationship with the cache (not tested), but it tracks SQL statements, or query first and then delete it. The only difference is that the number of affected rows is returned.
If you do not use the cache function of nhib.pdf, you can use CreateSQLQuery to delete it. This is ignored here and will be introduced in the future.
For example, compile the DeleteCustomer () method in the data access layer to delete the Customer object from the database.
////// Delete an object //////Public void DeleteCustomer (Customer customer) {_ session. Delete (Customer); _ session. Flush ();}
Test this method. Query the Customer object with CustomerId 1 in the database, call DeleteCustomer () to delete the object, and then query whether the object exists in the Database Based on CustomerId.
////// Delete the test ///[Test] public void DeleteCustomerTest () {var coutomer = _ crud. getCustomerById (1); _ crud. deleteCustomer (coutomer); var testCustomer = _ crud. getCustomerById (1); Assert. isNull (testCustomer );}
I don't know why, but the Delete statement of the test result has not come out, but the database has changed. ( The following update and save updates do not have the corresponding statement. The test diagram is not pasted for the time being.) Direct Let's take a look at the results in the database.Right. The record is no longer in use.



Update object
Get an object, change some of its attributes, call ISession. Update (), and synchronize ISession.
For example, compile the UpdateCustomer () method in the data access layer to modify the Customer object.
////// Update object //////Public void UpdateCustomer (Customer customer) {_ session. Update (Customer); _ session. Flush ();}
Test this method.Query the Customer object with 2 merid 2 in the database, modify its Firstname attribute value, and call the UpdateCustomer () method for updates, query again the Firstname value of the Customer object with 1 merid 1 in the database as the modified value.
////// Update the test ///[Test] public void UpdateCustomerTest () {var customer = _ crud. getCustomerById (2); customer. firstname = "lh"; _ crud. updateCustomer (customer); var testCustomer = _ crud. getCustomerById (2); Assert. areEqual ("lh", customer. firstname );}

Let's take a look at the results in the database.Change "Lianhai" to "lh:




Save update object
Will you come up with this question? What are newly created objects and modified objects? We need to save the newly created object to the database, and update the modified object to the database. Nhib.pdf can automatically determine whether to execute INSERT or UPDATE.This is SaveOrUpdate () methodWhen we execute this method, nhib.pdf completes the following work:
Check whether the object already exists in the Session. If the object is not present, call Save (object) to Save it. If the object exists, check whether the object has changed. If the object changes, Update (object) is called to Update the object. Take a look at the following example to illustrate this situation. At the data access layer, write the SaveOrUpdateCustomer () method, save and update the Customer Object List, traverse the Customer object in the list in sequence, and call ISession. the SaveOrUpdate (object) method saves and updates each Customer object.
////// Save the update object //////Public void SaveOrUpdateCustomer (IList
    
     
Customer) {foreach (var c in customer) {_ session. SaveOrUpdate (c);} _ session. Flush ();}
    

Test this method.First, query the Customer object whose Firstname is lh in the database and modify its Lastname attribute value. This object exists in the Database, changes it, and creates two new Customer objects, these two objects do not exist in the database and are newly created. Call the SaveOrUpdateCustomer () method to save the updated object, that is, update the modified object and save the two newly created objects.

////// Save the update test ///[Test] public void SaveOrUpdateCustomerTest () {IList
    
     
MERs = new List
     
      
(); Var oldCustomer = _ crud. getCustomerById (2); oldCustomer. lastname = "Zhang"; MERs. add (oldCustomer); var c1 = new Customer () {Firstname = "Lianhai", Lastname = "Zhang"}; var c2 = new Customer () {Firstname = "Lianhai ", lastname = "Zhang"}; MERs. add (c1); customers. add (c2); _ crud. saveOrUpdateCustomer (MERS mers );}
     
    
Let's take a look at the database. The first piece of data is updated. The following is newly added:



Summary

The above is a simple addition, deletion, and modification operation on the object. Of course, this section manipulating object operations involves the object state in nhibsion. For a specific ISession, the object has three states: instantaneous (transient) objects, persistence objects, and detached objects. This section is not mentioned. We will discuss the Session later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.