'Csla.Rules.BusinessRuleManager' does not contain a definition for 'GetRulesForType'

'Csla.Rules.BusinessRuleManager' does not contain a definition for 'GetRulesForType'

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


wasiuddin.786 posted on Thursday, December 13, 2012

hi

i am updating CSLA 4.1(Silverlight)  to CSLA 4.2(Silverlight)  i am getting compile time error,i am sending brief code below,

 

 

 BusinessRuleManager typeRules = BusinessRuleManager.GetRulesForType(this.GetType(), this.BusinessRules.RuleSet == "default" ? null : BusinessRules.RuleSet);          
        //check if dev wants to manager priority himself, if he does thant he will be responsible for the call sequence.
        bool IsNotPrioritized = (from r in typeRules.Rules
                                 where r.Priority > 0
                                 select r).Count() == 0;

Error 2 'Csla.Rules.BusinessRuleManager' does not contain a definition for 'GetRulesForType' D:\Devlopment\CslaUpdatesEdmos\Phoenix_V_1\Northwind\Phoenix.Model.Base\Phoenix.Model.Base.Server\PhoenixModelBase.cs 160 61 Phoenix.Model.Base.Server

JonnyBee replied on Friday, December 14, 2012

The it seems you/your company has made changes to CSLA yourself.
BusinessRuleManager.GetRulesForType is INTERNAL inside CSLA -  not available for your code!!

This is the code from Csla 4.1 (from the download page)

 /// <summary>
  /// Manages the list of rules for a business type.
  /// </summary>
  public class BusinessRuleManager
  {
#if !SILVERLIGHT
    private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>> _perTypeRules =
      new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>>();

    internal static BusinessRuleManager GetRulesForType(Type type, string ruleSet)
    {
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      return _perTypeRules.Value.GetOrAdd(key, (t) => { return new BusinessRuleManager(); });
    }
#else
    private static Dictionary<RuleSetKey, BusinessRuleManager> _perTypeRules = new Dictionary<RuleSetKey, BusinessRuleManager>();

    internal static BusinessRuleManager GetRulesForType(Type type, string ruleSet)
    {
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      BusinessRuleManager result = null;
      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      if (!_perTypeRules.TryGetValue(key, out result))
      {
        lock (_perTypeRules)
        {
          if (!_perTypeRules.TryGetValue(key, out result))
          {
            result = new BusinessRuleManager();
            _perTypeRules.Add(key, result);
          }
        }
      }
      return result;
    }

#endif

bootz replied on Wednesday, April 17, 2013

On a related note here, I am wondering exactly how to get or "extract" the business rules from a business object. I was successful in calling Csla.Rules.BusinessRuleManager.GetRulesForType(...) in the VS "Immediate" window... but it doesn't work in code outside the assembly :(

(FYI I'm trying to extract business rules for a given businessRule object to pass back to a JavaScript App/Client via REST/WCF.)

I did find a post you made on another forum:

 

“This sort of thing is exactly why rules can be added programatically through AddBusinessRules and not just with attributes. I specifically allow programatic adding of rules with the intent that people could write code in AddBusinessRules to use a read-only object to get the rules for the object type.”

 http://forums.lhotka.net/forums/t/10056.aspx

I guess my question is, can you point me in the right direction for a code sample or tutorial? Seems like it should be trivial, but I can't find it in "Expert C# 2008:BO" and I'm just a n00b :)

 

JonnyBee replied on Wednesday, April 17, 2013

Hi, 

You should be able to call BusinessRules.GetRuleDescriptions within the <businessobject>. You may need to expose  this a e a new method on the <businessobject>. The rule description should provide information required to parse into javascript rules. 

Sample content:

"rule://csla.rules.commonrules.maxvalue-system.int32-/Num1?max=5000"
"rule://csla.rules.commonrules.maxlength/Name?max=10"

bootz replied on Thursday, April 18, 2013

Thanks, that is helpful however I am returning custom rules which have descriptions like this:

BusinessRules.GetRuleDescriptions()

{string[5]}

    [0]: "rule://transplus.genesis.business.businessrules.required/80/ClaimedIdentifier"

    [1]: "rule://transplus.genesis.business.businessrules.required/81/EmailAddress"

    [2]: "rule://transplus.genesis.business.businessrules.regularexpressionmatch//82/EmailAddress"

    [3]: "rule://transplus.genesis.business.businessrules.required/84/UserId"

    [4]: "rule://transplus.genesis.business.businessrules.minimumlength/84/UserId"

Is there some way I can attach to them the relevant metadata (like regex or min val) as a query param, as you have shown?

bootz replied on Thursday, April 18, 2013

Never mind, I think I found it:

        public RegularExpressionMatch(IPropertyInfo primaryProperty, int id, string pattern)  : base(primaryProperty, id)

        {

            this.Pattern = pattern;

            this.InputProperties = new List<IPropertyInfo> { primaryProperty };

            this.RuleUri = new RuleUri(string.Concat(this.RuleUri.RuleTypeName, "/", id), this.RuleUri.PropertyName);

            this.RuleUri.AddQueryParameter("pattern", pattern);

        }

 

Also, would you recommend exposing the BusinessRules.getRuleDescriptions with a "mixin" method for all of my business objects? It seems like redundant, "base" functionality..

Copyright (c) Marimer LLC