CSLA 4.0 BusinessRule Sub System

CSLA 4.0 BusinessRule Sub System

Old forum URL: forums.lhotka.net/forums/t/9976.aspx


vbbeta posted on Monday, January 24, 2011

 Hi guys I have noticed a strange behavior on one of the rules which is that when I override the Protected Sub Execute its not being called when the rule is being called or added into the Collection Of rules only the Sub New of the Rule Class is being Invoked, Any ideas why this is happing,

 

JonnyBee replied on Tuesday, January 25, 2011

Serveral possible reasons:

1. Usage of RuleSets allows you to define separate sets of Rules for a BusinessObject.  Rules that do not exist in the "current" RuleSet will not be called during validation.

2.  Rules with priority > BusinessRules.ProcessThroughPriority will not be called if a previous Rule added ErrorResult (ie: RuleSeverity.Error).

3. A previous rule has ShortCircuited rule processing for this Field.

You should be able to see all the registered rules for a typ i the debugger in <bo>.BusinessRules._typeRules

 

 

 

vbbeta replied on Tuesday, January 25, 2011

Hi thanks for the reply My problem is not that its not being called the problem is that its being called but not the "Execute Sub Method " only the sub new of the class is being called

tmg4340 replied on Tuesday, January 25, 2011

It's been a while, but I believe that under CSLA 4, rule objects are shared across business-object instances - which means they are only created once.  So you would see your constructor run, probably fairly early in your application lifecycle, and possibly before any business objects actually need to be validated.  But it would only run once.

Jonny's recommendations still apply - you may have other rules that are precluding this rule from being run.  Priority settings, along with possible short-circuiting of rules, could cause your rule not to run.  Or using RuleSets could exclude your rule from the set of currently-processing rules.  These are the most likely places to start your investigation.

HTH

- Scott

JonnyBee replied on Tuesday, January 25, 2011

Hi,

If you could supply the code for a sample rule and how you register the rule in the Business Object we may identify the problem more precisely.

 

 

vbbeta replied on Tuesday, January 25, 2011

Its a very Simple vb Class here is the example

 <Serializable()>

Public Class Transaction
Inherits BusinessBase(Of Transaction)

Protected Overrides Sub AddBusinessRules()

MyBase.AddBusinessRules()

BusinessRules.AddRule(New RequierdInterest(InterestRateProperty, IsSale))

BusinessRules.AddRule(New RequierdInterest(TransactionTypeProperty, IsSale))

BusinessRules.AddRule(New InterestRateCalculater(IdProperty))
End Sub 

 Private Class InterestRateCalculater

 Inherits Csla.Rules.BusinessRule

 Public Sub New(ByVal prop As Csla.Core.IPropertyInfo)

 

MyBase.New(prop)

 End Sub

THIS SUB IS NOT BEING CALLED WHEN EVER THIS RULE IS BEING EXECUTED ANY HELP WHY THIS METHOD IS NOT BEING CALLED NOT BY ADDING NOR BY RUNING THIS RULE 

Protected Overrides Sub Execute(context As Csla.Rules.RuleContext)

'My code here 

context.Complete()

End Sub 

End Class

 

 

 

JonnyBee replied on Tuesday, January 25, 2011

The InterestRateCalculator is expected to be registered to IdProperty

BusinessRules.AddRule(New InterestRateCalculater(IdProperty))

Thus, your constructor on the rule is not correct:

 Public Sub New(ByVal prop As Csla.Core.IPropertyInfoMy

     Base.New(TransactionTotalProperty) )

and should be: 

Public Sub New(ByVal prop As Csla.Core.IPropertyInfo )

    MyBase.New(prop) 

JonnyBee replied on Tuesday, January 25, 2011

Execute should not be called when adding the rule.

The instance of the new rule is added to a RulesMangaer for all instances of this business object type.

The Execute method will be called

Copyright (c) Marimer LLC