C #3.0 Application of the division method test data entity Field

Source: Internet
Author: User

The previous article introduced the division method (http://www.cnblogs.com/AndersLiu/archive/2007/07/30/836870.html) in C #3.0, and gave several application scenarios. This article continues to introduce a practical scenario-Verifying fields in the object class.

If it is a self-written object class, the code for verifying the value is usually added to the set accessors of the attribute corresponding to the data field, this prevents data that does not meet the database requirements from entering the database (although the database also performs verification, it is a little too much to wait for the database to throw an exception ).

For object classes automatically generated by Linq, these code codes are not generated. If we manually modify the code automatically generated by Linq to SQL, we will find it depressing-when it is in xxx. after the entity class is edited in the dbml designer, all the code will be automatically generated again-our modifications will be overwritten! (This is quite depressing. I tried to add xml document comments to all automatically generated public types and their public members, but because a small change was made in the dbml designer, all the comments were lost ......)

In this case, the division method is used. VS2008 is still derogatory. To facilitate user expansion, all entity classes it generates are partial. Meanwhile, according to "scenario 1" in the previous article ", it also claims a series of partial methods. For example:

[Column (Storage = "_ AccessKey", DbType = "NVarChar (32) not null", CanBeNull = false)]
Public string AccessKey
{
Get
{
Return this. _ AccessKey;
}
Set
{
If (this. _ AccessKey! = Value ))
{
This. OnAccessKeyChanging (value); // note that the partial method is called to implement a "lightweight event"
This. SendPropertyChanging ();
This. _ AccessKey = value;
This. SendPropertyChanged ("AccessKey ");
This. OnAccessKeyChanged (); // note that the partial method is called to implement a "lightweight event"
}
}
}

The corresponding partial method is as follows:

Partial void OnAccessKeyChanging (string value );
Partial void OnAccessKeyChanged ();

Here, I want to be familiar with the. NET control (whether it is the WinForm control or WebForm control). I must be familiar with this Changing and Changed.

Well, I feel more and more nonsense. P is about half a day.

In short, you only need to add a source code file and extend the partial class to verify the field by implementing the partial method. For example:

Public const int AccessKeyMaxLength = 32;

Partial void OnAccessKeyChanging (string value)
{
If (value. Length> = AccessKeyMaxLength)
Throw new Exception ();
}

Note that the OnAccessKeyChanging event is handled here, because verification is required before the attribute value is set.

In fact, this is what Anders Liu wants to say, over.

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.