Llbl Gen 3.x source code tracing and parsing validation principles and procedures

Source: Internet
Author: User

First look at the applicationProgramHow to Apply verification. Take salesorderheaderentity as an example.
Three Common verifications:
1) Save the previous verification and rewrite validateentitybeforesave.
2) Verify before deletion. Rewrite validateentitybeforedelete.
3) Verify the property after obtaining the value. Rewrite validatefieldvalue.

First, we will explain a basic mode. The virtual method in the base class is designed and override in the derivation. We will call the actual type method at runtime.
Public Class

{

Public Virtual do (){}
}
Public Class B:
{
Public override do (){}
}
When called in this way
A obj = new B ():

OBJ. Do ();
It calls the corresponding method based on the actual type of obj.

 

Verify Code Write

[Serializable]
Public partial class salesorderheadervalidator: validatorbase
{
// _ Llblgenpro_user_code_region_start validationcode
Delete the verification before the purchase order. For example, if the purchase order is still referenced by the Contract contact, it cannot be deleted.
Public override void validateentitybeforedelete (ientitycore involvedentity)
{
Base. validateentitybeforedelete (involvedentity );
Salesorderheaderentity salesorder = (salesorderheaderentity) involvedentity;
If (salesorder. totaldue! = 0)
Throw new exception ("total due zero not allow for this period ");

}
The verification before saving the purchase order is often a field that must be entered for verification

Public override void validateentitybeforesave (ientitycore involvedentity)
{< br> base. validateentitybeforesave (involvedentity);
salesorderheaderentity salesorder = (salesorderheaderentity) involvedentity;
If (salesorder. taxamt> 1000)
throw new exception ("unable to deal with tax more than 1000");
}< br> when the attribute value of the purchase order changes, apply the verification here. For example, after obtaining the deadline, you must verify that it cannot be earlier than the current date

Public override bool validatefieldvalue (ientitycore involvedentity, int fieldindex, object value)
{
Bool result = base. validatefieldvalue (involvedentity, fieldindex, value );
If (! Result)
Return false;

Salesorderheaderentity salesorder = (salesorderheaderentity) involvedentity;

switch (salesorderheaderfieldindex) fieldindex)
{< br> case salesorderheaderfieldindex. duedate:
return validateduedate (salesorder, (datetime) value);
}< br> return true;
}

Private bool validateduedate (salesorderheaderentity salesorder, datetime value)
{
If (value. compareto (datetime. Now) <0)
Throw new exception ("due date cannot be less than today ");
Return true;
}
}

Upload verification code to entity

Override the oninitialized method and specify the validator for the validator attribute.
Protected override void oninitialized ()
{
Base. oninitialized ();
// Assign validator
This. validator = new salesorderheadervalidator ();
}

Trace and delete the stack of a purchase order

First, design a test method for deleting a purchase order.

[Testmethod]
Public void testdeletesalesorderheader ()
{
Dataaccessadapter adapter = new dataaccessadapter (connectionstring );
Salesorderheaderentity salesorder = new salesorderheaderentity (43659 );
Adapter. deleteentity (salesorder );
}

Method for entering dataaccessadapterbase
Public bool deleteentity (ientity2 entitytodelete)
{
Return deleteentity (entitytodelete, null );
}

Call its overload method

Public Virtual bool deleteentity (ientity2 entitytodelete, ipredicateexpression deleterestriction)
{

entitybase2 entitytodeleteasentitybase2 = (entitybase2) entitytodelete;
entitytodeleteasentitybase2.callvalidateentitybeforedelete ();
}

Enter callvalidateentitybeforedelete of entitybase2
Internal void callvalidateentitybeforedelete ()
{
Onvalidateentitybeforedelete ();
}
Continue to the onvalidateentitybeforedelete Method
Protected virtual void onvalidateentitybeforedelete ()
{
If (_ validator! = NULL)
{
_ Validator. validateentitybeforedelete (this );
}
}

So farArticleValidateentitybeforedelete method of salesorderheadervalidator
Let's take a look at the virtual method validateentitybeforedelete in validatorbase.

[Serializable]
Public abstract class validatorbase: ivalidator
{

Public Virtual void validateentitybeforedelete (ientitycore involvedentity)
{
// NOP
}

}

Verify attribute values

Let's look at the validation when setting properties. This verification method can verify the copying behavior of the Program on any attribute of the purchase order. For example, the business rule is that the end date of a purchase order at any time cannot be less than the day when the purchase order is entered. The Code is as follows,
Salesorderheaderentity salesorder = new salesorderheaderentity (2068 );
Salesorder. duedate = datetime. Now. adddays (-1); // set the value of duedate to yesterday, in violation of business rules
To track its stack and enter the Set Method of the duedate attribute.

Public Virtual System. datetime duedate
{
Set {setvalue (INT) salesorderheaderfieldindex. duedate, value );}
}

Enter the setvalue method of entitybase2
Protected bool setvalue (INT fieldindex, object value)
{
Return setvalue (fieldindex, value, true );
}

Overload method with the same name

Protected bool setvalue (INT fieldindex, object value, bool extends mdesyncforfkfields)
{

// Set value is not the same as the value to set, proceed
If (validatevalue (fieldtoset, ref valuetoset, fieldindex ))
Enter the validatevalue Method

Private bool validatevalue (ifieldinfo fieldtovalidate, ref object value, int fieldindex)
{
// Perform custom validation.
Validationresult = (onvalidatefieldvalue (fieldindex, value ));

}
Process to onvalidatefieldvalue Method

Protected virtual bool onvalidatefieldvalue (INT fieldindex, object value)
{
Bool returnvalue = true;
If (_ validator! = NULL)
{
Returnvalue = _ validator. validatefieldvalue (this, fieldindex, value );
}
Return returnvalue;
}

Please note that this returnvalue = _ validator. validatefieldvalue (this, fieldindex, value), it will enter the validatefieldvalue method I wrote at the beginning, and finally enter the custom method validateduedate

This is the complete stack process for verification.

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.