Extract non-Basic authentication to rule file: a2d Rule Engine

Source: Internet
Author: User
Tags foreach static class

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&lt ; 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

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.