Basic verification and business verification, the basic validation is always maintain the same validation rules, can be achieved through the following hard coding:
public class Order
{
[Required]
[Range (typeof (Decimal), ' 1 ', ' 10000 ')]] public
decimal price {get; set;}
[Required]
[Stringlength ()]
public string Customer {get; set;}
[Required (allowemptystrings=true)]
[Stringlength (m)]
public string StoreID {get; set;}
}
Then validate the error into the list with the following code:
private bool Validatebasicrule (order) {list<keyvaluepair<string, string>> errors = Order.
IsValid (); if (errors. Count > 0) {this.
AddRange (Errors);
return false;
return true; public static class Dataannotationhelper {public static list<keyvaluepair<string, string>> Isvalid<t> (This T o) {list<keyvaluepair<string, string>> errors = new List<keyv
Aluepair<string, string>> ();
var descriptor = GetTypeDescriptor (typeof (T)); foreach (PropertyDescriptor propertydescriptor in descriptor. GetProperties ()) {foreach (var validationattribute in Propertydescriptor.attributes.oftype< ; Validationattribute> ()) {if (!validationattribute.isvalid) (Propertydescriptor.getva
Lue (o))) {errors. ADD (New keyvaluepair<string, string> (Propertydescriptor.name, Validationattribute.formaterrormessage (
Propertydescriptor.name)));
}} return errors; private static ICustomTypeDescriptor GetTypeDescriptor (type type) {return new Associatedmet Adatatypetypedescriptionprovider (Type).
GetTypeDescriptor (type); }
}
And then talk about the variable business rules.
SaaS programs, or when business rules are extremely variable, there are other ways to do it, it is impossible for each company to write separately in design mode (although it is also OK, but inconvenient, the company's business rules are many, the management of these rules code is very high cost, and to developer to be responsible). So use the rules file to separate the rules of the writing, the benefits:
Give the responsibility of the change to others, such as Project manager, project implementation personnel
Code does not require recompilation to implement changes to business rules
Let's solve this variable problem by assuming there are 2 companies: A and B.
A company validation rules:
Basic validation (that is, the hard code of the validation rules for the Order Class)
Custom validation rule: The following URL for the current order must come from these URLs, such as: Www.cnblogs.com, www.cnblogs1.com, www.cnblogs2.com
B Company Verification Rules:
Basic Verification (IBID.)
Custom validation rules: None
What do you need to do if you use the A2D rule engine to solve the problem?
The first step is of course to write the rules file, a company's rules file:
DECLARE
allowstores=["www.cnblogs.com", "www.cnblogs1.com", "www.cnblogs2.com"] end
declare rule
"test "
when
!_.contains (Allowstores, entity.) StoreID)
then
errors. ADD ("StoreID", "StoreID value not right")
Because company B does not have a custom rule, you do not need to write the appropriate rule file