I have several applications within my domain that accept similar inputs in text fields. Each application implements its own validation. I want to bring that functionality into a class library so that rather than re-inventing the wheel on each project, our developers can quickly implement the validation library, and move on.
I’m not the best when it comes to OO design. What I need is the ability for a user to enter an arbitrary string, and then for the validation library to check it against the known types to make sure that it matches one of them. Should I build an interface and make each type of string a class that implements that interface? (seems wrong since I won’t know the type when I read in the string). I could use some help identifying a pattern for this.
Thanks.
I’ve always been a fan of Fluent Validation for .Net. If it’s more robust then you need, it’s functionality is easy enough to mimic on your own.
If you’re interested, here’s a link to my very simple validation class. It’s similar in usage to Fluent Validation, but uses lambdas to create the validation assertions. Here’s a quick example of how to use it:
public class Person { public Person(int age){ Age = age; } public int Age{ get; set;} } public class PersonValidator : AbstractValidator { public PersonValidator() { RuleFor(p => p.Age >= 0, () => new ArgumentOutOfRangeException( "Age must be greater than or equal to zero." )); } } public class Example { void exampleUsage() { var john = new Person(28); var jane = new Person(-29); var personValidator = new PersonValidator(); var johnsResult = personValidator.Validate(john); var janesResult = personValidator.Validate(jane); displayResult(johnsResult); displayResult(janesResult); } void displayResult(ValidationResult result) { if(!result.IsValid) Console.WriteLine("Is valid"); else Console.WriteLine(result.Exception.GetType()); } }(see source code for a more thorough example).
Output: