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 ~