Rules being checked after BO Save and triggering PropertyChanged #2827
-
It looks like my validation rules are running again after the save of my business object is complete. I'm trying to figure out if this is something I'm inadvertently doing somewhere, or if this is by design. So, does BusinessBase run CheckRules again after an update? Further, it looks like some of my business rules are triggering OnPropertyChanged for the PrimaryProperty of the rule. Is this normal? I don't appear to be actually changing the property. Here's the rule: private class LivingArrangementRequiresExplanation : CommonRules.NetsoftCommonBusinessRuleAsync
{
private IPropertyInfo AdvLivingArrangementIDProperty;
public LivingArrangementRequiresExplanation(IPropertyInfo primaryProp, IPropertyInfo AdvLivingArrangementIDProp) : base(primaryProp)
{
AdvLivingArrangementIDProperty = AdvLivingArrangementIDProp;
InputProperties = new List<IPropertyInfo>() { PrimaryProperty, AdvLivingArrangementIDProperty };
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CSLA0017:Find Business Rules That Do Not Use Add() Methods on the Context", Justification = "<Pending>")]
protected override async Task ExecuteAsync(IRuleContext context)
{
int advLivingArrangmentID = context.GetInputValue<int>(AdvLivingArrangementIDProperty);
string LivingArrangementExplanation = context.GetInputValue<string>(PrimaryProperty);
if (string.IsNullOrEmpty(LivingArrangementExplanation))
{
var cmd = await DataPortal.CreateAsync<LivingArrangementRequiresExplanationCommand>(advLivingArrangmentID, NdrnCommon.GetUserAdvocacyGroup());
cmd = await DataPortal.ExecuteAsync<LivingArrangementRequiresExplanationCommand>(cmd);
if (cmd is object && cmd.RequiresExplanation)
{
string desc;
if (string.IsNullOrEmpty(GetMessage()))
{
desc = "The Living Arrangement selected requires an explanation";
}
else
{
desc = MessageText;
}
SetContext(context, desc);
}
}
}
protected void SetContext(Csla.Rules.IRuleContext context, string desc)
{
SetContext(context, desc, Severity);
}
protected void SetContext(Csla.Rules.IRuleContext context, string desc, RuleSeverity sev)
{
switch (sev)
{
case RuleSeverity.Error:
{
context.AddErrorResult(desc);
break;
}
case RuleSeverity.Information:
{
context.AddInformationResult(desc);
break;
}
case RuleSeverity.Warning:
{
context.AddWarningResult(desc);
break;
}
case RuleSeverity.Success:
{
context.AddSuccessResult(false);
break;
}
}
}
}
} I'm not calling AddOutValue on the context, so I don't see why it would end up calling OnPropertyChanged for my ExplanationProperty. But if were to comment out this rule from running, then the PropertyChanged event isn't raised for my ExplanationProperty. This is happening with several rules that are hitting my database via a Command Object, but I don't see how that would generate a PropertyChanged even either. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
This is expected, because we don't have a way of knowing whether the UI needs refreshing based on the result of the rule (not the property value itself, but whether the UI is showing an info/warn/error message that needs to be updated). |
Beta Was this translation helpful? Give feedback.
-
Interesting. I created a simple test, and If I remember correctly, this is intended behavior, because we're merging the result of the update back into the original object graph, and the save operation may have changed property values on the logical server. As we merge those (potentially changed) property values back into the original object graph, it is necessary to rerun rules because the (potentially) new values might be different. |
Beta Was this translation helpful? Give feedback.
Interesting. I created a simple test, and
SaveAsync
doesn't, butSaveAndMergeAsync
does.If I remember correctly, this is intended behavior, because we're merging the result of the update back into the original object graph, and the save operation may have changed property values on the logical server. As we merge those (potentially changed) property values back into the original object graph, it is necessary to rerun rules because the (potentially) new values might be different.