I've often thought that it would be nice to have a direct way to add a permanent Information 'broken rule' for a property. The kind of thing you'd do in DataPortal_Create() or DP_Fetch().
Maybe like this:
ValidationRules.AddInformationMessage(IPropertyInfo property, string text)
Or if we wanted to be able add/remove them:
ValidationRules.SetInformationMessage(IPropertyInfo property, string ruleName, string text)
Such a message would never auto-add or auto-remove as other rules run, but they'd be in the broken rules list and so would appear in the UI, etc.
Probably something that should go on the wish list - it seems like this is the kind of thing that'd make your scenario pretty easy.
In the meantime, the solution is to write a rule method and attach it to each of your properties. The trick is to provide that rule method with access to the comparison data, which can be accomplished by storing it as a NonSerialized field on your business class itself.
This is far from elegant, but something like this should work:
[Serializable]
public class Test : BusinessBase<Test>
{
private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}
protected override void AddBusinessRules()
{
base.AddBusinessRules();
ValidationRules.AddRule(InfoRule, NameProperty);
}
private static bool InfoRule(object target, Csla.Validation.RuleArgs e)
{
var obj = target as Test;
var comp = obj._comparisonData;
if (obj != null && comp != null)
{
e.Severity = Csla.Validation.RuleSeverity.Information;
switch (e.PropertyName)
{
case "Name":
if (obj.Name != comp.Name)
{
e.Description = "Name is different";
return false;
}
break;
default:
break;
}
}
return true;
}
[NonSerialized]
private Test _comparisonData;
private void DataPortal_Fetch(SingleCriteria<Test, int> criteria)
{
// load object data
// load comparison data
ValidationRules.CheckRules();
}
}
Copyright (c) Marimer LLC