business rules and predicate expressions

Posts   
 
    
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 01-Nov-2005 10:16:37   

Is it possible to evaluate a predicate expression without actually sending it to the database? Something like the following code:


PredicateExpression bizzRule = BusinessRulesEngine.Rule1();
bool ruleOK = EvaluateRule(bizzRule, someEntity);

This would be a great way to define business rules only once. We would be able to use the above form in situations where we do complex logic inside C# code and reuse the same expression when fetching a collection that satisfies the business rule.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Nov-2005 10:39:48   

Sure, you can examine every predicate in the predicate expression. Do a foreach over the PredicateExpression object, and you'll get predicateexpressionelement objects. These can be a predicate, predicate expression or an operator (AND/OR).

Frans Bouma | Lead developer LLBLGen Pro
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 01-Nov-2005 12:04:27   

I think i'm missing something. The PredicateExpression object is not enumerable.


PredicateExpression test = new PredicateExpression();
foreach(IPredicateExpressionElement element in test)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Nov-2005 12:44:05   

You're right flushed , use a for loop, use the Count property and use the indexer.

Frans Bouma | Lead developer LLBLGen Pro
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 01-Nov-2005 13:49:38   

Thanks, i have now a working concept.

The only thing is that it requires a lot of conditional processing. This is probably not the best way under heavy loads. I will try to find some alternatives.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Nov-2005 14:51:21   

wvnoort wrote:

Thanks, i have now a working concept.

The only thing is that it requires a lot of conditional processing. This is probably not the best way under heavy loads. I will try to find some alternatives.

A predicate contains an enum value, which might help you: InstanceType, it's an int but of type PredicateType. You can use that to quickly determine the actual type of the predicate object: a predicateexpression or a particular predicate class. This can help controlling the flow of the interpretation.

Frans Bouma | Lead developer LLBLGen Pro