There are lots of Fluent implementations out there now that work with Lambdas to do things that are quite neat. I'd like to wrap my brain around it so I can start creating some of these things, but I have yet to find an explanation that my brain understands.
Consider this simple example of a Person Validator
public class PersonValidator : IValidator<Person>
{
public PersonValidator()
{
AddRule(p => p.FirstName).CannotBeNull().CannotBeBlank();
AddRule(p => p.LastName).CannotBeNull().CannotBeBlank();
}
public List<ValidationResult> Validate(Person p)
{
// pseudo...
apply all rules specified in constructor, return results
}
}
I've managed to get part of all of this working using a method on my Validator like this...
public ValidationResult<T,TProp> AddRule<T,TProp>(Func<T,TProp> property)
{
... not sure what to do here. This method gives me the ability to use the lambda
... for specifying which properties i want to validate
}
I can then create Extension methods that extend IValidator for the purposes of CannotBeNull and CannotBeEmpty.
So it seems I have the first half and the second half of the problem but I'm not sure how to bring them together.
Looking for a meaningful explanation...I'd like to "Get it". :)