C # How to access data (3)

Source: Internet
Author: User

It's late today-the weather. When can it be warmer? I'm bored. Why is the weather forecast in Shanghai?

It's not as accurate as our hometown yet. It's cloudy as rain, but cloudy as heavy as wind or rain. Yesterday it was said that the sky was fine and the result was actually below.

Er Xue ~~~~! The landlord has come again, and he has to pay a lot of money -------

Speak nonsense! It's important to be serious ....

Next, we talked about how to operate a record yesterday. Now we have a recorded set of gymnastics.

This section is a little difficult. You need to know the. NET generic and generic constraints (I will not talk about them here). There are several original

Interface implementation and inheritance of the start collection class ------

In fact, for record set operations, I prefer to inherit the. NET original collection class, because it is integrated with a lot

Function, enough for us to deal with a lot of problems, but also override a lot of virtual methods, how good !!

OK! Do the following:

[Serializable]
Public abstract class tablecollection <t>: collection <t>
Where T: tableitem
{

}

The actual task of this abstract class is 1. Judge isdirty, isnew, and isdeleted of tableitem;

2. submit data (for the set, it is not in the DB layer) or return data to the set. So there is code:

 

Public void acceptchanges ()
{
// Submit
Collection <t> itemstodelete = new collection <t> ();
Foreach (T item in this. allitems ())
{
If (item. isdeleted)
{
// Wait for deletion
Itemstodelete. Add (item );
}
Else
{
// Restore status
Item. acceptchanges ();
}
}

// Finally remove from collcection
Foreach (T item in itemstodelete)
{
Base. removeitem (indexof (item ));
}
}

Public void rejectchanges ()
{
// Return
Collection <t> itemstodelete = new collection <t> ();
Foreach (T item in this. allitems ())
{
If (item. isnew)
{
// Prepare the record to be deleted
Itemstodelete. Add (item );
}
Else
{
// Returns a single record
Item. rejectchanges ();
}
}

// Delete the newly added record
Foreach (T item in itemstodelete)
{
Base. removeitem (indexof (item ));
}
}

Protected override void removeitem (INT index)
{
// Make a delete mark
This [Index]. Delete ();
}

Why do I only mark a deleted record? It is very simple. If I delete it completely here, what should I do after todb is deleted?

You cannot delete the created conditions. Nothing else can be said. The description is also clear. ^_^

Isn't this. allitems () in the above Code very strange ?? Is to obtain the items of the entire set, the Code is as follows:

Public ienumerable <t> allitems ()
{
Return this;
}

The key is in the following code, we need to implement getenumerator () again, because we don't want all the items, but select what we need

Item:

Public new ienumerator <t> getenumerator ()
{
Foreach (T item in this. allitems ())
{
If (! Item. isdeleted)
{
Yield return item;
}
}
}

Okay, this abstract class is all about it. This guy is always upset when he does more. He always wants to do something for his son and doesn't want him to eat it.

Ready for use !! This is not the case:

Public tablecollection (bool loaddata)
{
If (loaddata)
{
Load ();
}
}
// Implement the actual inheritance class
Public abstract void load ();

So how can my son do his father's explanation? How can I break it down next time? ^ _ ^

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.