CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BusinessRulesExtensions.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusinessRulesExtensions.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Extension methods to simplify lambda business rules</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Text;
10using Csla.Core;
11
12namespace Csla.Rules
13{
17 public static class BusinessRulesExtensions
18 {
28 public static void AddRule<T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, string message, RuleSeverity severity)
29 where T: Csla.Core.BusinessBase
30 {
31 var rule = new CommonRules.Lambda(primaryProperty, (o) =>
32 {
33 var target = (T) o.Target;
34 using (target.BypassPropertyChecks)
35 {
36 if (!ruleHandler.Invoke(target))
37 {
38 o.Results.Add(new RuleResult(o.Rule.RuleName, primaryProperty, string.Format(message, o.Rule.PrimaryProperty.FriendlyName)) {Severity = severity});
39 }
40 }
41 });
42 var methodName = ruleHandler.Method.ToString();
43 rule.AddQueryParameter("s", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
44
45 businessRules.AddRule(rule);
46 }
47
56 public static void AddRule<T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, string message) where T : BusinessBase
57 {
58 AddRule(businessRules, primaryProperty, ruleHandler, message, RuleSeverity.Error);
59 }
60
71 public static void AddRule<T>(this BusinessRules businessRules, string ruleSet, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, string message, RuleSeverity severity)
72 where T : Csla.Core.BusinessBase
73 {
74 var rule = new CommonRules.Lambda(primaryProperty, (o) =>
75 {
76 var target = (T)o.Target;
77 using (target.BypassPropertyChecks)
78 {
79 if (!ruleHandler.Invoke(target))
80 {
81 o.Results.Add(new RuleResult(o.Rule.RuleName, primaryProperty, string.Format(message, o.Rule.PrimaryProperty.FriendlyName)) { Severity = severity });
82 }
83 }
84 });
85 var methodName = ruleHandler.Method.ToString();
86 rule.AddQueryParameter("s", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
87 businessRules.AddRule(rule, ruleSet);
88 }
89
99 public static void AddRule<T>(this BusinessRules businessRules, string ruleSet, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, string message) where T : BusinessBase
100 {
101 AddRule(businessRules, primaryProperty, ruleHandler, message, RuleSeverity.Error);
102 }
103
113 public static void AddRule<T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, Func<string> messageDelegate, RuleSeverity severity)
114 where T: Csla.Core.BusinessBase
115 {
116 AddRule(businessRules, ApplicationContext.DefaultRuleSet, primaryProperty, ruleHandler, messageDelegate, severity);
117 }
118
119
128 public static void AddRule<T>(this BusinessRules businessRules, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, Func<string> messageDelegate) where T : BusinessBase
129 {
130 AddRule(businessRules, ApplicationContext.DefaultRuleSet, primaryProperty, ruleHandler, messageDelegate, RuleSeverity.Error);
131 }
132
143 public static void AddRule<T>(this BusinessRules businessRules, string ruleSet, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, Func<string> messageDelegate, RuleSeverity severity)
144 where T : Csla.Core.BusinessBase
145 {
146 var rule = new CommonRules.Lambda(primaryProperty, (o) =>
147 {
148 var target = (T)o.Target;
149 using (target.BypassPropertyChecks)
150 {
151 if (!ruleHandler.Invoke(target))
152 {
153 o.Results.Add(new RuleResult(o.Rule.RuleName, primaryProperty, messageDelegate.Invoke()) { Severity = severity });
154 }
155 }
156 });
157 var methodName = ruleHandler.Method.ToString();
158 rule.AddQueryParameter("s", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
159 businessRules.AddRule(rule, ruleSet);
160 }
161
171 public static void AddRule<T>(this BusinessRules businessRules, string ruleSet, Csla.Core.IPropertyInfo primaryProperty, Func<T, bool> ruleHandler, Func<string> messageDelegate) where T : BusinessBase
172 {
173 AddRule(businessRules, ruleSet, primaryProperty, ruleHandler, messageDelegate, RuleSeverity.Error);
174 }
175 }
176}
This is the non-generic base class from which most business objects will be derived.
Maintains metadata about a property.
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16