I have defined classes like Email,Url,Alphabet,Number and … just to centralize the validation logic needed for each of these objects , like :
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;
}
}
}
can this be called a value object ?if not what it lacks ?
The implementation is one I would expect for a value object. It is immutable (though you may want to add
readonlymodifiers on the fields) and doesn’t seem to have its own identity.You need to consider whether it is indeed a value object depending on the domain – that’s how you decide whether this should be a value object or not.