Skip to content

Instantly share code, notes, and snippets.

@mr5z
Last active August 20, 2020 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/614df0780f0e0d5f85675313d528d2e3 to your computer and use it in GitHub Desktop.
Save mr5z/614df0780f0e0d5f85675313d528d2e3 to your computer and use it in GitHub Desktop.
validation service
public class ValidationService : IValidationService
{
private BindableBase bindableBase;
private object ruleCollection;
private MethodInfo methodInfo;
public event EventHandler<ValidationResultArgs> PropertyInvalid;
public RuleCollection<VM> For<VM>(VM bindableBase) where VM : BindableBase
{
this.bindableBase = bindableBase;
ruleCollection = new RuleCollection<VM>();
bindableBase.PropertyChanged += BindableBase_PropertyChanged;
var type = typeof(RuleCollection<VM>);
methodInfo = type.GetMethod(nameof(RuleCollection<BindableBase>.GetRules));
return (RuleCollection<VM>)ruleCollection;
}
// was ruleCollection.GetRules()
private List<IValidationRule> GetRules()
{
var returnValue = methodInfo.Invoke(ruleCollection, null);
return (List<IValidationRule>)returnValue;
}
...
}
public class RuleCollection<VM> where VM : BindableBase
{
private readonly List<IValidationRule> validationRuleList = new List<IValidationRule>();
public RuleCollection<VM> AddRule<T>(Expression<Func<VM, object>> expression, params ValidationRule<T>[] rules)
{
var propertyName = expression.GetMemberName();
return AddRule(propertyName, rules);
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment