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.
Csla/Rules/CommonRules.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="CommonRules.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Base class used to create common rules.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using Csla.Properties;
13using System.Text.RegularExpressions;
14
16{
20 public abstract class CommonBusinessRule : PropertyRule
21 {
25 public RuleSeverity Severity { get; set; }
26
31 protected CommonBusinessRule(Csla.Core.IPropertyInfo primaryProperty) : base(primaryProperty)
32 {
33 Severity = RuleSeverity.Error;
34 }
35
40 {
41 Severity = RuleSeverity.Error;
42 }
43 }
44
50 {
54 public System.ComponentModel.DataAnnotations.ValidationAttribute Attribute { get; private set; }
55
61 public DataAnnotation(Csla.Core.IPropertyInfo primaryProperty, System.ComponentModel.DataAnnotations.ValidationAttribute attribute)
62 : base(primaryProperty)
63 {
64 this.Attribute = attribute;
65 RuleUri.AddQueryParameter("a", attribute.GetType().FullName);
66 if (primaryProperty != null)
67 InputProperties.Add(primaryProperty);
68 }
69
74 protected override void Execute(IRuleContext context)
75 {
76 var ctx = new System.ComponentModel.DataAnnotations.ValidationContext(context.Target, null, null);
77 if (PrimaryProperty != null)
78 ctx.MemberName = PrimaryProperty.FriendlyName;
79
80 System.ComponentModel.DataAnnotations.ValidationResult result = null;
81 try
82 {
83 if (PrimaryProperty != null)
84 {
85 object value = context.InputPropertyValues[PrimaryProperty];
86 result = this.Attribute.GetValidationResult(value, ctx);
87 }
88 else
89 {
90 result = this.Attribute.GetValidationResult(null, ctx);
91 }
92 }
93 catch (Exception ex)
94 {
95 context.AddErrorResult(ex.Message);
96 }
97 if (result != null)
98 context.AddErrorResult(result.ErrorMessage);
99 }
100 }
101
106 {
111 public Required(Csla.Core.IPropertyInfo primaryProperty)
112 : base(primaryProperty)
113 {
114 InputProperties.Add(primaryProperty);
115 }
116
117
123 public Required(Csla.Core.IPropertyInfo primaryProperty, string message)
124 : this(primaryProperty)
125 {
126 MessageText = message;
127 }
128
134 public Required(Csla.Core.IPropertyInfo primaryProperty, Func<string> messageDelegate )
135 : this(primaryProperty)
136 {
137 MessageDelegate = messageDelegate;
138 }
139
144 protected override string GetMessage()
145 {
147 }
148
153 protected override void Execute(IRuleContext context)
154 {
155 var value = context.InputPropertyValues[PrimaryProperty];
156 if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
157 {
158 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName);
159 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
160 }
161 }
162 }
163
168 {
172 public int Max { get; private set; }
173
179 public MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max)
180 : base(primaryProperty)
181 {
182 Max = max;
183 this.RuleUri.AddQueryParameter("max", max.ToString());
184 InputProperties.Add(primaryProperty);
185 }
186
193 public MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max, string message)
194 : this(primaryProperty, max)
195 {
196 MessageText = message;
197 }
198
205 public MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max, Func<string> messageDelegate)
206 : this(primaryProperty, max)
207 {
208 MessageDelegate = messageDelegate;
209 }
210
215 protected override string GetMessage()
216 {
218 }
219
224 protected override void Execute(IRuleContext context)
225 {
226 var value = context.InputPropertyValues[PrimaryProperty];
227 if (value != null && value.ToString().Length > Max)
228 {
229 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName, Max);
230 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
231 }
232 }
233 }
234
239 {
243 public int Min { get; private set; }
244
250 public MinLength(Csla.Core.IPropertyInfo primaryProperty, int min)
251 : base(primaryProperty)
252 {
253 Min = min;
254 this.RuleUri.AddQueryParameter("min", min.ToString());
255 InputProperties.Add(primaryProperty);
256 }
257
264 public MinLength(Csla.Core.IPropertyInfo primaryProperty, int min, string message)
265 : this(primaryProperty, min)
266 {
267 MessageText = message;
268 }
269
276 public MinLength(Csla.Core.IPropertyInfo primaryProperty, int min, Func<string> messageDelegate)
277 : this(primaryProperty, min)
278 {
279 MessageDelegate = messageDelegate;
280 }
281
286 protected override string GetMessage()
287 {
289 }
290
295 protected override void Execute(IRuleContext context)
296 {
297 var value = context.InputPropertyValues[PrimaryProperty];
298 if (value != null && value.ToString().Length < Min)
299 {
300 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName, Min);
301 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
302 }
303 }
304 }
305
309 public class MinValue<T> : CommonBusinessRule
310 where T : IComparable
311 {
315 public T Min { get; private set; }
320 public string Format { get; set; }
321
327 public MinValue(Csla.Core.IPropertyInfo primaryProperty, T min)
328 : base(primaryProperty)
329 {
330 Min = min;
331 this.RuleUri.AddQueryParameter("min", min.ToString());
332 InputProperties.Add(primaryProperty);
333 }
334
341 public MinValue(Csla.Core.IPropertyInfo primaryProperty, T min, string message)
342 : this(primaryProperty, min)
343 {
344 MessageText = message;
345 }
346
353 public MinValue(Csla.Core.IPropertyInfo primaryProperty, T min, Func<string> messageDelegate)
354 : this(primaryProperty, min)
355 {
356 MessageDelegate = messageDelegate;
357 }
358
363 protected override string GetMessage()
364 {
365 return HasMessageDelegate ? base.MessageText : Csla.Properties.Resources.MinValueRule;
366 }
367
372 protected override void Execute(IRuleContext context)
373 {
374 var value = context.InputPropertyValues[PrimaryProperty] != null
376 : PrimaryProperty.DefaultValue != null
377 ? (T)PrimaryProperty.DefaultValue
378 : default(T);
379
380 var result = value.CompareTo(Min);
381 if (result <= -1)
382 {
383 string outValue;
384 if (string.IsNullOrEmpty(Format))
385 outValue = Min.ToString();
386 else
387 outValue = string.Format(string.Format("{{0:{0}}}", Format), Min);
388 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName, outValue);
389 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
390 }
391 }
392 }
393
397 public class MaxValue<T> : CommonBusinessRule
398 where T : IComparable
399 {
403 public T Max { get; private set; }
408 public string Format { get; set; }
409
415 public MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max)
416 : base(primaryProperty)
417 {
418 Max = max;
419 this.RuleUri.AddQueryParameter("max", max.ToString());
420 InputProperties.Add(primaryProperty);
421 }
422
429 public MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max, string message)
430 : this(primaryProperty, max)
431 {
432 MessageText = message;
433 }
434
441 public MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max, Func<string> messageDelegate)
442 : this(primaryProperty, max)
443 {
444 MessageDelegate = messageDelegate;
445 }
446
451 protected override string GetMessage()
452 {
453 return HasMessageDelegate ? base.MessageText : Csla.Properties.Resources.MaxValueRule;
454 }
455
460 protected override void Execute(IRuleContext context)
461 {
462 var value = context.InputPropertyValues[PrimaryProperty] != null
464 : PrimaryProperty.DefaultValue != null
465 ? (T)PrimaryProperty.DefaultValue
466 : default(T);
467
468 var result = value.CompareTo(Max);
469 if (result >= 1)
470 {
471 string outValue;
472 if (string.IsNullOrEmpty(Format))
473 outValue = Max.ToString();
474 else
475 outValue = string.Format(string.Format("{{0:{0}}}", Format), Max);
476 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName, outValue);
477 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
478 }
479 }
480 }
481
486 {
487 #region NullResultOptions
488
494 {
500 ReturnFalse,
506 ReturnTrue,
514 ConvertToEmptyString
515 }
516
517 #endregion
518
523 public string Expression { get; private set; }
524
529 public NullResultOptions NullOption { get; set; }
530
536 public RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression)
537 : base(primaryProperty)
538 {
539 Expression = expression;
540 RuleUri.AddQueryParameter("e", expression);
541 InputProperties.Add(primaryProperty);
542 }
543
550 public RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression, string message)
551 : this(primaryProperty, expression)
552 {
553 MessageText = message;
554 }
555
556
563 public RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression, Func<string> messageDelegate)
564 : this(primaryProperty, expression)
565 {
566 MessageDelegate = messageDelegate;
567 }
568
573 protected override string GetMessage()
574 {
575 return HasMessageDelegate ? base.MessageText : Csla.Properties.Resources.RegExMatchRule;
576 }
577
582 protected override void Execute(IRuleContext context)
583 {
584 var value = context.InputPropertyValues[PrimaryProperty];
585 bool ruleSatisfied;
586 var expression = new Regex(Expression);
587
588 if (value == null && NullOption == NullResultOptions.ConvertToEmptyString)
589 value = string.Empty;
590
591 if (value == null)
592 {
593 // if the value is null at this point
594 // then return the pre-defined result value
595 ruleSatisfied = (NullOption == NullResultOptions.ReturnTrue);
596 }
597 else
598 {
599 // the value is not null, so run the
600 // regular expression
601 ruleSatisfied = expression.IsMatch(value.ToString());
602 }
603
604 if (!ruleSatisfied)
605 {
606 var message = string.Format(GetMessage(), PrimaryProperty.FriendlyName);
607 context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
608 }
609 }
610 }
611
621 {
625 public string MessageText
626 {
627 get { return _messageDelegate.Invoke(); }
628 protected set { MessageDelegate = () => value; }
629 }
630
631 private Func<string> _messageDelegate;
632
640 public Func<string> MessageDelegate
641 {
642 get { return _messageDelegate; }
643 set { _messageDelegate = value; }
644 }
645
650 public InfoMessage(Csla.Core.IPropertyInfo primaryProperty)
651 : base(primaryProperty)
652 {
653 Priority = 1;
654 }
655
661 public InfoMessage(Csla.Core.IPropertyInfo primaryProperty, string messageText)
662 : this(primaryProperty)
663 {
664 MessageText = messageText;
665 }
666
672 public InfoMessage(Csla.Core.IPropertyInfo primaryProperty, Func<string> messageDelegate)
673 : this(primaryProperty)
674 {
675 MessageDelegate = messageDelegate;
676 }
677
682 protected override void Execute(IRuleContext context)
683 {
685 }
686 }
687
691 public class Lambda : BusinessRule
692 {
697 public Lambda(Action<IRuleContext> rule)
698 {
699 Initialize(rule);
700 }
701
707 public Lambda(Csla.Core.IPropertyInfo primaryProperty, Action<IRuleContext> rule)
708 : base(primaryProperty)
709 {
710 Initialize(rule);
711 }
712
713 private void Initialize(Action<IRuleContext> rule)
714 {
715 Rule = rule;
716 var methodName = Rule.Method.ToString();
717 base.RuleUri.AddQueryParameter("r", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
718 }
719
725 public void AddQueryParameter(string key, string value)
726 {
727 base.RuleUri.AddQueryParameter(key, value);
728 }
729
730 private Action<IRuleContext> Rule { get; set; }
731
736 protected override void Execute(IRuleContext context)
737 {
738 Rule(context);
739 }
740 }
741
746 {
758 public Dependency(Csla.Core.IPropertyInfo primaryProperty, params Csla.Core.IPropertyInfo[] dependencyProperty)
759 : base(primaryProperty)
760 {
761 AffectedProperties.AddRange(dependencyProperty);
762 }
763 }
764}
A strongly-typed resource class, for looking up localized strings, etc.
static string MaxValueRule
Looks up a localized string similar to {0} can not exceed {1}.
static string StringMaxLengthRule
Looks up a localized string similar to {0} can not exceed {1} characters.
static string StringRequiredRule
Looks up a localized string similar to {0} required.
static string MinValueRule
Looks up a localized string similar to {0} can not be less than {1}.
static string RegExMatchRule
Looks up a localized string similar to {0} does not match regular expression.
static string StringMinLengthRule
Looks up a localized string similar to {0} must be at least {1} characters.
virtual Csla.Core.IPropertyInfo PrimaryProperty
Gets or sets the primary property affected by this rule.
string RuleName
Gets a unique rule:// URI for the specific instance of the rule within the context of the business ob...
int Priority
Gets the rule priority.
List< Csla.Core.IPropertyInfo > InputProperties
Gets a list of secondary property values to be supplied to the rule when it is executed.
List< Csla.Core.IPropertyInfo > AffectedProperties
Gets a list of properties affected by this rule.
Base class used to create business and validation rules.
Definition: BusinessRule.cs:15
Base class used to create common rules.
CommonBusinessRule()
Creates an instance of the rule.
RuleSeverity Severity
Gets or sets the severity for this rule.
CommonBusinessRule(Csla.Core.IPropertyInfo primaryProperty)
Creates an instance of the rule.
Business rule that encapsulates a DataAnnotations ValidationAttribute rule.
override void Execute(IRuleContext context)
Rule implementation.
DataAnnotation(Csla.Core.IPropertyInfo primaryProperty, System.ComponentModel.DataAnnotations.ValidationAttribute attribute)
Creates an instance of the rule.
System.ComponentModel.DataAnnotations.ValidationAttribute Attribute
Gets the ValidationAttribute instance.
A rule that establishes a dependency between two properties.
Dependency(Csla.Core.IPropertyInfo primaryProperty, params Csla.Core.IPropertyInfo[] dependencyProperty)
Creates an instance of the rule.
Adds an information message to a property.
string MessageText
Gets the default description used by this rule.
InfoMessage(Csla.Core.IPropertyInfo primaryProperty)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Rule implementation.
InfoMessage(Csla.Core.IPropertyInfo primaryProperty, string messageText)
Creates an instance of the rule.
InfoMessage(Csla.Core.IPropertyInfo primaryProperty, Func< string > messageDelegate)
Creates an instance of the rule.
Func< string > MessageDelegate
Gets or sets the localizable message function.
A business rule defined by a lambda expression.
Lambda(Action< IRuleContext > rule)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Executes the rule.
Lambda(Csla.Core.IPropertyInfo primaryProperty, Action< IRuleContext > rule)
Creates an instance of the rule.
void AddQueryParameter(string key, string value)
Add a query parameter to make the RuleUri uniques for this rule and primary property.
Business rule for a maximum length string.
MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Rule implementation.
MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max, Func< string > messageDelegate)
Creates an instance of the rule.
int Max
Gets the max length value.
override string GetMessage()
Gets the error message.
MaxLength(Csla.Core.IPropertyInfo primaryProperty, int max, string message)
Creates an instance of the rule.
Business rule for a maximum value.
MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max, string message)
Creates an instance of the rule.
override string GetMessage()
Gets the error message.
MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max, Func< string > messageDelegate)
Creates an instance of the rule.
MaxValue(Csla.Core.IPropertyInfo primaryProperty, T max)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Rule implementation.
string Format
Gets or sets the format string used to format the Max value.
Business rule for a minimum length string.
override void Execute(IRuleContext context)
Rule implementation.
override string GetMessage()
Gets the error message.
MinLength(Csla.Core.IPropertyInfo primaryProperty, int min)
Creates an instance of the rule.
MinLength(Csla.Core.IPropertyInfo primaryProperty, int min, Func< string > messageDelegate)
Creates an instance of the rule.
MinLength(Csla.Core.IPropertyInfo primaryProperty, int min, string message)
Creates an instance of the rule.
int Min
Gets the min length value.
Business rule for a minimum value.
string Format
Gets or sets the format string used to format the Min value.
MinValue(Csla.Core.IPropertyInfo primaryProperty, T min)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Rule implementation.
MinValue(Csla.Core.IPropertyInfo primaryProperty, T min, Func< string > messageDelegate)
Creates an instance of the rule.
override string GetMessage()
Gets the error message.
MinValue(Csla.Core.IPropertyInfo primaryProperty, T min, string message)
Creates an instance of the rule.
Business rule that evaluates a regular expression.
RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression)
Creates an instance of the rule.
RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression, Func< string > messageDelegate)
Creates an instance of the rule.
NullResultOptions NullOption
Gets or sets a value that controls how null input values are handled.
override void Execute(IRuleContext context)
Rule implementation.
RegExMatch(Csla.Core.IPropertyInfo primaryProperty, string expression, string message)
Creates an instance of the rule.
override string GetMessage()
Gets the error message.
NullResultOptions
List of options for the NullResult property.
string Expression
Gets the regular expression to be evaluated.
Business rule for a required string.
override void Execute(IRuleContext context)
Rule implementation.
Required(Csla.Core.IPropertyInfo primaryProperty, Func< string > messageDelegate)
Creates an instance of the rule.
Required(Csla.Core.IPropertyInfo primaryProperty)
Creates an instance of the rule.
override string GetMessage()
Gets the error message.
Required(Csla.Core.IPropertyInfo primaryProperty, string message)
Creates an instance of the rule.
Base class for a property rule
Definition: PropertyRule.cs:17
Func< string > MessageDelegate
Gets or sets the error message function for this rule.
Definition: PropertyRule.cs:31
bool HasMessageDelegate
Gets a value indicating whether this instance has message delegate.
Definition: PropertyRule.cs:40
string MessageText
Gets or sets the error message (constant).
Definition: PropertyRule.cs:22
Contains information about the result of a rule.
Definition: RuleResult.cs:20
Parses a rule:// URI to provide easy access to the parts of the URI.
Definition: RuleUri.cs:19
void AddQueryParameter(string key, string value)
Adds a query parameter to the URI.
Definition: RuleUri.cs:113
Maintains metadata about a property.
Context information provided to a business rule when it is invoked.
Definition: IRuleContext.cs:20
object Target
Gets a reference to the target business object.
Definition: IRuleContext.cs:29
List< RuleResult > Results
Gets a list of RuleResult objects containing the results of the rule.
Definition: IRuleContext.cs:52
void AddErrorResult(string description)
Add a Error severity result to the Results list.
Dictionary< Csla.Core.IPropertyInfo, object > InputPropertyValues
Gets a dictionary containing copies of property values from the target business object.
Definition: IRuleContext.cs:35
void AddInformationResult(string description)
Add an Information severity result to the Results list.
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16