Exemple #1
0
        private List <string> CheckRules(Csla.Core.IPropertyInfo property, RuleContextModes executionContext)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (_suppressRuleChecking)
            {
                return(new List <string>());
            }

            var oldRR = RunningRules;

            RunningRules = true;

            var affectedProperties = new List <string>();

            affectedProperties.AddRange(CheckRulesForProperty(property, true, executionContext));

            RunningRules = oldRR;
            if (!RunningRules && !RunningAsyncRules)
            {
                _target.AllRulesComplete();
            }
            return(affectedProperties.Distinct().ToList());
        }
Exemple #2
0
 public Rule2(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     IsAsync         = true;
     _innerRule      = new Rule1(primaryProperty);
     InputProperties = _innerRule.InputProperties;
 }
Exemple #3
0
        /// <summary>
        /// Adds a lambda rule with fluent coding style
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="businessRules">The business rules.</param>
        /// <param name="primaryProperty">The primary property.</param>
        /// <param name="ruleHandler">The rule handler.</param>
        /// <param name="message">The message.</param>
        /// <param name="severity">The rule severity.</param>
        public static void AddRule <T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func <T, bool> ruleHandler, string message, RuleSeverity severity)
            where T : Csla.Core.BusinessBase
        {
            var rule = new CommonRules.Lambda(primaryProperty, (o) =>
            {
                var target = (T)o.Target;
                using (target.BypassPropertyChecks)
                {
                    if (!ruleHandler.Invoke(target))
                    {
                        o.Results.Add(new RuleResult(o.Rule.RuleName, primaryProperty, string.Format(message, o.Rule.PrimaryProperty.FriendlyName))
                        {
                            Severity = severity
                        });
                    }
                }
            });

      #if !NETFX_CORE   // must set unique name
            var methodName = ruleHandler.Method.ToString();
            rule.AddQueryParameter("s", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
      #endif

            businessRules.AddRule(rule);
        }
Exemple #4
0
 public CheckLazyInputFieldExists(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     InputProperties = new List <Core.IPropertyInfo> {
         primaryProperty
     };
 }
Exemple #5
0
 public DoAsyncRule(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     InputProperties = new List <IPropertyInfo> {
         PrimaryProperty
     };
     IsAsync = true;
 }
Exemple #6
0
 public void AddDirtyProperty(Csla.Core.IPropertyInfo property)
 {
     if (!Rule.AffectedProperties.Contains(property))
     {
         throw new ArgumentOutOfRangeException(property.Name, string.Format(Resources.PropertyNotInAffectedPropertiesException, property.Name));
     }
     _dirtyProperties.Value.Add(property);
 }
Exemple #7
0
 /// <summary>
 /// Add an outbound value to update a property on the business
 /// object once the rule is complete.
 /// </summary>
 /// <param name="property">Property to update.</param>
 /// <param name="value">New property value.</param>
 /// <exception cref="System.ArgumentOutOfRangeException">When property is not defined in AffectedProperties list.</exception>
 public void AddOutValue(Csla.Core.IPropertyInfo property, object value)
 {
     if (!Rule.AffectedProperties.Contains(property))
     {
         throw new ArgumentOutOfRangeException(property.Name, string.Format(Resources.PropertyNotInAffectedPropertiesException, property.Name));
     }
     _outputPropertyValues.Value.Add(property, value);
 }
Exemple #8
0
 public Rule3(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     IsAsync                = true;
     _innerRule             = new Rule1(primaryProperty);
     InputProperties        = _innerRule.InputProperties;
     ProvideTargetWhenAsync = true;
 }
Exemple #9
0
 public Rule1(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     IsAsync         = true;
     InputProperties = new List <IPropertyInfo> {
         primaryProperty
     };
 }
 public FiveSecondsLongRule(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     IsAsync         = true;
     InputProperties = new List <Core.IPropertyInfo> {
         primaryProperty
     };
 }
Exemple #11
0
 /// <summary>
 /// Add a Error severity result to the Results list.
 /// This method is only allowed on "object" level rules to allow an object level rule to set warning result on a field.
 /// </summary>
 /// <param name="property">Property to which the result applies.</param>
 /// <param name="description">Human-readable description of
 /// why the rule failed.</param>
 /// <exception cref="System.ArgumentOutOfRangeException">When property is not defined in AffectedProperties list.</exception>
 public void AddErrorResult(Csla.Core.IPropertyInfo property, string description)
 {
     if (!Rule.AffectedProperties.Contains(property))
     {
         throw new ArgumentOutOfRangeException(property.Name, string.Format(Resources.PropertyNotInAffectedPropertiesException, property.Name));
     }
     Results.Add(new RuleResult(Rule.RuleName, property, description));
 }
Exemple #12
0
 public IntegerRequired(Csla.Core.IPropertyInfo primaryProperty, int emptyValue)
     : base(primaryProperty)
 {
     this.EmptyValue      = emptyValue;
     this.InputProperties = new List <Csla.Core.IPropertyInfo> {
         primaryProperty
     };
 }
Exemple #13
0
 public TwoSecondsLongRule(Csla.Core.IPropertyInfo primaryProperty)
     : base(primaryProperty)
 {
     IsAsync         = true;
     InputProperties = new List <Core.IPropertyInfo> {
         primaryProperty
     };
     base.RunMode = RunModes.DenyOnServerSidePortal | RunModes.DenyCheckRules;
 }
Exemple #14
0
 public TwoProps(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo secondProperty)
     : base(primaryProperty)
 {
     SecondaryProperty = secondProperty;
     AffectedProperties.Add(SecondaryProperty);
     InputProperties = new List <Core.IPropertyInfo> {
         PrimaryProperty, SecondaryProperty
     };
 }
Exemple #15
0
 /// <summary>
 /// Creates an instance of the rule.
 /// </summary>
 /// <param name="primaryProperty">Primary property for the rule.</param>
 /// <param name="dependencyProperties">Dependent property.</param>
 /// <remarks>
 /// When rules are run for one of the dependency properties the rules for primary property i rerun.
 /// </remarks>
 public DependencyFrom(Csla.Core.IPropertyInfo primaryProperty, params Csla.Core.IPropertyInfo[] dependencyProperties)
     : base(primaryProperty)
 {
     if (InputProperties == null)
     {
         InputProperties = new List <IPropertyInfo>();
     }
     InputProperties.AddRange(dependencyProperties);
 }
Exemple #16
0
 /// <summary>
 /// Creates an instance of the rule.
 /// </summary>
 /// <param name="primaryProperty">Primary property for the rule.</param>
 /// <param name="dependencyProperties">Dependent property.</param>
 /// <remarks>
 /// When rules are run for one of the dependency properties the rules for primary property i rerun.
 /// </remarks>
 public DependencyFrom(Csla.Core.IPropertyInfo primaryProperty, params Csla.Core.IPropertyInfo[] dependencyProperties)
     : base(primaryProperty)
 {
     if (InputProperties == null)
     {
         InputProperties = new List <IPropertyInfo>();
     }
     InputProperties.AddRange(dependencyProperties);
     RuleUri.AddQueryParameter("dependencyfrom", string.Join(",", dependencyProperties.Select(p => p.Name)));
 }
Exemple #17
0
 /// <summary>
 /// Creates an instance of the rule.
 /// </summary>
 /// <param name="primaryProperty">Primary property for the rule.</param>
 /// <param name="dependencyProperty">The dependency property.</param>
 /// <param name="isBiDirectional">if set to <c>true</c> [is bi directional].</param>
 /// <remarks>
 /// When rules are run for one of the dependency properties the rules for primary property i rerun.
 /// </remarks>
 public DependencyFrom(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo dependencyProperty, bool isBiDirectional)
     : base(primaryProperty)
 {
     if (InputProperties == null)
     {
         InputProperties = new List <IPropertyInfo>();
     }
     InputProperties.Add(dependencyProperty);
     if (isBiDirectional)
     {
         AffectedProperties.Add(dependencyProperty);
     }
 }
Exemple #18
0
        /// <summary>
        /// Gets a value indicating whether a managed field
        /// exists for the specified property.
        /// </summary>
        /// <param name="obj">Business object.</param>
        /// <param name="property">Property info object.</param>
        protected bool FieldExists(object obj, Csla.Core.IPropertyInfo property)
        {
            var target = obj as Core.IManageProperties;

            if (target != null)
            {
                return(target.FieldExists(property));
            }
            else
            {
                throw new ArgumentException(Resources.IManagePropertiesRequiredException);
            }
        }
Exemple #19
0
        /// <summary>
        /// Invokes all rules for a specific property of the business type.
        /// </summary>
        /// <param name="property">Property to check.</param>
        /// <returns>
        /// Returns a list of property names affected by the invoked rules.
        /// The PropertyChanged event should be raised for each affected
        /// property.
        /// </returns>
        public List <string> CheckRules(Csla.Core.IPropertyInfo property)
        {
            if (_suppressRuleChecking)
            {
                return(new List <string>());
            }

            var oldRR = RunningRules;

            RunningRules = true;

            var affectedProperties = new List <string>();

            affectedProperties.AddRange(CheckRulesForProperty(property, true));

            RunningRules = oldRR;
            if (!RunningRules && !RunningAsyncRules)
            {
                _target.AllRulesComplete();
            }
            return(affectedProperties.Distinct().ToList());
        }
Exemple #20
0
        /// <summary>
        /// Invokes all rules for a specific property.
        /// </summary>
        private List <string> CheckRulesForProperty(Csla.Core.IPropertyInfo property, bool cascade)
        {
            var rules = from r in TypeRules.Rules
                        where ReferenceEquals(r.PrimaryProperty, property)
                        orderby r.Priority
                        select r;
            var affectedProperties = new List <string> {
                property.Name
            };

            BrokenRules.ClearRules(property);
            affectedProperties.AddRange(RunRules(rules));

            if (cascade)
            {
                // get properties affected by all rules
                var propertiesToRun = new List <Csla.Core.IPropertyInfo>();
                foreach (var item in rules)
                {
                    foreach (var p in item.AffectedProperties)
                    {
                        if (!ReferenceEquals(property, p))
                        {
                            propertiesToRun.Add(p);
                        }
                    }
                }
                // run rules for affected properties
                foreach (var item in propertiesToRun.Distinct())
                {
                    CheckRulesForProperty(item, false);
                }
            }

            return(affectedProperties.Distinct().ToList());
        }
Exemple #21
0
        /// <summary>
        /// Adds a lambda property level rule to business rules.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="businessRules">Business rules instance.</param>
        /// <param name="ruleHandler">Rule method.</param>
        /// <param name="primaryProperty">Primary property.</param>
        /// <param name="ruleArgs">Rule args object.</param>
        /// <param name="priority">Priority.</param>
        //[Obsolete("For migration of older apps to Csla 4.x only")]
        public static void AddRule <T>(this BusinessRules businessRules, RuleHandler <T, RuleArgs> ruleHandler, Csla.Core.IPropertyInfo primaryProperty, RuleArgs ruleArgs, int priority) where T : BusinessBase <T>
        {
            var rule = new Csla.Rules.CommonRules.Lambda(primaryProperty, (o) =>
            {
                var target = (T)o.Target;
                using (new ObjectAccessor().BypassPropertyChecks(target))
                {
                    if (!ruleHandler(target, ruleArgs))
                    {
                        o.Results.Add(new RuleResult(o.Rule.RuleName, o.Rule.PrimaryProperty, ruleArgs.Description)
                        {
                            Severity = ruleArgs.Severity, StopProcessing = ruleArgs.StopProcessing
                        });
                    }
                    else if (ruleArgs.StopProcessing)
                    {
                        o.AddSuccessResult(true);
                    }
                    else
                    {
                        o.AddSuccessResult(false);
                    }
                    ruleArgs.StopProcessing = false;
                }
            });

#if NETFX_CORE
            var methodName = Guid.NewGuid().ToString();
#else
            var methodName = ruleHandler.Method.ToString();
#endif
            rule.AddQueryParameter("r", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
            rule.Priority = priority;
            businessRules.AddRule(rule);
        }
Exemple #22
0
 /// <summary>
 /// Adds a lambda property level rule to business rules.
 /// </summary>
 /// <param name="businessRules">Business rules</param>
 /// <param name="ruleHandler">Rule method</param>
 /// <param name="primaryProperty">The primary property.</param>
 /// <param name="priority">Priority</param>
 //[Obsolete("For migration of older apps to Csla 4.x only")]
 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, Csla.Core.IPropertyInfo primaryProperty, int priority)
 {
     AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
 }
Exemple #23
0
 /// <summary>
 /// Gets a value indicating whether a specific
 /// property has any async rules running.
 /// </summary>
 /// <param name="property">Property to check.</param>
 public bool GetPropertyBusy(Csla.Core.IPropertyInfo property)
 {
     return(BusyProperties.Contains(property));
 }
Exemple #24
0
 /// <summary>
 /// Adds a lambda property level rule to business rules.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="businessRules">Business rules.</param>
 /// <param name="ruleHandler">Rule method.</param>
 /// <param name="primaryProperty">Primary property.</param>
 //[Obsolete("For migration of older apps to Csla 4.x only")]
 public static void AddRule <T>(this BusinessRules businessRules, RuleHandler <T, RuleArgs> ruleHandler, Csla.Core.IPropertyInfo primaryProperty) where T : BusinessBase <T>
 {
     AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
 }
Exemple #25
0
 /// <summary>
 /// Adds a lambda rule with fluent coding style
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="businessRules">The business rules.</param>
 /// <param name="primaryProperty">The primary property.</param>
 /// <param name="ruleHandler">The rule handler.</param>
 /// <param name="message">The message.</param>
 public static void AddRule <T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func <T, bool> ruleHandler, string message) where T : BusinessBase
 {
     AddRule(businessRules, primaryProperty, ruleHandler, message, RuleSeverity.Error);
 }
Exemple #26
0
 /// <summary>
 /// Add an outbound value to update a property on the business
 /// object once the rule is complete.
 /// </summary>
 /// <param name="property">Property to update.</param>
 /// <param name="value">New property value.</param>
 /// <exception cref="System.ArgumentOutOfRangeException">When property is not defined in AffectedProperties list.</exception>
 public void AddOutValue(Csla.Core.IPropertyInfo property, object value)
 {
     _outputPropertyValues.Value[property] = value;
 }
Exemple #27
0
 /// <summary>
 /// Invokes all rules for a specific property of the business type.
 /// </summary>
 /// <param name="property">Property to check.</param>
 /// <returns>
 /// Returns a list of property names affected by the invoked rules.
 /// The PropertyChanged event should be raised for each affected
 /// property.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">If property is null</exception>
 public List <string> CheckRules(Csla.Core.IPropertyInfo property)
 {
     return(CheckRules(property, RuleContextModes.PropertyChanged));
 }
Exemple #28
0
        /// <summary>
        /// Adds a lambda rule with fluent coding style to the RuleSet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="businessRules">The business rules.</param>
        /// <param name="ruleSet">The RuleSet.</param>
        /// <param name="primaryProperty">The primary property.</param>
        /// <param name="ruleHandler">The rule handler.</param>
        /// <param name="messageDelegate">The message delegate.</param>
        /// <param name="severity">The rule severity.</param>
        public static void AddRule <T>(this BusinessRules businessRules, string ruleSet, Csla.Core.IPropertyInfo primaryProperty, Func <T, bool> ruleHandler, Func <string> messageDelegate, RuleSeverity severity)
            where T : Csla.Core.BusinessBase
        {
            var rule = new CommonRules.Lambda(primaryProperty, (o) =>
            {
                var target = (T)o.Target;
                using (target.BypassPropertyChecks)
                {
                    if (!ruleHandler.Invoke(target))
                    {
                        o.Results.Add(new RuleResult(o.Rule.RuleName, primaryProperty, messageDelegate.Invoke())
                        {
                            Severity = severity
                        });
                    }
                }
            });
            var methodName = ruleHandler.Method.ToString();

            rule.AddQueryParameter("s", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
            businessRules.AddRule(rule, ruleSet);
        }
Exemple #29
0
        /// <summary>
        /// Invokes all rules for a specific property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="cascade">if set to <c>true</c> [cascade].</param>
        /// <param name="executionContext">The execute context.</param>
        /// <returns></returns>
        private List <string> CheckRulesForProperty(Csla.Core.IPropertyInfo property, bool cascade, RuleContextModes executionContext)
        {
            var rules = from r in TypeRules.Rules
                        where ReferenceEquals(r.PrimaryProperty, property) &&
                        CanRunRule(r, executionContext)
                        orderby r.Priority
                        select r;

            BrokenRules.ClearRules(property);
            var firstResult = RunRules(rules, cascade, executionContext);

            if (CascadeOnDirtyProperties)
            {
                cascade = cascade || firstResult.DirtyProperties.Any();
            }
            if (cascade)
            {
                // get properties affected by all rules
                var propertiesToRun = new List <Csla.Core.IPropertyInfo>();
                foreach (var item in rules)
                {
                    if (!item.IsAsync)
                    {
                        foreach (var p in item.AffectedProperties)
                        {
                            if (!ReferenceEquals(property, p))
                            {
                                propertiesToRun.Add(p);
                            }
                        }
                    }
                }

                // add PrimaryProperty where property is in InputProperties
                var input = from r in TypeRules.Rules
                            where !ReferenceEquals(r.PrimaryProperty, property) &&
                            r.PrimaryProperty != null &&
                            r.InputProperties != null &&
                            r.InputProperties.Contains(property)
                            select r.PrimaryProperty;

                foreach (var p in input)
                {
                    if (!ReferenceEquals(property, p))
                    {
                        propertiesToRun.Add(p);
                    }
                }
                // run rules for affected properties
                foreach (var item in propertiesToRun.Distinct())
                {
                    var doCascade = false;
                    if (CascadeOnDirtyProperties)
                    {
                        doCascade = firstResult.DirtyProperties.Any(p => p == item.Name);
                    }
                    firstResult.AffectedProperties.AddRange(CheckRulesForProperty(item, doCascade,
                                                                                  executionContext | RuleContextModes.AsAffectedPoperty));
                }
            }

            // always make sure to add PrimaryProperty
            firstResult.AffectedProperties.Add(property.Name);
            return(firstResult.AffectedProperties.Distinct().ToList());
        }
Exemple #30
0
 /// <summary>
 /// Adds a lambda rule with fluent coding style
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="businessRules">The business rules.</param>
 /// <param name="primaryProperty">The primary property.</param>
 /// <param name="ruleHandler">The rule handler.</param>
 /// <param name="messageDelegate">The message delegate.</param>
 public static void AddRule <T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func <T, bool> ruleHandler, Func <string> messageDelegate) where T : BusinessBase
 {
     AddRule(businessRules, ApplicationContext.DefaultRuleSet, primaryProperty, ruleHandler, messageDelegate, RuleSeverity.Error);
 }