CodeSmith Community
Your Code. Your Way. Faster!

Question about Validation Rules

Latest post 06-24-2008 3:55 PM by njappboy. 4 replies.
  • 01-17-2007 2:13 PM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,686

    Question about Validation Rules

    Hi!

    Suppose you have a web interface (ASP.NET) to enter information about new clients, and that one of the rules you want to implement is that two clients cannot have the same name. Where would you put the validation for this or for similar scenarios? At first, I thought I would simply write a new Validation Rule and add it in my Client.AddValidationRules() method. However, I need to access the database through the Service Layer in order to make sure that the Client Name does not already exist in the DB, and the Validation Rules are in the Entitites namespace, which does not know anything about the database and the Service Layer. Where should such validation be done then? I have many similar cases to implement.

    Cheers!

    JF 

    • Post Points: 45
  • 01-17-2007 7:57 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 726
    • Points 16,910

    Re: Question about Validation Rules

    Reply |Contact |Answer

    velum,
     

    It could be achieved via processors (a logical unit of work) check here for an example http://wiki.nettiers.com/componentlayer

    You would then override any of your methods in the service layer to insert  this process into a business process pipeline like following:

            public override bool Insert(Order entity)
            {
                ProcessorList.Add(new InventoryProcessor(entity));
                Execute();
                return base.Insert(entity);
            }

    velum:

    Hi!

    Suppose you have a web interface (ASP.NET) to enter information about new clients, and that one of the rules you want to implement is that two clients cannot have the same name. Where would you put the validation for this or for similar scenarios? At first, I thought I would simply write a new Validation Rule and add it in my Client.AddValidationRules() method. However, I need to access the database through the Service Layer in order to make sure that the Client Name does not already exist in the DB, and the Validation Rules are in the Entitites namespace, which does not know anything about the database and the Service Layer. Where should such validation be done then? I have many similar cases to implement.

    Cheers!

    JF 

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 75
  • 01-17-2007 11:24 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,686

    Re: Question about Validation Rules

    Thanks Mike!

    This looks like exactly what I need. It looks simple, but somehow all these things about processors and validators was a bit blurry in my mind.

    Cheers! 

    JF


     

    • Post Points: 35
  • 01-18-2007 8:59 AM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 726
    • Points 16,910

    Re: Question about Validation Rules

    Hey velum,

    Yeah, it may sound easy, and it is actually easy once things are clear Big Smile

    Here is another example, of the class that resides in the Service Layer called VerifyCountryProcessor.cs. Its' responsibility to create a broken rule and add it to the BrokenRulesList of the process result if the rule is invalid

    using System;
    using System.Collections.Generic;
    using System.Text;

    using NetTiers.Data;
    using NetTiers.Entities;
    using NetTiers.Services;

    namespace NetTiers.Services.Pipline
    {
        /// <summary>
        /// Processor to verify Country Name if exists in Database
        /// </summary>
        public class VerifyCountryProcessor : ProcessorBase
        {
            private Entities.Country _country;
            private GenericProcessorResult genericProcessorResult;

            /// <summary>
            /// Country Processor
            /// </summary>
            /// <param name="country"></param>
            public VerifyCountryProcessor(Entities.Country country)
            {
                this._country = country;
            }

            /// <summary>
            /// Process the IProcessResult
            /// </summary>
            /// <returns></returns>
            public override IProcessorResult Process()
            {
                _country.AddValidationRuleHandler(ValidateIfCountryExists, "CountryExists");

                //check country
                _country.Validate();

                if (!_country.IsValid)
                    ProcessResult.AddBrokenRulesList(typeof(Entities.Country), _country.BrokenRulesList);

                return ProcessResult;
            }
           
            private bool ValidateIfCountryExists(object target, Entities.Validation.ValidationRuleArgs e)
            {

                CountryService CountryService = new CountryService();

                string Where = string.Format("CountryName = '{0}'", _country.CountryName);
                TList<Country> CountryList = CountryService.Find(Where);

                e.Description = string.Format("The {0} already exists and cannot be added or updated.", _country.CountryName);
               
                if (CountryList.Count > 0 && _country.CountryId == CountryList[0].CountryId)
                {
                    return true;
                }
                else if (CountryList.Count > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
               
            }

            /// <summary>
            /// ProcessResult
            /// </summary>
            public override IProcessorResult ProcessResult
            {
                get
                {
                    if (genericProcessorResult == null)
                    {
                        genericProcessorResult = new GenericProcessorResult();

                    }
                    return genericProcessorResult;
                }
            }
        }
    }

     

    velum:

    Thanks Mike!

    This looks like exactly what I need. It looks simple, but somehow all these things about processors and validators was a bit blurry in my mind.

    Cheers! 

    JF


     

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 5
  • 06-24-2008 3:55 PM In reply to

    • njappboy
    • Top 500 Contributor
    • Joined on 06-11-2008
    • Posts 14
    • Points 220

    Re: Question about Validation Rules

     public override bool Insert(Order entity)
            {
                ProcessorList.Add(new InventoryProcessor(entity));
                Execute();
                return base.Insert(entity);
            }

     

    This code pointless if you actually care about the ServiceResult returned by the InventoryProcessor because your entity will still be saved unless the InventoryProcessor method throws an error?

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems