我已经定义了诸如 Email、Url、Alphabet、Number 和 ... 之类的类,只是为了集中每个对象所需的验证逻辑,例如:
public class Email
{
private string value;
private bool isValid;
const string RegexPattern = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
public Email(string address, int minLength = 8, int maxLength = 50)
{
value = address;
isValid = Validator.IsValid(address,RegexPattern,minLength,maxLength);
}
public string Address
{
get
{
return value;
}
}
public bool IsValid
{
get
{
return isValid;
}
}
}
这可以称为价值对象吗?如果不是它缺少的东西?