Defining Business Rules

The following rules are considered to be the minimum rules needed to ensure database integrity for Invoicing Sample:

There are generally two types of Common Business (Validation) Rules

  1. Without Constructors (i.e. Required)
  2. With Constructors (MinValue, MaxValue, MaxLength

Custom Business Rules

Customer business rules are defined as a class which inherit from BusinessRule

Business Rules General

Business Rule(s) are defined within each value property

Choose the class in which to define the rule and select the 'Value Properties collection'
BrandValueProperties.png

Business Rule without Constructor

1 - Select the 'BrandName' property In the 'Value Property Collection Editor' (see 'Business Rules General')
2 - Select the '02. Advanced: Business Rules Collection'
BusinessRulesCollection.png

3 - Select 'Add' then input:
  • 01 Assembly File Name - Full path to Csla.dll
  • 01 Rule Type Name - Required
BrandNameRequired.png

Business Rule with Constructor

1 - Select the 'Brand' object rom the Csla Objects pane
2 - Select 'Value Properties' from 02. Business Properties Csla Object Info pane
3 - Select BrandName'
4 - Select 02. Advanced - Business Rules Collection
5 - Select 'Add' button
  • 01 Assembly File Name - select Csla.dll file
  • 01 Rule Type Name - MaxLength
  • 02 Business Rules Constructor: Select Ellipse
BrandNameMaxLength.png
  • 03 Parameter Value: max: 50
BrandNameRuleConstructor.png

Custom Business Rules

Custom business rule are defined in a classes which inherits from BusinessRule

The following business rule will calculate extended amount for individual line items and would be called as follows:

BusinessRules.AddRule(new BusinessRules.CalcExtendedAmt(QuantityProperty));

Code listing

using System;
using System.Collections.Generic;
using System.Linq;
using Csla.Core;
using Csla.Rules;

namespace SampleInvoice.Library.BusinessRules
{
    /// <summary>
    /// CalcSum rule will set primary property to the product of both.
    /// </summary>
    public class CalcExtendedAmt : BusinessRule
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CalcExtendedAmt"/> class.
        /// </summary>
        /// <param name="primaryProperty">The primary property.</param>
        /// <param name="inputProperties">The input properties.</param>
        public CalcExtendedAmt(IPropertyInfo primaryProperty)
            : base(primaryProperty)
        { }

        protected override void Execute(RuleContext context)
        {
            var invoiceLineItem = (InvoiceLineEditListItem)context.Target;

            invoiceLineItem.ExtendedAmount = Convert.ToDecimal(invoiceLineItem.Quantity * invoiceLineItem.Price);
        }
    }
}

Last edited Oct 24, 2011 at 12:41 PM by RumbleCow, version 9

Comments

No comments yet.