Click here to Skip to main content
15,885,978 members
Articles / Database Development / SQL Server

Ready-to-use Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker

Rate me:
Please Sign up or sign in to vote.
4.84/5 (40 votes)
7 Sep 200613 min read 297.7K   4.6K   210  
This paper demonstrates an extensible mass emailing framework (Smart Mass Email SME). The demo implementation uses cutting edge .NET technologies available today such as C#, .NET 2.0, Microsoft® SQL Server 2005 Service Broker, MS Provider Pattern, Enterprise Library January 2006 etc.
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMassEmail.Entities.Validation
{

   /// <summary>
   /// Maintains the list of validation rules associated with an object
   /// </summary>
   [Serializable()]
   public class ValidationRules
   {
      /// <summary>
      /// List of rules that have not passed validation
      /// </summary>
      private BrokenRulesList _brokenRules;

      /// <summary>
      /// Object associated with this list of rules.
      /// </summary>
      [NonSerialized()]
      private object _target;

      [NonSerialized()]
      private Dictionary<string, List<ValidationRuleInfo>> _rulesList;

      /// <summary>
      /// Creates an instance of the class and associates the target.
      /// </summary>
      /// <param name="businessEntity">Target</param>
      internal ValidationRules(object businessEntity)
      {
         this.Target = businessEntity;
      }

      /// <summary>
      /// Object associated with this list of rules.
      /// </summary>
      internal object Target
      {
         get
         {
            return _target;
         }
         set
         {
            _target = value;
         }
      }

      /// <summary>
      /// List of <see cref="BrokenRule"/> objects
      /// </summary>
      private BrokenRulesList BrokenRulesList
      {
         get
         {
            if (_brokenRules == null)
               _brokenRules = new BrokenRulesList();
            return _brokenRules;
         }
      }

      /// <summary>
      /// Read-only list of validation rules
      /// </summary>
      private Dictionary<string, List<ValidationRuleInfo>> RulesList
      {
         get
         {
            if (_rulesList == null)
               _rulesList = new Dictionary<string, List<ValidationRuleInfo>>();
            return _rulesList;
         }
      }

      #region Adding Rules

      /// <summary>
      /// Returns a list of <see cref="ValidationRuleInfo"/> objects for a specified property.
      /// </summary>
      /// <param name="propertyName">The name of the property to get the rules for.</param>
      /// <returns>A <see cref="List{ValidationRuleInfo}"/> containing all of the rules for the specified property.</returns>
      private List<ValidationRuleInfo> GetPropertyRules(string propertyName)
      {
         List<ValidationRuleInfo> list = null;

         //See if the list of rules exists
         if (RulesList.ContainsKey(propertyName))
            list = RulesList[propertyName];
         if (list == null)
         {
            //No list found - create a new one.
            list = new List<ValidationRuleInfo>();
            RulesList.Add(propertyName, list);
         }
         return list;
      }

      /// <summary>
      /// Adds a rule to the list of validated rules.
      /// </summary>
      /// <remarks>
      /// <para>
      /// A rule is implemented by a method which conforms to the 
      /// method signature defined by the <see cref="ValidationRuleHandler" /> delegate.
      /// </para>
      /// </remarks>
      /// <param name="handler">The method that implements the rule.</param>
      /// <param name="propertyName">
      /// The name of the property on the target object where the rule implementation can retrieve
      /// the value to be validated.
      /// </param>
      public void AddRule(ValidationRuleHandler handler, string propertyName)
      {
         AddRule(handler, new ValidationRuleArgs(propertyName));
      }

      /// <summary>
      /// Adds a rule to the list of validated rules.
      /// </summary>
      /// <remarks>
      /// <para>
      /// A rule is implemented by a method which conforms to the 
      /// method signature defined by the <see cref="ValidationRuleHandler" /> delegate.
      /// </para>
      /// </remarks>
      /// <param name="handler">The method that implements the rule.</param>
      /// <param name="args">
      /// A <see cref="ValidationRuleArgs"/> object specifying the property name and other arguments
      /// passed to the rule method
      /// </param>
      public void AddRule(ValidationRuleHandler handler, ValidationRuleArgs args)
      {
         // get the list of rules for the property
         List<ValidationRuleInfo> list = GetPropertyRules(args.PropertyName);

         // we have the list, add our new rule
         list.Add(new ValidationRuleInfo(_target, handler, args));
      }

      #endregion

      #region Validating Rules

      /// <summary>
      /// Validates a list of rules.
      /// </summary>
      /// <remarks>
      /// This method calls the Invoke method on each rule in the list.  If the rule fails, it 
      /// is added to the <see cref="BrokenRulesList"/>
      /// </remarks>
      /// <param name="ruleList">List of rules to validate.</param>
      private void ValidateRuleList(List<ValidationRuleInfo> ruleList)
      {
         foreach (ValidationRuleInfo rule in ruleList)
         {
            if (rule.Invoke())
               BrokenRulesList.Remove(rule);
            else
               BrokenRulesList.Add(rule);
         }
      }

      /// <summary>
      /// Validates all rules for a property
      /// </summary>
      /// <param name="propertyName">Name of the property to validate.</param>
      public void ValidateRules(string propertyName)
      {
         List<ValidationRuleInfo> list;
         //Get the rules for the property
         if (RulesList.ContainsKey(propertyName))
         {
            list = RulesList[propertyName];
            if (list == null)
               return;

            ValidateRuleList(list);
         }
      }

      /// <summary>
      /// Validate all the rules for all properties.
      /// </summary>
      public void ValidateRules()
      {
         // get the rules for each rule name
         foreach (KeyValuePair<string, List<ValidationRuleInfo>> rulePair in RulesList)
         {
            ValidateRuleList(rulePair.Value);
         }
      }

      #endregion

      #region Validation Status

      /// <summary>
      /// Returns a value indicateing whether the <see cref="Target"/> object is valid.
      /// </summary>
      /// <remarks>If one or more rules are broken, the object is assumed to be invalid and 
      /// false is return.  Otherwise, True is returned.
      /// </remarks>
      /// <returns>A value indicating whether any rules are broken.</returns>
      internal bool IsValid
      {
         get { return BrokenRulesList.Count == 0; }
      }

      /// <summary>
      /// Return a <see cref="BrokenRulesList"/> that contains all of the invalid rules.
      /// </summary>
      public BrokenRulesList GetBrokenRules()
      {
         return BrokenRulesList;
      }

      #endregion
      
      /// <summary>
      /// 	Clear the rules list.
      /// </summary>
			public void Clear()
	   	{
		  	_rulesList.Clear();
	   	}
   }
}

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
Web Developer
Australia Australia
I have been awarded MVP (Visual C#) for year 2007, 2008, 2009. I am a Microsoft Certified Application Developer (C# .Net). I currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and Founder of Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Simplexhub.
My BLOG http://www.geekswithblogs.net/shahed
http://msmvps.com/blogs/shahed/Default.aspx.

Comments and Discussions