Auth Rules of CSLA.net 4 - RuleHandler -RuleArgs

Auth Rules of CSLA.net 4 - RuleHandler -RuleArgs

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


JJLoubser posted on Thursday, November 11, 2010

Csla.Rules.RuleHandler is replace with what in Auth Rules of CSLA.net 4 ?

sample:

private class EmailRule<SupplierMarketingER> : Csla.Rules.BusinessRule
        {
            protected override void Execute(RuleContext context)
            {
                var target = (SupplierMarketingER)context.Target;

                Csla.Rules.RuleHandler emailRule = new Csla.Rules.RuleHandler(Csla.Rules.CommonRules.RegExMatch);

                Csla.Rules.CommonRules.RegExRuleArgs args = new Csla.Rules.CommonRules.RegExRuleArgs(e.PropertyName, Csla.Rules.CommonRules.RegExPatterns.Email);

                bool result = emailRule(target, args);

                if (!result)
                {
                    context.AddErrorResult(" is not a valid e-mail address");
                   
                }
                return result;
            }
        }

JJLoubser replied on Thursday, November 11, 2010

I also have a problem to upgrade this:

<csla 3.7>

//public static bool ListEntrySelected<T>(T target, Csla.Rules.RuleArgs e) where T : UserCurrentTransportEC
        //{

        //    System.Reflection.PropertyInfo pi = target.GetType().GetProperty(e.PropertyName);

        //    int result = (int)pi.GetValue(target, null);

        //    if (result <= 0)
        //    {
        //        e.Severity = Csla.Rules.RuleSeverity.Error;
        //        e.Description = string.Format("{0} must have a selected value", Csla.Rules.RuleArgs.GetPropertyName(e));
        //        return false;
        //    }
        //    else
        //        return true;
        //}

 

<to Csla 4.1>

        private class ListEntrySelected<T> : Csla.Rules.BusinessRule
        {
            protected override void Execute(RuleContext context)
            {
                var target = (UserCurrentTransportEC)context.Target;
               
                System.Reflection.PropertyInfo pi = target.GetType().GetProperty(e.PropertyName);

                int result = (int)pi.GetValue(target, null);

                if (result <= 0)
                {
                   
                    context.AddErrorResult("{0} must have a selected value", Csla.Rules.RuleArgs.GetPropertyName(e)));
                   
                }
                else{}
                  
            }
        }

 

how do I get RuleArgs?

JonnyBee replied on Thursday, November 11, 2010

Instead of RuleArgs in 3.x you create member variables in your rule class and initalize them either as contructor parametere or as properties.

In Csla 4 BusinessRule has a property: PrimaryProperty (type IPropertyInfo) and a load of new feature like InputProperties,  AffectedProperties and OutputProperties.

I'd also recommend to look at the BusinessRuleDemo in the Samples download for Csla 4.

Your rule would look something like this:

        public class ListEntrySelected : Csla.Rules.BusinessRule
        {

            public ListEntrySelected(IPropertyInfo primaryProperty) : base(primaryProperty)
            {
                    if (this.InputProperties == null) this.InputProperties = new List<IPropertyInfo>();
                    InputProperties.Add(primaryProperty);
            }

  
            protected override void Execute(RuleContext context)
            {

                int value = (int) context.InputPropertyValues[PrimaryProperty];

                if (value <= 0)
                {
                   
                    context.AddErrorResult("{0} must have a selected value", PrimaryProperty.FriendlyName);
                   
                }
                 
            }
        }

JJLoubser replied on Thursday, January 27, 2011

rokkie took out this in CommonRules:

 

  //  public static string GetPattern(RegExPatterns pattern)
                //  {
                //    switch (pattern)
                //    {
                //      case RegExPatterns.SSN:
                //        return @"^\d{3}-\d{2}-\d{4}$";
                //      case RegExPatterns.Email:
                //        return @"^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$";
                //      default:
                //        return string.Empty;
                //    }
                //  }
                //}

 

 

i fix and replace it with this in my  protected override void Execute(Csla.Rules.RuleContext context)
            {

...

 if (System.Text.RegularExpressions.Regex.IsMatch(CheckUserLogonNameCO_.LogonName, @"^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$") == false)
                    {

                        context.AddErrorResult("Invalid Email Address");
                        context.Complete();
                    }
                    else
                    {

...

 

<for the email rule>

 

chethantr replied on Monday, October 08, 2012

Hi,

All most similar situation! Below is my piece of code

         public static readonly string abc= "temp";

 

        public static bool Method_VI<T>(object target, RuleArgs ruleArgs) where T : IComparable

        {

            var args = (DecoratedRuleArgs)ruleArgs;

            var pi = target.GetType().GetProperty(ruleArgs.PropertyName);

            var value = (T)pi.GetValue(target, null);

            var range = ((T[])args[abc]) as IList;

            var returnval = range.Contains(value);

            if (!returnval)

            {

                ruleArgs.Description = string.Format(CultureInfo.CurrentUICulture, "Not Valid", RuleArgs.GetPropertyName(ruleArgs));

            }

            return returnval;

        }


        public class  Method_VIRuleArgs<T> : DecoratedRuleArgs

        {

            public  Method_VIRuleArgs (string propertyName, params T[] correctvalues)  : base(propertyName)

            {

                this[ abc] =  correctvalues ;

            }

       }

and this was called like

BusinessRules.AddRule(CustomRules. Method_VI <ValueDirection>, new CustomRules. Method_VIRuleArgs <ValueDirection >(_ValueDirectionProperty, CommandDirectionValidValues()));

//public void AddRule(RuleHandler handler, RuleArgs args)


where CommandDirectionValidValues() returns array of enum

now in 4.X.X version, how can i re-write my code? coz now BusinessRule.AddRule has only 5 overload method which doesn't expect any Rule Handler!

 

JonnyBee replied on Monday, October 08, 2012

Hi,

All rules in Csla 4.x is classes so you must create a new Rule class that is derived from Csla.Rules.BusinessRule (I recommend to use the PropertyRule base class for property rules).

I do recommend the Using CSLA 4 ebooks from Rocky. For the rules engine the UsingCsla4-02-Objects ebook has a chapter on the new Rule Engine.
You may also want to look at the Net/cs/RuleTutorial sample in the samples downloads.

Othere resources is:
http://www.lhotka.net/weblog/CSLA4BusinessRulesSubsystem.aspx
http://www.lhotka.net/weblog/CSLA4BusinessRuleChaining.aspx
http://www.lhotka.net/weblog/CSLA4AuthorizationRules.aspx

There's loads of new stuff -  to much to even list in a post here - so best suggestion is to get the ebook.

tiago replied on Monday, October 08, 2012

And, of course, Jonny's own blog post CSLA 4.2 Rules update.

Copyright (c) Marimer LLC