Reload BusinessRules collection in CSLA 4

Reload BusinessRules collection in CSLA 4

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


Wayne Hiller posted on Friday, May 14, 2010

Hi there,

I have custom Validation rules stored in a database that I load at runtime in AddBusinessRules(). If the user makes changes to the rules from within the app I need to be able to clear the existing rules for an object type and force AddBusinessRules to be called again. Any ideas? I did this in 3.8 but it was quite a bit of work finding all the places the rules were cached etc. Now in Csla 4 the rules system has really changed so instead of digging for hours I thought I might just ask first this time.

P.S. Rocky, I love the new Business Rules system. It seems so much more natural than the previous system. Good work :)

 

Wayne

RockfordLhotka replied on Friday, May 14, 2010

The idea I have for this, and you can see if it meets your needs, is that you'd use rule chaining. Basically you create one rule and attach it to your property. That rule is what dynamically loads any other rules, and invokes them using rule chaining. This one rule wouldn't ever add an error message (except in the case of an exception I suppose), but rather is purely responsible for invoking your other rules.

This way CSLA will do its normal thing - invoking your rule when rules should be run. And your rule in turn will invoke whatever rules you have dynamically loaded for that property. Each of those rules will do its normal thing, operating against the chained context (see my blog post on chaining).

Wayne Hiller replied on Friday, May 14, 2010

Thanks Rocky,

This may work for me however as far as I can tell I would need to Access the database every time the rule fired. I suppose I can design a cache system on my list of Authorization rules. I really need to dig into this rule engine to understand it better. I am a bit confused as to how Authorization rules fit into this. I have not looked at the code but does an Authorization rule get fired very time one of the BusinessRules.HasPermission methods is called?

I will spend some time today digging into the code and create some unit tests to follow the flow to see where my dynamic rules can be loaded. The nice part of this new design is that I can also use whatever process I come up with to also dynamically load business rules as well as authorization rules.

The way I did it in version 3.8 worked well as per type rules were only loaded once in AddAuthorizationRules. As rules rarely change this was excellent. The trick was that I needed to reload them if the user made changes. Loading rules every time they are used makes it a lot easier to deal with user changes but performance could be an issue. 

I have always wondered how programmers were able to anticipate what Roles an application would need and what operations are allowed for each role. I have a large business app and I really can't see how I could hard code authorizations as every Customer seems to want something different. I think it would be quite valuable to develop a "Dynamic" rule/authorization example for others to use as a starting point for this kind of thing. If I come up with something decent I will post it. 

Wayne 

Copyright (c) Marimer LLC