|
Quick Start
Using NValidate involves three simple steps:
- Add a reference to NValidate.dll to your project.
- Reference the NValidate.Framework
in your source code.
- Invoke Demand.That within your propeties or methods.
For example, consider the following simple class:
namespace EmployeeSampleLib {
public class Employee {
public Employee(string firstName, string LastName, DateTime dateOfBirth) {
FirstName = firstName;
LastName = lastName;
DateOfBirth = dateOfBirth;
}
public string FirstName {
get { return _firstName; }
set { _firstName = value ; }
}
public string LastName {
get { return _lastName; }
set { _lastName = value; }
}
public DateTime DateOfBirth {
get { return _dateOfBirth; }
set { _dateOfBirth = value; }
}
private string _firstName ;
private string _lastName;
private DateTime _dateOfBirth;
}
}
This class represents the base information for an employee; however, it allows just
about anything to be stored in its properties which is likely not what
we have in mind. In the real world, there are typically a few rules we want to enforce:
- Neither of the name fields should be null.
- Neither of the name fields should
be empty.
- The employee’s date of birth should not be a value that would permit
him to be under 16 years of age (the legal working age for our particular state).
To do that, we add our parameter validation code as follows:
using NValidate.Framework;
namespace EmployeeSampleLib {
public class Employee {
public Employee(string firstName, string lastName, DateTime dateOfBirth) {
FirstName = firstName;
LastName = lastName;
DateOfBirth = dateOfBirth;
}
public string FirstName {
get { return _first; }
set {
Demand.That(value, "FirstName").IsNotNull().IsNotEmpty();
_first = value;
}
}
public string LastName {
get { return _last; }
set {
Demand.That(value, "LastName").IsNotNull().IsNotEmpty();
_last = value;
}
}
public DateTime DateOfBirth {
get { return _dateOfBirth; }
set {
Demand.That(value, "DateOfBirth").IsLessThan(DateTime.Today.AddYears(-16), AgeErrorMessage);
_dateOfBirth = value;
}
}
private string _first;
private string _last;
private DateTime _dateOfBirth;
private const string AgeErrorMessage = "Must be at least 16.";
}
}
Now, when you pass invalid parameters to this code, the following will happen:
- Passing valid parameters to any of these properties will cause the software to operate
as intended.
- Passing a null reference to either the FirstName or the
LastName properties will
cause NValidate to throw an ArgumentNullException, with the name of the parameter
automatically supplied.
- Passing an empty string to either the FirstName or the
LastName properties will cause NValidate to throw an ArgumentException,
with the name of the parameter automatically
supplied. The error message will specify that the empty strings are not permitted.
- If the caller attempts to pass a date value that would fall within the past 16 years
to the DateOfBirth property, NValidate throws an ArgumentException
with the
message “Must be at least 16.”
|
|