예제 #1
0
 /// <summary>
 /// Called when a rule fails
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private void OnRuleFailed(object sender, RuleEventArgs args)
 {
     if (RuleFailed != null)
     {
         RuleFailed(sender, args);
     }
 }
예제 #2
0
        /// <summary>
        /// Checks all rules in this classes ruleset
        /// </summary>
        public void InvokeRules(object objToEvaluate)
        {
            if (objToEvaluate == null)
            {
                throw new ArgumentNullException("objToEvaluate");
            }

            if (!haveRegisteredRules)
            {
                RegisterRules();
            }

            OnBeginRulesEvaluation(this, objToEvaluate);

            if (rules == null)
            {
                OnEndRulesEvaluation(this, objToEvaluate);
                return;
            }

            string type = objToEvaluate.GetType().FullName;

            if (!rules.ContainsKey(type))
            {
                OnEndRulesEvaluation(this, objToEvaluate);
                logger.Info(String.Format(CultureInfo.InvariantCulture, "No rules exist for type {0}", type));
                return;
            }

            IList <MethodInfo> typeRules = rules[type];

            foreach (MethodInfo ruleMethod in typeRules)
            {
                object[] ruleAttributes = ruleMethod.GetCustomAttributes(typeof(RuleAttribute), false);
                if (ruleAttributes != null && ruleAttributes.Length == 1)
                {
                    RuleAttribute att  = ruleAttributes[0] as RuleAttribute;
                    RuleEventArgs args = new RuleEventArgs(att, objToEvaluate);

                    object ruleResponse = ruleMethod.Invoke(objToEvaluate, null);
                    bool   rulePassed   = (bool)ruleResponse;
                    if (rulePassed)
                    {
                        OnRulePassed(this, args);
                    }
                    else
                    {
                        OnRuleFailed(this, args);
                    }
                }
                else
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                                                      "All methods in rules must have a single RuleAttribute. The failing method is {0} of type {1}",
                                                                      ruleMethod.Name, type));
                }
            }

            OnEndRulesEvaluation(this, objToEvaluate);
        }
예제 #3
0
		/// <summary>
		/// Checks all rules in this classes ruleset
		/// </summary>
		public void InvokeRules(object objToEvaluate){
			if(objToEvaluate == null) throw new ArgumentNullException("objToEvaluate");
			
			if(!haveRegisteredRules)
				RegisterRules();
			
			OnBeginRulesEvaluation(this, objToEvaluate);
			
			if(rules == null){
				OnEndRulesEvaluation(this, objToEvaluate);
				return;
			}
			
			string type = objToEvaluate.GetType().FullName;
			if(!rules.ContainsKey(type)){
				OnEndRulesEvaluation(this, objToEvaluate);
				logger.Info(String.Format(CultureInfo.InvariantCulture, "No rules exist for type {0}", type));
				return;
			}
			
			IList<MethodInfo> typeRules = rules[type];
			foreach(MethodInfo ruleMethod in typeRules){

				object[] ruleAttributes = ruleMethod.GetCustomAttributes(typeof(RuleAttribute), false);
				if(ruleAttributes != null && ruleAttributes.Length == 1){
					RuleAttribute att = ruleAttributes[0] as RuleAttribute;
					RuleEventArgs args = new RuleEventArgs(att, objToEvaluate);
					
					object ruleResponse = ruleMethod.Invoke(objToEvaluate, null);
					bool rulePassed = (bool)ruleResponse;
					if(rulePassed)
						OnRulePassed(this, args);
					else
						OnRuleFailed(this, args);
				}else{
					throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
					                                                  "All methods in rules must have a single RuleAttribute. The failing method is {0} of type {1}",
					                                                  ruleMethod.Name, type ));
				}
			}
			
			OnEndRulesEvaluation(this, objToEvaluate);
		}
예제 #4
0
		/// <summary>
		/// Called when a rule fails
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="args"></param>
		private void OnRuleFailed(object sender, RuleEventArgs args)
		{
			if(RuleFailed != null)
				RuleFailed( sender, args );
		}
예제 #5
0
 public void HandlePassedRule(object sender, RuleEventArgs args){
     logger.Debug("Handle Passed Rule was called");
     this.calledRules.Push(args.RuleName + " passed");
 }