Parameter input and verification problems are often encountered during development. The general verification methods are as follows:
public bool Register(string name, int age){ if (string.IsNullOrEmpty(name)) { throw new ArgumentException("name should not be empty", "name"); } if (age < 10 || age > 70) { throw new ArgumentException("the age must between 10 and 70","age"); } //...}When the demand changes, there are many corresponding codes to be changed, which is more troublesome. I have recently come into contact with two convenient parameter verification methods in Java and C#. Let me give you a brief introduction.
Java parameter verification:
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 length must be greater than 0"); Preconditions.checkArgument(age > 0, "age must be greater than 0"); System.out.println("a person age: " + age + ", name: " + name); } public static void getPostCode(String code){ Preconditions.checkArgument(checkPostCode(code),"Zip Code does not meet the requirements"); System.out.println(code); } public static void main(String[] args) { try { checkPersonInfo(10,"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, and the information carried in the exception is the customized string afterwards, which makes writing much more convenient.
C# parameter verification:
Use FluentValidation as a class library, the reference address is below.
How to use:
A simple Person class:
public class Person { public string Name { set; get; } public int Age { set; get; } public Person(string name, int age) { Name = name; Age = age; } }Person's verification class:
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("Name character cannot exceed 50"); 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(); } }Documentation for FluentValidation: http://fluentvalidation.codeplex.com/documentation
The above is all the content of the parameter verification method under Java and C# brought to you by the editor. I hope it will be helpful to everyone and support Wulin.com more~