/// <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); }
/// <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); }
/// <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); }