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.
RuleBaseClassesRoot.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6using Csla.Core;
7using Csla.Rules;
9using Csla.TestHelpers;
10using Csla.Threading;
11
13{
15 public class RuleBaseClassesRoot : BusinessBase<RuleBaseClassesRoot>
16 {
17 #region Properties
18
19 public static readonly PropertyInfo<int> CustomerIdProperty = RegisterProperty<int>(c => c.CustomerId);
20 public int CustomerId
21 {
22 get { return GetProperty(CustomerIdProperty); }
23 set { SetProperty(CustomerIdProperty, value); }
24 }
25
26 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
27 public string Name
28 {
29 get { return GetProperty(NameProperty); }
30 set { SetProperty(NameProperty, value); }
31 }
32
33 public static readonly PropertyInfo<int> Num1Property = RegisterProperty<int>(c => c.Num1);
34 public int Num1
35 {
36 get { return GetProperty(Num1Property); }
37 set { SetProperty(Num1Property, value); }
38 }
39
40 public static readonly PropertyInfo<int> Num2Property = RegisterProperty<int>(c => c.Num2);
41 public int Num2
42 {
43 get { return GetProperty(Num2Property); }
44 set { SetProperty(Num2Property, value); }
45 }
46
47 public static readonly PropertyInfo<int> SumProperty = RegisterProperty<int>(c => c.Sum);
48 public int Sum
49 {
50 get { return GetProperty(SumProperty); }
51 set { SetProperty(SumProperty, value); }
52 }
53
54 public static readonly PropertyInfo<string> CountryProperty = RegisterProperty<string>(c => c.Country);
55 public string Country
56 {
57 get { return GetProperty(CountryProperty); }
58 set { SetProperty(CountryProperty, value); }
59 }
60
61 public static readonly PropertyInfo<string> StateProperty = RegisterProperty<string>(c => c.State);
62 public string State
63 {
64 get { return GetProperty(StateProperty); }
65 set { SetProperty(StateProperty, value); }
66 }
67
68 public static readonly PropertyInfo<Csla.SmartDate> StartDateProperty = RegisterProperty<Csla.SmartDate>(c => c.StartDate, null, new Csla.SmartDate());
69 public string StartDate
70 {
71 get { return GetPropertyConvert<Csla.SmartDate, string>(StartDateProperty); }
72 set { SetPropertyConvert<Csla.SmartDate, string>(StartDateProperty, value); }
73 }
74
75 public static readonly PropertyInfo<Csla.SmartDate> EndDateProperty = RegisterProperty<Csla.SmartDate>(c => c.EndDate, null, new Csla.SmartDate());
76 public string EndDate
77 {
78 get { return GetPropertyConvert<Csla.SmartDate, string>(EndDateProperty); }
79 set { SetPropertyConvert<Csla.SmartDate, string>(EndDateProperty, value); }
80 }
81
82 #endregion
83
84 protected override void PropertyHasChanged(IPropertyInfo property)
85 {
86 base.PropertyHasChanged(property);
88 }
89
90 #region Validation Rules
91
92 protected override void AddBusinessRules()
93 {
94 // call base class implementation to add data annotation rules to BusinessRules
95 base.AddBusinessRules();
96
97 BusinessRules.RuleSet = "Date";
99 // above rule will run on both properies changed - no need for dependency
100
101 BusinessRules.RuleSet = "Lookup";
103 // rule will run async lookup of customer and set customer name
104
105 BusinessRules.RuleSet = "LookupAndNameRequired";
108 // rule will run async lookuop of customer, set customer name and rerun Required rule on Name
109
110 BusinessRules.RuleSet = "Object";
112
113
114 BusinessRules.RuleSet = "Required";
116
117 }
118
119 #endregion
120
121 #region Factory Methods
122
124 {
125 return dataPortal.Create(ruleSet);
126 }
127
128 #endregion
129
130 [RunLocal]
131 protected void DataPortal_Create(string ruleSet)
132 {
133 BusinessRules.RuleSet = ruleSet;
135 }
136 }
137
141 public class CalcSum : Csla.Rules.PropertyRule
142 {
148 public CalcSum(IPropertyInfo primaryProperty, params IPropertyInfo[] inputProperties)
149 : base(primaryProperty)
150 {
151 if (InputProperties == null)
152 {
153 InputProperties = new List<IPropertyInfo>();
154 }
155 InputProperties.AddRange(inputProperties);
156
157 CanRunOnServer = false;
158 }
159
160 protected override void Execute(IRuleContext context)
161 {
162 // Use linq Sum to calculate the sum value
163 var sum = context.InputPropertyValues.Sum(property => (dynamic)property.Value);
164
165 // add calculated value to OutValues
166 // When rule is completed the RuleEngine will update businessobject
167 context.AddOutValue(PrimaryProperty, sum);
168 }
169 }
170
171 public class ValidateRootObject : Csla.Rules.ObjectRule
172 {
174 : base()
175 {
179 }
180
181 protected override void Execute(IRuleContext context)
182 {
183 var customerId = (int)ReadProperty(context.Target, RuleBaseClassesRoot.CustomerIdProperty);
184
185 switch (customerId)
186 {
187 case 4:
188 context.AddErrorResult(RuleBaseClassesRoot.NameProperty, "customer name required");
189 context.AddErrorResult(RuleBaseClassesRoot.CountryProperty, "country required");
190 context.AddErrorResult(RuleBaseClassesRoot.StateProperty, "state required");
191 break;
192 case 5:
193 context.AddWarningResult(RuleBaseClassesRoot.NameProperty, "customer name required");
194 context.AddWarningResult(RuleBaseClassesRoot.CountryProperty, "country required");
195 context.AddWarningResult(RuleBaseClassesRoot.StateProperty, "state required");
196 break;
197 case 6:
198 context.AddInformationResult(RuleBaseClassesRoot.NameProperty, "customer name required");
199 context.AddInformationResult(RuleBaseClassesRoot.CountryProperty, "country required");
200 context.AddInformationResult(RuleBaseClassesRoot.StateProperty, "state required");
201 break;
202 }
203
204 }
205 }
206
210 public class LessThan : PropertyRule
211 {
212 private IPropertyInfo CompareTo { get; set; }
213
219 public LessThan(IPropertyInfo primaryProperty, IPropertyInfo compareToProperty)
220 : base(primaryProperty)
221 {
222 CompareTo = compareToProperty;
223 InputProperties = new List<IPropertyInfo>() { primaryProperty, compareToProperty };
224 AffectedProperties.Add(compareToProperty);
225 }
226
231 protected override void Execute(IRuleContext context)
232 {
233 var value1 = (IComparable)context.InputPropertyValues[PrimaryProperty];
234 var value2 = (IComparable)context.InputPropertyValues[CompareTo];
235
236 if (value1.CompareTo(value2) >= 0)
237 {
238 context.AddErrorResult(string.Format("{0} must be less than {1}", PrimaryProperty.FriendlyName, CompareTo.FriendlyName));
239 context.AddErrorResult(CompareTo, string.Format("{0} must be larger than {1}", CompareTo.FriendlyName, PrimaryProperty.FriendlyName));
240 }
241 }
242 }
243
245 {
246 public IPropertyInfo NameProperty { get; set; }
247 public LookupCustomer(IPropertyInfo primaryProperty, IPropertyInfo nameProperty)
248 : base(primaryProperty)
249 {
250 NameProperty = nameProperty;
251 InputProperties = new List<IPropertyInfo>() { primaryProperty };
253
254 CanRunOnServer = false;
255 CanRunInCheckRules = false;
257 IsAsync = true;
258 }
259
260 protected override async Task ExecuteAsync(IRuleContext context)
261 {
262 var customerId = (int)context.InputPropertyValues[PrimaryProperty];
263
264 await Task.Delay(200);
265 string name;
266 switch (customerId)
267 {
268 case 1:
269 name = "Rocky Lhotka";
270 break;
271 default:
272 name = string.Format("Customer_{0}", customerId);
273 break;
274 }
275 context.AddOutValue(NameProperty, name);
276 context.Complete();
277 context.AddSuccessResult(false);
278 }
279 }
280}
281
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
A strongly-typed resource class, for looking up localized strings, etc.
static string StringRequiredRule
Looks up a localized string similar to {0} required.
Maintains metadata about a property.
override bool IsAsync
Gets a value indicating whether the rule will run on a background thread.
virtual Csla.Core.IPropertyInfo PrimaryProperty
Gets or sets the primary property affected by this rule.
object ReadProperty(object obj, Csla.Core.IPropertyInfo propertyInfo)
Reads a property's field value.
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.
Tracks the business rules for a business object.
string?? RuleSet
Gets or sets the rule set to use for this business object instance.
List< string > CheckRules()
Invokes all rules for the business type.
void AddRule(IBusinessRuleBase rule)
Associates a business rule with the business object.
Business rule for a required string.
Definition: CommonRules.cs:106
Base class for object level rules.
Definition: ObjectRule.cs:18
Base class for a property rule
bool CanRunOnServer
Gets or sets a value indicating whether this instance can run in logical serverside data portal.
bool CanRunInCheckRules
Gets or sets a value indicating whether this instance can run when CheckRules is called on BO.
bool CanRunAsAffectedProperty
Gets or sets a value indicating whether this instance can run as affected property.
Base class for a property rule
Definition: PropertyRule.cs:17
bool CanRunOnServer
Gets or sets a value indicating whether this instance can run in logical serverside data portal.
CalcSum rule will set primary property to the sum of all.
CalcSum(IPropertyInfo primaryProperty, params IPropertyInfo[] inputProperties)
Initializes a new instance of the CalcSum class.
override void Execute(IRuleContext context)
Business or validation rule implementation.
Implements a rule to compare 2 property values and make sure property1 is less than property2
override void Execute(IRuleContext context)
Does the check for primary propert less than compareTo property
LessThan(IPropertyInfo primaryProperty, IPropertyInfo compareToProperty)
Initializes a new instance of the LessThan class.
override async Task ExecuteAsync(IRuleContext context)
Business or validation rule implementation.
LookupCustomer(IPropertyInfo primaryProperty, IPropertyInfo nameProperty)
static readonly PropertyInfo< int > Num1Property
static readonly PropertyInfo< string > StateProperty
static readonly PropertyInfo< string > CountryProperty
static readonly PropertyInfo< Csla.SmartDate > EndDateProperty
override void PropertyHasChanged(IPropertyInfo property)
static readonly PropertyInfo< int > CustomerIdProperty
static readonly PropertyInfo< Csla.SmartDate > StartDateProperty
static readonly PropertyInfo< string > NameProperty
static readonly PropertyInfo< int > Num2Property
static readonly PropertyInfo< int > SumProperty
static RuleBaseClassesRoot NewEditableRoot(IDataPortal< RuleBaseClassesRoot > dataPortal, string ruleSet)
override void Execute(IRuleContext context)
Business or validation rule implementation.
Maintains metadata about a property.
string FriendlyName
Gets the friendly display name for the property.
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Context information provided to a business rule when it is invoked.
Definition: IRuleContext.cs:22
object Target
Gets a reference to the target business object.
Definition: IRuleContext.cs:31
void AddWarningResult(string description)
Add a Warning severity result to the Results list.
void AddErrorResult(string description)
Add a Error severity result to the Results list.
void AddOutValue(object value)
Add an outbound value to update the rule's primary property on the business object once the rule is c...
void Complete()
Indicates that the rule processing is complete, so CSLA .NET will process the Results list.
void AddSuccessResult(bool stopProcessing)
Add a Success 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:37
void AddInformationResult(string description)
Add an Information severity result to the Results list.
@ CheckObjectRules
Called from CheckObjectRules
@ Serializable
Prevents updating or inserting until the transaction is complete.
Provides a date data type that understands the concept of an empty date value.
Definition: SmartDate.cs:32