CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
OneOfSeveralStringsRequiredRule.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Csla.Core;
5using Csla.Rules;
6
8{
13 {
14
19 public OneOfSeveralStringsRequiredRule(params Csla.Core.IPropertyInfo[] affectedProperties)
20 : base(affectedProperties[0])
21 {
22 foreach (Csla.Core.IPropertyInfo affectedProperty in affectedProperties)
23 {
24 InputProperties.Add(affectedProperty);
25 }
26 }
27
33 public OneOfSeveralStringsRequiredRule(string message, params Csla.Core.IPropertyInfo[] affectedProperties)
34 : this(affectedProperties)
35 {
36 MessageText = message;
37 }
38
44 public OneOfSeveralStringsRequiredRule(Func<string> messageDelegate, params Csla.Core.IPropertyInfo[] affectedProperties)
45 : this(affectedProperties)
46 {
47 MessageDelegate = messageDelegate;
48 }
49
50 public RuleSeverity Severity { get; set; } = RuleSeverity.Error;
51
56 protected override string GetMessage()
57 {
58 return HasMessageDelegate ? base.MessageText : "One of the properties must be provided";
59 }
60
65 protected override void Execute(IRuleContext context)
66 {
67 bool valueProvided = false;
68
69 // Iterate all of the properties and see if any of them have a value
70 foreach (var value in context.InputPropertyValues.Values)
71 {
72 if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
73 {
74 // Property has a value, so the rule is satisfied
75 valueProvided = true;
76 break;
77 }
78 }
79
80 if (!valueProvided)
81 {
82 // Rule not satisfied, so report it as such
83 foreach (Csla.Core.IPropertyInfo propertyInfo in InputProperties)
84 {
85 var message = string.Format(GetMessage(), propertyInfo.FriendlyName);
86 context.Results.Add(new RuleResult(RuleName, propertyInfo, message) { Severity = Severity });
87 }
88 }
89 }
90 }
91
92}
Business rule for when one of two strings must be provided
OneOfSeveralStringsRequiredRule(string message, params Csla.Core.IPropertyInfo[] affectedProperties)
Creates an instance of the rule.
override void Execute(IRuleContext context)
Rule implementation.
OneOfSeveralStringsRequiredRule(params Csla.Core.IPropertyInfo[] affectedProperties)
Creates an instance of the rule.
OneOfSeveralStringsRequiredRule(Func< string > messageDelegate, params Csla.Core.IPropertyInfo[] affectedProperties)
Creates an instance of the rule.
string RuleName
Gets a unique rule:// URI for the specific instance of the rule within the context of the business ob...
List< Csla.Core.IPropertyInfo > InputProperties
Gets a list of secondary property values to be supplied to the rule when it is executed.
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
Maintains metadata about a property.
Context information provided to a business rule when it is invoked.
Definition: IRuleContext.cs:22
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16