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.
HasAsyncRule.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="HasAsyncRule.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using Csla.Rules;
13using System.ComponentModel;
14using System.Threading;
15using Csla.Core;
16
18{
19 public partial class HasAsyncRule : BusinessBase<HasAsyncRule>
20 {
21 public static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
22 public string Name
23 {
24 get { return GetProperty(NameProperty); }
25 set { SetProperty(NameProperty, value); }
26 }
27
28 protected override void AddBusinessRules()
29 {
33 }
34
35 public class Rule1 : BusinessRule
36 {
37 public Rule1(Csla.Core.IPropertyInfo primaryProperty)
38 : base(primaryProperty)
39 {
40 IsAsync = true;
41 InputProperties = new List<IPropertyInfo> { primaryProperty };
42 }
43
44 protected override void Execute(IRuleContext context)
45 {
46 if (context.Target != null)
47 throw new ArgumentOutOfRangeException("context.Target must be null");
48
49 BackgroundWorker worker = new BackgroundWorker();
50 // Using closures to access the context would be easier but this is not possible
51 // in all languages. Below is an example of how to use the context without closures
52
53 worker.DoWork += (s, e) =>
54 {
55 var avrc = (RuleContext)e.Argument;
56 e.Result = avrc;
57 System.Threading.Thread.Sleep(50);
58 var name = avrc.InputPropertyValues[NameProperty];
59 if (name != null && name.ToString() == "error")
60 avrc.AddErrorResult("error detected");
61 };
62 worker.RunWorkerCompleted += (s, e) =>
63 {
64 var avrc = (RuleContext)e.Result;
65 if (e.Error != null)
66 avrc.AddErrorResult(e.Error.Message);
67 avrc.Complete();
68 };
69
70 // simulating an asynchronous process.
71 worker.RunWorkerAsync(context);
72 }
73 }
74
75 public class Rule2 : BusinessRule
76 {
77 private Rule1 _innerRule;
78
79 public Rule2(Csla.Core.IPropertyInfo primaryProperty)
80 : base(primaryProperty)
81 {
82 IsAsync = true;
83 _innerRule = new Rule1(primaryProperty);
85 }
86
87 protected override void Execute(IRuleContext context)
88 {
89 if (context.Target != null)
90 throw new ArgumentOutOfRangeException("context.Target must be null");
91
92 ((IBusinessRule)_innerRule).Execute(context.GetChainedContext(_innerRule));
93 }
94 }
95
96 public class Rule3 : BusinessRule
97 {
98 private Rule1 _innerRule;
99
100 public Rule3(Csla.Core.IPropertyInfo primaryProperty)
101 : base(primaryProperty)
102 {
103 IsAsync = true;
104 _innerRule = new Rule1(primaryProperty);
105 InputProperties = _innerRule.InputProperties;
107 }
108
109 protected override void Execute(IRuleContext context)
110 {
111 if (context.Target == null)
112 throw new ArgumentOutOfRangeException("context.Target must not be null");
113
114 ((IBusinessRule)_innerRule).Execute(context.GetChainedContext(_innerRule));
115 }
116 }
117
118 [Create]
119 private void Create()
120 {
121 }
122 }
123}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Maintains metadata about a property.
bool ProvideTargetWhenAsync
Gets a value indicating that the Target property should be set even for an async rule (note that usin...
List< Csla.Core.IPropertyInfo > InputProperties
Gets a list of secondary property values to be supplied to the rule when it is executed.
Base class used to create business and validation rules.
Definition: BusinessRule.cs:15
override bool IsAsync
Gets a value indicating whether the rule will run on a background thread.
Definition: BusinessRule.cs:23
Tracks the business rules for a business object.
void AddRule(IBusinessRuleBase rule)
Associates a business rule with the business object.
Context information provided to a business rule when it is invoked.
Definition: RuleContext.cs:51
override void Execute(IRuleContext context)
Business or validation rule implementation.
Definition: HasAsyncRule.cs:44
Rule1(Csla.Core.IPropertyInfo primaryProperty)
Definition: HasAsyncRule.cs:37
override void Execute(IRuleContext context)
Business or validation rule implementation.
Definition: HasAsyncRule.cs:87
Rule2(Csla.Core.IPropertyInfo primaryProperty)
Definition: HasAsyncRule.cs:79
override void Execute(IRuleContext context)
Business or validation rule implementation.
Rule3(Csla.Core.IPropertyInfo primaryProperty)
static PropertyInfo< string > NameProperty
Definition: HasAsyncRule.cs:21
Maintains metadata about a property.
Interface defining a business/validation rule implementation.
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
IRuleContext GetChainedContext(IBusinessRuleBase rule)
Gets a new RuleContext object for a chained rule.