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.
AsyncRuleRoot.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Threading;
5using System.Threading.Tasks;
6using Csla.Core;
7using Csla.Rules;
9
11{
13 public class AsyncRuleRoot : BusinessBase<AsyncRuleRoot>
14 {
15 public static readonly PropertyInfo<string> CustomerNumberProperty = RegisterProperty<string>(c => c.CustomerNumber);
16
17 public string CustomerNumber
18 {
19 get { return GetProperty(CustomerNumberProperty); }
20
21 set { SetProperty(CustomerNumberProperty, value); }
22 }
23
24 public static readonly PropertyInfo<string> CustomerNameProperty = RegisterProperty<string>(c => c.CustomerName);
25
26 public string CustomerName
27 {
28 get { return GetProperty(CustomerNameProperty); }
29
30 set { SetProperty(CustomerNameProperty, value); }
31 }
32
33 public static readonly PropertyInfo<string> AsyncAwaitProperty = RegisterProperty<string>(c => c.AsyncAwait);
34 public string AsyncAwait
35 {
36 get { return GetProperty(AsyncAwaitProperty); }
37 set { SetProperty(AsyncAwaitProperty, value); }
38 }
39
41 {
42 return dataPortal.Create();
43 }
44
46 {
47 }
48
49 protected override void AddBusinessRules()
50 {
53
54 // async rule will only run when CustomerNumber has value
55 BusinessRules.AddRule(new LookupCustomerRule(CustomerNumberProperty, CustomerNameProperty) { Priority = 10 });
56
57 BusinessRules.AddRule(new AsyncAwaitRule(AsyncAwaitProperty));
58 }
59
60 private class LookupCustomerRule : Csla.Rules.BusinessRule
61 {
62 private IPropertyInfo _nameProperty;
63
64 public LookupCustomerRule(IPropertyInfo primaryProperty, IPropertyInfo nameProperty)
65
66 : base(primaryProperty)
67 {
68 _nameProperty = nameProperty;
69 AffectedProperties.Add(nameProperty);
70 InputProperties = new List<IPropertyInfo>() { primaryProperty };
71
72 IsAsync = true;
73 }
74
75 protected override void Execute(IRuleContext context)
76 {
77 var cn = (string)context.InputPropertyValues[PrimaryProperty];
78
79 var bw = new System.ComponentModel.BackgroundWorker();
80
81 bw.RunWorkerCompleted += (o, e) =>
82 {
83 context.AddOutValue(_nameProperty, string.Format("customer name {0}", cn));
84
85 context.Complete();
86 };
87 bw.DoWork += (o, e) =>
88 {
89 Thread.Sleep(50);
90 };
91 bw.RunWorkerAsync();
92
93 }
94 }
95
96 private class AsyncAwaitRule : BusinessRuleAsync
97 {
98 public AsyncAwaitRule(IPropertyInfo primaryProperty)
99 : base(primaryProperty)
100 { }
101
102 protected override async Task ExecuteAsync(IRuleContext context)
103 {
104 await Task.Delay(0);
105 context.AddOutValue("abc");
106 }
107 }
108
109 [Create]
110 private void Create()
111 {
113 }
114 }
115}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Maintains metadata about a property.
Base class used to create async business and validation rules.
Base class used to create business and validation rules.
Definition: BusinessRule.cs:15
Tracks the business rules for a business object.
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
static readonly PropertyInfo< string > AsyncAwaitProperty
static readonly PropertyInfo< string > CustomerNameProperty
static readonly PropertyInfo< string > CustomerNumberProperty
static AsyncRuleRoot NewRoot(IDataPortal< AsyncRuleRoot > dataPortal)
Maintains metadata about a 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
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.
Dictionary< Csla.Core.IPropertyInfo, object > InputPropertyValues
Gets a dictionary containing copies of property values from the target business object.
Definition: IRuleContext.cs:37
@ Serializable
Prevents updating or inserting until the transaction is complete.
@ Execute
Execute operation.