NewLife. XCode Getting Started Guide (4) cascade operations

Source: Internet
Author: User

1. Introduction

In the previous section, we talked about extended attributes. To put it bluntly, we can click Teacher from the Subject and click the List of Subject from the Teacher. in practice, we usually Delete Teacher, so the course offered by this Teacher is useless, because Subjcet has no teachers. so we hope that the courses we delete when deleting the Teacher will also be deleted. therefore, cascade operations are available.

 

Ii. Logical Relationship

Before demonstrating cascade operations, let's take a look at the relationship. A course corresponds to a teacher, and a teacher can take multiple subjects, in this way, you can delete all the subjects under the instructor, but you cannot delete the teacher when deleting the subject because other subjects are associated with the teacher. after clarifying the logical relationship, we will introduce cascade operations in XCode.

 

Iii. Descriptions of Delete, OnDelete, and others

In XCode, We can reload OnDelete, Delete, OnUpdate, and Update methods.

Let's take a look at the Delete method. The OnDelete method is called after the Delete method is verified. That is to say, the OnDelete method is the true database deletion operation.

At the same time, we can see the description of Delete. If the entity does not have complete information, the affiliated data cannot be deleted through the extended attribute.

What does this mean? To explain this, we may encounter this situation. In a list, click the delete button next to a column, at this time, we use the Post parameter to obtain the primary key ID of the row of data to be deleted. In the program, we may write

String strid = Request. QueryString ["id"]; int id =-1; if (! Int. TryParse (strid, out id) throw new Exception ("invalid parameter! "); // The first method directly deletes the Domain using the object static method. teacher. delete (new string [] {Domain. teacher. _. ID}, new object [] {id}); // method 2 delete an object
Domain. Teacher teacher = new Domain. Teacher () {ID = id };
Teacher. Delete ();
// Method 2: first query and then Delete Domain. Teacher. FindByID (id). Delete ();

 

So what are the differences between the three methods?

First, it is not recommended to write this statement. Writing this statement is equivalent to setting up an SQL statement, Delete From teacher where id = 1; because XCode does not obtain any entity, it will not help you. More importantly, XCode will not help you delete or update the content in the Cache until the next Cache expires, the data you deleted is still in the Cache. If someone else hits the Cache during query, the data is inconsistent;

 

Method 2: This method is only applicable when ID is the auto-incrementing primary key. XCode will automatically help you try cascade deletion, but it cannot ensure that all cascade operations can be deleted, the reason is as follows. here, we will mention that all attributes except ID of an object are null;

 

The third method is the most recommended method. First of all, for a person who once had performance cleanliness, I do not like to delete it after query, but here the big stone tells me, this query will not exceed 20 ms. This is a primary key query! Primary Key query is the fastest. nothing is faster than primary key query, so the loss of this performance can be ignored, but the advantage is the integrity of cascade operations and the integrity of Cache operations.

4. How to Write cascade operations in XCode

First, we know to reload OnDelete and OnUpdate. therefore, the operation is simple. in this section, we do not need to write any code for XCode. The XCode code generator has already helped us. You can open Teacher. biz. line 3 of cs indicates that OnDelete has been reloaded.

        protected override int OnDelete()        {     if (Subjects != null) Subjects.Delete();            return base.OnDelete();        }

The code is explained here, and the second method above is further explained, why cascade operations are incomplete, and further consolidating the extended attributes in the previous section.

If (Subjects! = Null)

Here, the Subjects is the extended attribute we mentioned in the previous section. The program is dead and there is no idea about it. So he will find the Subject attribute and enter the get method, it is determined that _ Subject is indeed null and the id is greater than 0, and no corresponding data exists in the dictionary. Therefore, the FindAllByTeacherID method will be executed to fill in the Subjects. By the end of this Get, except that the id and subjects attributes of the new object of our teacher are not null, other attributes are still null. Do not think that XCode will help you automatically obtain other attributes, this Subjets is exactly the coincidence you wrote in The get method, and is searched by ID.

For example, if the Teacher class has an OfficeID attribute, this OfficeID is associated with another table Office, that is, multiple teachers are in one Office, you can also check the teachers in an office. At this time, because you are a new teacher entity, and all others except id are null.

So even if you write an extended attribute of the Office, run the Get method to execute _ Office = Office. findAllByOfficeID (OfficeID); The OfficeID in this region is null, or an error is reported or cannot be found. in short, your cascade operations are incomplete.

5. Other cascade operations

Although we didn't have to write code on our own, we usually need to improve the Code logic on our own. For example, XCode didn't help us generate code during the Update process. We just need to remember that, reload OnUpdate and call base. onUpdate () method.

For example, in practical applications, cascade operations may not be very relevant, but the overload of OnUpdate is related to OnDelete. Generally, a user has an avatar, the relative address of the Avatar is saved in the database. If the user modifies or deletes the Avatar, will the associated Avatar be useless? If the physical file is not deleted, as the number of junk pictures on the disk increases over the past few days, we have to manually clean up or perform other operations.

At this time, if you reload the method during Update or Delete, and add the disk operation method, you can clear useless junk portraits during editing and deletion.

How to write it? Want me to teach you? Well, let me just write it down. Disk operations are not the focus of this section. You can find many such methods on the Internet.

Here I suddenly thought that the delete operation was easy to handle the problems encountered in the previous project, but during the update operation, you should think about it, Teacher. pic = "xxx"; Teacher. update (); at this time, the Pic has changed !!!! I don't know which file I want to operate on the disk... what to do... what to do... at that time, the landlord's performance cleanup made another mistake, that is, he was unwilling to query the database. I tried every means to seek help from the experts and friends. The final conclusion was that such update operations were not frequent! Primary Key query speed is the fastest! Performance cleanup! The landlord compromised. In fact, it would take less than 20 ms for this primary key to query the database once.

When writing a query, you need to write a method by yourself to forcibly retrieve data from the database, because the pic value in the Cache has been modified at this time.

So here we have a piece of code in the main project.

FindByIDFromDb is a method rewritten by the landlord to avoid searching for data from the Cache. In fact, it is to remove if and else from the following code, leave only FindAll (_. companID, CompanyID ).

 

This section ends now. The next section describes complex queries, which may be divided into two sections. This section describes how to implement complex queries, I will talk about the trillions of pages of data that are proud of the big stones (the landlord is attracted by this performance ~~)

 

Hundreds of billions of data records are displayed on pages. The big stone said that someone else would treat him for dinner ~~~

 

Demo in this section (basically no code is written, so... But we recommend that you use the Demo)

Http://dl.dbank.com/c0672sbtuj

Ps: the code in this section is collapsed ~~~ Search for "fold" on the forum to find it.

 

 

 

XCode Getting Started Guide series:

NewLife. XCode Getting Started Guide NewLife. XCode Getting Started Guide (2) Examples of reverse engineering use NewLife. Xcode Getting Started Guide (3) use of extended attributes

 

NewLife Forum address:

Http://www.53wb.com

 

Big stone blog:

Http://www.cnblogs.com/nnhy/

NewLife. XCode Development Resource Directory

Http://www.cnblogs.com/asxinyu/archive/2012/06/02/2532210.html

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.