The method of parameter verification under Java and C # _java

Source: Internet
Author: User

parameter input and validation problems are frequently encountered during development, and the general authentication method is as follows:

public bool Register (string name, int age)
{
 if (string). IsNullOrEmpty (name)
 {
  throw new ArgumentException ("name should not being empty", "name");
 }
 if (age < A | | age >)
 {
  throw new ArgumentException ("The Age must between", "age");
 }
 //...
}

This is done when the demand changes, the code to change the corresponding is also more, so more trouble, recently contacted Java and C # 2 convenient parameter verification methods, simple introduction.

Java Parameter Validation:

Use a helper class under Google's guava:

Import com.google.common.base.Preconditions;

Sample code:

public static void Checkpersoninfo (int age, String name) {
  preconditions.checknotnull (name, "name is null");
  Preconditions.checkargument (name.length () > 0, "name is longer than 0");
  Preconditions.checkargument (age > 0, "Age must be greater than 0");
  System.out.println ("A person of Age:" + Age + ", Name:" + name);
 }
 
 public static void Getpostcode (String code) {
  preconditions.checkargument (Checkpostcode (code), "postal code does not meet the requirements");
  SYSTEM.OUT.PRINTLN (code);
 }

 public static void Main (string[] args) {
  try {
   checkpersoninfo ("FDSFSD");
   Checkpersoninfo (10,null);
   Checkpersoninfo ( -10, "FDSFSD");
   Getpostcode ("012234");
   
  } catch (Exception e) {
   e.printstacktrace ();
  }
 }

When the parameters do not meet the requirements, throw exception information, the exception is carried in the information of the subsequent custom string, which is more convenient to write.

C # Parameter Validation:

Using the Fluentvalidation class Library, the reference address is below.

How to use:

A simple Person class:

public class person
 {public
  string Name {set;

  public int Age {set;

  Public person (string name, int age)
  {
   name = name;
   Age = Age;
  }
 }

Authentication class for Person:

public class personvalidator:abstractvalidator<person>
 {public
  personvalidator ()
  {
   Rulefor (x => x.name). Notempty (). Withmessage ("Name cannot be empty");
   Rulefor (x => x.name). Length (1,50). Withmessage ("Surname character cannot exceed");   
   Rulefor (x => x.age). GreaterThan (0). Withmessage ("Age must be greater than 0");
  }

  private bool Validname (string name)
  {
   //Custom name validating logic goes here return
   true;
  }
 

Use:

Class program
 {
  static void Main (string[] args)
  {person
   customer = new Person (null,-10);
   Personvalidator validator = new Personvalidator ();
   Validationresult results = validator. Validate (customer);

   BOOL validationsucceeded = results. IsValid;
   ilist<validationfailure> failures = results. Errors;
   foreach (var failure in failures)
   {
    Console.WriteLine (failure. errormessage);
   }

   Console.readkey ();
  }
 

Fluentvalidation's use document: Http://fluentvalidation.codeplex.com/documentation

The above is a small series for everyone to bring the Java and C # under the parameters of the verification method of all the content, hope to help everyone, a lot of support cloud Habitat Community ~

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.