CSLA (>= v4.0) QuickRule Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 1. This sample creates and adds a lambda rule (LogChangeRule) | |
// for the primary property PostCode | |
BusinessRules.AddRule( new QuickRule( LogChangeRule ).For( PostCodeProperty ) ); | |
// Example 2. This sample creates and adds a lambda rule (CheckIfExistsRule) | |
// for the primary property PostCode, and uses it as an input property | |
BusinessRules.AddRule( new QuickRule( CheckIfExistsRule ).For( PostCodeProperty ).UsingIt() ); | |
// Example 3. This sample creates and adds a lambda rule (MutatePostCodeToUpperCase) | |
// for the primary property PostCode (using the PostCode as an "input property") | |
// and affects the FormattedAddress property as a consequence ("affected properties") | |
BusinessRules.AddRule( | |
new QuickRule( MutatePostCodeToUpperCase ) | |
.For( PostCodeProperty ).UsingIt() | |
.Affects( FormattedAddressProperty ) ); | |
// Example 4. This sample creates and adds a Lambda rule (MutateTotals) for the primary | |
// property ProductCode and uses SellingPrice and Quantity properties as input properties. | |
// The LineTotal and Tax properties are affected by the rule. | |
BusinessRules.AddRule( | |
new QuickRule( MutateTotals ) | |
.For( ProductCodeProperty ) | |
.Using( SellingPriceProperty, QuantityProperty ) | |
.Affects( LineTotalProperty, TaxProperty ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// A quick and basic business rule wrapping a lambda expression, and optional input and affected properties using a fluent-like API. | |
/// </summary> | |
public class QuickRule : BusinessRule | |
{ | |
private string _ruleUriQuery; | |
private Action<RuleContext> Rule { get; set; } | |
/// <summary> | |
/// Initializes a new instance of the <see cref="QuickRule"/>. | |
/// </summary> | |
/// <param name="rule">The rule.</param> | |
public QuickRule( Action<RuleContext> rule ) | |
{ | |
Initialize( rule ); | |
} | |
/// <summary> | |
/// Define the primary property that applies to this rule. | |
/// </summary> | |
/// <param name="primaryProperty">Primary property for the rule.</param> | |
/// <returns>The rule with updated primary properties</returns> | |
public QuickRule For( Csla.Core.IPropertyInfo primaryProperty ) | |
{ | |
PrimaryProperty = primaryProperty; | |
RuleUri.AddQueryParameter( "lambda", _ruleUriQuery ); | |
return this; | |
} | |
/// <summary> | |
/// The rule uses the specified input properties. | |
/// </summary> | |
/// <param name="inputProperties">The primary and input property.</param> | |
/// <returns>The rule with updated input properties</returns> | |
public QuickRule Using( params Csla.Core.IPropertyInfo[] inputProperties ) | |
{ | |
InputProperties.AddRange( inputProperties ); | |
return this; | |
} | |
/// <summary> | |
/// The rule uses the defined primary property as an input property. | |
/// </summary> | |
/// <returns> | |
/// The rule with updated input property. | |
/// </returns> | |
public QuickRule UsingIt() | |
{ | |
InputProperties.Add( PrimaryProperty ); | |
return this; | |
} | |
/// <summary> | |
/// The rule affects the specified properties. | |
/// </summary> | |
/// <param name="affectedProperties">The affected properties.</param> | |
/// <returns></returns> | |
public QuickRule Affects( params Csla.Core.IPropertyInfo[] affectedProperties ) | |
{ | |
AffectedProperties.AddRange( affectedProperties ); | |
return this; | |
} | |
/// <summary> | |
/// Run as an asynchronous rule. | |
/// </summary> | |
/// <param name="provideTargetWhenAsync">if set to <c>true</c> provide target when asynchronous. Bad things will happen if you enable this.</param> | |
/// <returns></returns> | |
public QuickRule AsAsync( bool provideTargetWhenAsync = false ) | |
{ | |
IsAsync = true; | |
ProvideTargetWhenAsync = provideTargetWhenAsync; | |
return this; | |
} | |
/// <summary> | |
/// Set the rule run modes. | |
/// </summary> | |
/// <param name="runModes">The run modes.</param> | |
/// <returns></returns> | |
public QuickRule RunModeAs( RunModes runModes ) | |
{ | |
RunMode = runModes; | |
return this; | |
} | |
/// <summary> | |
/// Initializes this instance with the specified rule. | |
/// </summary> | |
/// <param name="rule">The rule.</param> | |
private void Initialize( Action<RuleContext> rule ) | |
{ | |
Rule = rule; | |
_ruleUriQuery = Guid.NewGuid().ToString(); | |
} | |
/// <summary> | |
/// Executes the rule. | |
/// </summary> | |
/// <param name="context">Rule context.</param> | |
protected override void Execute( RuleContext context ) | |
{ | |
Rule( context ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated it to ensure unique rule URI for multiple uses of "QuickRule" against the same property.
Also, setting the PrimaryProperty - via For(...) method - clears the RuleUri Query Parameter, so changed the flow to save the query parameter until the primary property is set.