Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

Nested Property Binding

Rate me:
Please Sign up or sign in to vote.
4.99/5 (41 votes)
12 Sep 2007CPOL7 min read 182.2K   3K   95  
Extending the BindingSource component to support nested property binding
using System;
using System.Collections.Generic;
using System.Core;
using System.Core.Emit;
using System.Reflection;
using System.Text;

namespace System.Core.Validation
{
    /// <summary>
    /// Base class for implementing validators.
    /// </summary>
    public abstract class ValidatorBase
    {
        private string _errorMessage;
        private Type _propertyType;
        private string _propertyName;        
                        

        protected ValidatorBase(Type propertyType, string propertyName, string errorMessage)
        {
            if (propertyType == null)
                throw new ArgumentNullException("propertyType");

            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentOutOfRangeException("propertyName");

            if (string.IsNullOrEmpty(errorMessage))
                _errorMessage = string.Format(DefaultErrorMessages.Default, propertyName);
            else
                _errorMessage = errorMessage;

            _propertyType = propertyType;
            _propertyName = propertyName;
            _errorMessage = errorMessage;        
        }

        internal Type PropertyType
        {
            get { return _propertyType; }
        }

        internal string PropertyName
        {
            get { return _propertyName; }
        }

        


        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        /// <summary>
        /// Performs the validation
        /// </summary>
        /// <param name="value">The value to be validated</param>
        /// <returns></returns>
        protected abstract bool Validate(object value);


        /// <summary>
        /// Gets the valid property types.
        /// </summary>
        /// <value>The valid property types.</value>    
        protected abstract Type[] ValidPropertyTypes { get; }


        /// <summary>
        /// Determines whether the specified instance is valid.
        /// </summary>
        /// <param name="instance">The instance declaring the property to be validated</param>
        /// <returns>
        /// 	<c>true</c> if the specified instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(object instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");
            IDynamicAccessor DynamicAccessor = DynamicAccessorFactory.GetDynamicAccessor(instance.GetType());

            object value = DynamicAccessor.GetPropertyValue(instance, _propertyName);
            return Validate(value);
        }

    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Norway Norway
I'm a 39 year old software developer living in Norway.
I'm currently working for a software company making software for the retail industry.

Comments and Discussions