I wrote a method for validating properties based on passed conditions. It works well, but declaration takes more space than the actual condition. Is there any way to make it shorter?
public virtual string Validate(string propertyName, params Tuple<Expression<Func<bool>>, string>[] predicates)
{
if (!ValidationProperties.ContainsKey(propertyName))
throw new ArgumentException($"No property found with name '{propertyName}'. Make sure you register it first.");
foreach (var predicate in predicates)
{
if (predicate.Item1.Compile().Invoke())
{
ValidationProperties[propertyName] = false;
ValidationChangedEvent?.Invoke(this, false);
return predicate.Item2;
}
}
ValidationProperties[propertyName] = true;
if (ValidationProperties.All(pair => pair.Value))
ValidationChangedEvent?.Invoke(this, true);
return null;
}
And here's example of call which I want to be shorter:
var result = Validate(nameof(FilePath),
new Tuple<Expression<Func<bool>>, string>(() => string.IsNullOrEmpty(FilePath), "Select a file to load"));
Basically I want to get rid of: new Tuple<Expression<Func<bool>>, string>