Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created January 24, 2011 02:50
Show Gist options
  • Save jmarnold/792742 to your computer and use it in GitHub Desktop.
Save jmarnold/792742 to your computer and use it in GitHub Desktop.
Validation Rules in Fubu
public interface IValidationQuery
{
IEnumerable<IValidationRule> RulesFor(object target);
IEnumerable<IValidationRule> RulesFor(Type targetType);
T GetRule<T>(Accessor accessor) where T : class, IValidationRule;
T GetStrategy<T>(Accessor accessor) where T : class, IFieldValidationStrategy;
void ForRule<T>(Accessor accessor, Action<T, Accessor> action) where T : class, IValidationRule;
void ForStrategy<T>(Accessor accessor, Action<T, Accessor> action) where T : class, IFieldValidationStrategy;
bool HasRule<T>(Accessor accessor) where T : class, IValidationRule;
bool HasStrategy<T>(Accessor accessor) where T : class, IFieldValidationStrategy;
}
public interface IValidationRule
{
bool AppliesTo(Accessor accessor);
void Validate(object target, Notification notification);
}
public interface IValidationSource
{
IEnumerable<IValidationRule> RulesFor(Type type);
}
public class MyCustomValidationRegistry : ValidationRegistry
{
public MyCustomValidationRegistry()
{
Rules
.If(accessor => accessor.Name.Contains("Email"))
.ApplyStrategy<MyCustomEmailStrategy>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment