Go NHibernate Tour (5): Explore Insert, Update, delete operations

Source: Internet
Author: User
Tags assert

The content of this section

    • Operational Data Overview
    • 1. New Object
    • 2. Deleting objects
    • 3. Updating objects
    • 4. Save the updated object
    • Conclusion
Operational Data Overview

What we often call a unit of work, typically performing 1 or more operations, is either committed or discarded/rolled back for these operations. Thinking about using LINQ to SQL, everything is operating in memory, and only the Datacontext.submitchanges () method is called to commit the changed data to the database, and LINQ to SQL commits or rolls back.

We use NHibernate as well, if we only query the data and not change its value, we do not need to commit (or roll back) to the database.

Note: This section, based on the previous section of the source code, creates a new CRUD.cs class in the data access layer for writing operation methods, and a new CRUDFixture.cs class is used for testing in the test layer of data access.

1. New Object

Simple description: Create a new object; call Isession.save (); Synchronize ISession.

Example: Write the CreateCustomer () method at the data access layer and save the passed-in customer object in the database.

public int CreateCustomer (customer customer) {    int newid = (int) _session. Save (customer);    _session. Flush ();    return newid;}

We test this method, create a new customer object, call the CreateCustomer () method to return the newly inserted CustomerID, and again query the database for the existence of this object based on CustomerID.

[test]public void Createcustomertest () {    var customer = new Customer () {Firstname = "yjing", Lastname = "Lee"};    int newidentity = _crud. CreateCustomer (customer);    var testcustomer = _crud. Getcustomerbyid (newidentity);    Assert.isnotnull (Testcustomer);}
2. Deleting objects

Simple description: Get an object; call Isession.delete (); Synchronize ISession.

Description: Using Isession.delete () removes the state of the object from the database. Of course, your application may still hold a reference to it. Therefore, it is best to understand that the purpose of Delete () is to turn a persisted instance into a temporary instance. You can also delete many objects at once by passing them to the delete () a nhibernate query string. Delete Object order is not required and no FOREIGN KEY constraint violation is raised. Of course, it is possible to raise a NOT NULL constraint violation defined by the Foreign key field.

Example: Write the DeleteCustomer () method at the data access layer to remove the customer object from the database.

public void DeleteCustomer (customer customer) {    _session. Delete (customer);    _session. Flush ();}

We test this method, querying the database for the CustomerID 2 Customer object, calling the DeleteCustomer () method to delete, and again querying the database for the existence of this object based on CustomerID.

[test]public void Deletecustomertest () {    var coutomer = _crud. Getcustomerbyid (2);    _crud. DeleteCustomer (Coutomer);    var testcustomer = _crud. Getcustomerbyid (2);    Assert.isnull (Testcustomer);}
3. Updating objects

Simple description: Get an object, change some of its properties, call Isession.update (), Synchronize ISession.

Example: Writing the UpdateCustomer () method at the data access layer modifies the Customer object.

public void UpdateCustomer (customer customer) {    _session. Update (customer);    _session. Flush ();}

Test this method, query the database for the CustomerID 1 customer object and modify its FirstName property value, call the UpdateCustomer () method Update, The FirstName value of the Customer object that CustomerID to 1 in the database is queried again as the modified value.

[test]public void Updatecustomertest () {    var customer = _crud. Getcustomerbyid (1);    Customer. Firstname = "liyongjing";    _crud. UpdateCustomer (customer);    var testcustomer = _crud. Getcustomerbyid (1);    Assert.AreEqual ("Liyongjing", customer. Firstname);}
4. Save the updated object

Will you come up with this question? What are the objects that you just created, and which are the modified ones? We need to save it to the database for the object we just created, and we need to update it to the database for the modified object.

Fortunately, ISession can identify the different objects and provide us with the Isession.saveorupdate (object) method

The Isession.saveorupdate (object) method completes the following work:

    • Check if the object already exists in the session.
    • If the object is not present, call Save (object).
    • If the object exists, check to see if the object has changed.
    • If the object changes, call Update (object).

Take a look at the following example to illustrate this situation, write the Saveorupdatecustomer () method at the data access layer, save the Update Customer object list, loop through the Customer object in the list, and call Isession.saveorupdate ( Object) method to save the update for each customer object.

public void Saveorupdatecustomer (ilist<customer> customer) {    foreach (var c in Customer)    {        _session. Saveorupdate (c);    }    _session. Flush ();}

To test this method, first query the Customer object FirstName as yjing in the database and modify its LastName property values, which exist in the database, change, and then create a new 2 customer object that does not exist in the database. is newly created. Call the Saveorupdatecustomer () method to save the updated object, which is to update the previously modified object and save the newly created 2 objects later. Re-query the database for the consistency of the customer objects FirstName Yjing,lastname to yongjing.

[test]public void Saveorupdatecustomertest () {    ilist<customer> customers = _crud. Getcustomersbyfirstname ("yjing");    foreach (Var c in customers)    {        c.lastname = "yongjing";    }    var C1 = new Customer () {Firstname = "yjing", Lastname = "yongjing"};    var C2 = new Customer () {Firstname = "yjing", Lastname = "yongjing"};    Customers. ADD (C1);    Customers. ADD (C2);    int initiailistcount = customers. Count;    _crud. Saveorupdatecustomer (customers);    int testlistcount = _crud. Getcustomersbyfirstnameandlastname ("yjing", "yongjing"). Count;    Assert.AreEqual (Initiailistcount, testlistcount);}
Conclusion

Of course, this section manipulates object operations, involving the state of the object in NHibernate, where there are three states for a particular isession: instantaneous (transient) objects, persisted (persistent) objects, free (detached) objects. This section does not mention, later in the discussion session of the time to introduce.

Go NHibernate Tour (5): Explore Insert, Update, delete operations

Related Article

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.