Click here to Skip to main content
15,892,480 members
Articles / Web Development / ASP.NET

Delegates and Business Objects

Rate me:
Please Sign up or sign in to vote.
4.90/5 (100 votes)
21 May 20069 min read 280.6K   2.5K   268  
An approach to implementing validation on custom business rules, using delegates.
using System;
using System.Collections.Generic;
using System.Text;

namespace DelegateBusinessObjects {
    /// <summary>
    /// A simple type of domain object rule that uses a delegate for validation. 
    /// </summary>
    /// <returns>True if the rule has been followed, or false if it has been broken.</returns>
    /// <remarks>
    /// Usage:
    /// <code>
    ///     this.Rules.Add(new SimpleRule("Name", "The customer name must be at least 5 letters long.", delegate { return this.Name &gt; 5; } ));
    /// </code>
    /// </remarks>
    public delegate bool SimpleRuleDelegate();

    /// <summary>
    /// A class to define a simple rule, using a delegate for validation.
    /// </summary>
    public class SimpleRule : 
        Rule {
        private SimpleRuleDelegate _ruleDelegate;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="propertyName">The name of the property this rule validates for. This may be blank.</param>
        /// <param name="brokenDescription">A description message to show if the rule has been broken.</param>
        /// <param name="ruleDelegate">A delegate that takes no parameters and returns a boolean value, used to validate the rule.</param>
        public SimpleRule(string propertyName, string brokenDescription, SimpleRuleDelegate ruleDelegate):
            base(propertyName, brokenDescription) {
            this.RuleDelegate = ruleDelegate;
        }

        /// <summary>
        /// Gets or sets the delegate used to validate this rule.
        /// </summary>
        protected virtual SimpleRuleDelegate RuleDelegate {
            get { return _ruleDelegate; }
            set { _ruleDelegate = value; }
        }

        /// <summary>
        /// Validates that the rule has not been broken.
        /// </summary>
        /// <param name="domainObject">The domain object being validated.</param>
        /// <returns>True if the rule has not been broken, or false if it has.</returns>
        public override bool ValidateRule(DomainObject domainObject) {
            return RuleDelegate();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Octopus Deploy
Australia Australia
My name is Paul Stovell. I live in Brisbane and develop an automated release management product, Octopus Deploy. Prior to working on Octopus I worked for an investment bank in London, and for Readify. I also work on a few open source projects. I am a Microsoft MVP for Client Application Development.

Comments and Discussions