It's still time to read the logs. Let's review the business and verification rules in the csla framework today. This function is also an important aspect of the implementation in the framework, the intention is to support and process the validation of data validity rules in a standard way in the business object. (It should be noted in advance that the primary purpose of learning this framework is to improve the Thinking Ability of software development. Therefore, some content may be targeted at this framework and may be mixed with personal ideas, if any error occurs, please advise .)
The preceding section also mentions that the business rule validation in the framework is automatically triggered by the framework when the property set is called. In addition, when you call the factory method to create a new object, you can also call the checkrules () method to traverse all verification rules. During special attribute Association, you can also use the property name to call verification rules for other properties for verification.
There are two methods to define an object rule. One is called a rule of each type, that is, the same object type is instantiated only once. This will reduce the internal expenditure of the system, it is also the default implementation method of the framework. The other is the rule of each instance type, that is, the Business Rule Information of this object will be created for the current object when the new object instance is created, however, the operation permissions of some objects are determined by the current login user. This situation applies.
When implementing the verification rules, the author introduced the delegate, that is, to promote the verification rules to the object type of certain rules, determined by the agreed rules
For each type of verification method, such as required items, number range, complicated operations may also be performed on the database. A typical method signature is as follows:
1:Public Delegate BoolRulehandler (ObjectTarget, ruleargs E );
2:Public Delegate BoolRulehandler <t, r> (T target, r e)WhereR: ruleargs;
We can see that their styles are particularly like events, and 2nd rows are implemented fan types. In this way, developers can define their own business methods according to rules and assign them to the attributes that need to be defined by rules. The ruleargs in method parameters saves the property information and verification result description information.
Another helper class is the tracking of invalid business rules, which will exist in each object instance (and will be initialized only when the data violates the rules ), when a value assignment operation violates business rules, the system will record the current error information and provide error information to the client data binding through the error processing interface (the framework implements the Data Binding interface ), when the user updates the invalid data, the system promptly clears the current failure record. It is worth mentioning that the failure record will be taken and revoked along with the N-layer revocation function. The error information will also be an important basis for the system to determine whether the object can be saved when it is stored.
This feature uses reflection. This should not be a point of discussion. We often know the trade-off between system performance, scalability, and maintainability.
Here, we will give a general description, hoping that we will not be too disappointed. The following is an example of the verification rule (derived from the Framework commonrules class ):
1:/// <Summary>
2:/// Rule ensuring a string value contains one or more
3:/// Characters.
4:/// </Summary>
5:/// <Param name = "target"> Object ining the data to validate </param>
6:/// <Param name = "E"> arguments parameter specifying the name of the string
7:/// Property to validate </param>
8:/// <Returns> <see langword = "false"/> If the rule is broken </returns>
9:/// <Remarks>
10:/// This implementation uses late binding, and will only work
11:/// Against string property values.
12:/// </Remarks>
13:[System. Diagnostics. codeanalysis. suppressmessage ("Microsoft. Design","Ca1062: validateargumentsofpublicmethods")]
14:Public Static BoolStringrequired (ObjectTarget, ruleargs E)
15:{
16:String Value= (String) Utilities. callbyname (
17:Target, E. propertyname, calltype. Get );
18:If(String. Isnullorempty (Value))
19:{
20:E. Description =String. Format (resources. stringrequiredrule, ruleargs. getpropertyname (e ));
21:Return False;
22:}
23:Return True;
24:}
The usage is quite simple, as shown below:
Protected Override VoidAddbusinessrules ()
{
// Todo: Add validation rules
Validationrules. addrule (csla. validation. commonrules. stringrequired, nameproperty );
}