Return to Snippet

Revision: 42075
at February 26, 2011 09:58 by marktaichen


Initial Code
public class GreaterThanNullableDate<T> : PropertyValidator
    {
        private Expression<Func<T, DateTime?>> mTarget;
        private Expression<Func<T, T>> mTargetWithCollection;
        private string mCollectionProperty;
        private string mPropertyOfCollection;

        public GreaterThanNullableDate(Expression<Func<T, DateTime?>> expression)
            : base("Property {PropertyName} greater than another date!")
        {
            mTarget = expression;
        }

        public GreaterThanNullableDate(Expression<Func<T, T>> expression, string pCollectionProperty, string pPropertyOfCollection)
            : base("some date is greater than some other date.")
        {//for use with a collection and a property of each object in that collection.
            //example: RuleFor(p => p.Master.IncidentDateTime).GreaterThanNullableDateList( p => p ,"NIBRSArrests", "ArrestDateTime").WithMessage("Arrest Date time must smaller than incident datetime.");
            mTargetWithCollection = expression;
            mCollectionProperty = pCollectionProperty;
            mPropertyOfCollection = pPropertyOfCollection;
        }

        protected override bool IsValid(PropertyValidatorContext context)
        {
            if (mTarget != null)
            {
                Func<T, DateTime?> oFunc = mTarget.Compile();
                //Type oType = mTarget.Parameters[0].Type;
                DateTime? oTargetDateTime = oFunc.Invoke((T)context.Instance);

                DateTime? oSource = context.PropertyValue as DateTime?;

                if (oSource != null && oTargetDateTime != null)
                {
                    if (oSource < oTargetDateTime)
                        return false;
                }
            }
            else if (mTargetWithCollection != null)
            {
                Func<T, T> oFunc = mTargetWithCollection.Compile();
                //Type oType = mTarget.Parameters[0].Type;
                T oTarget = oFunc.Invoke((T)context.Instance);
                PropertyInfo oCollectionPropertyInfo = oTarget.GetType().GetProperty(mCollectionProperty);
                Object oCollectionValue = oCollectionPropertyInfo.GetValue(oTarget, null);

                
                DateTime? oSource = context.PropertyValue as DateTime?;

                foreach (Object oObj in (IEnumerable)oCollectionValue)
                {
                    PropertyInfo oPropertyOfCollectionInfo = oObj.GetType().GetProperty(mPropertyOfCollection);

                    if (oSource != null && oPropertyOfCollectionInfo.GetValue(oObj, null) != null)
                    {
                        if (oSource < (DateTime?)oPropertyOfCollectionInfo.GetValue(oObj, null))
                            return false;
                    }
                }
            }

            return true;
        }
    }
    public static class DefaultValidatorExtensions
    {
        public static IRuleBuilderOptions<T, DateTime?> GreaterThanNullableDate<T>(this IRuleBuilder<T, DateTime?> ruleBuilder, Expression<Func<T, DateTime?>> expression) //Expression<PropertySelector<T, DateTime?>> expression)//Expression<Func<T, DateTime?>> expression) 
        {

            return ruleBuilder.SetValidator(new GreaterThanNullableDate<T>(expression));
        }

        public static IRuleBuilderOptions<T, DateTime?> GreaterThanNullableDateList<T>(this IRuleBuilder<T, DateTime?> ruleBuilder, Expression<Func<T, T>> expression, string pCollectionProperty, string pPropertyOfCollection) //Expression<PropertySelector<T, DateTime?>> expression)//Expression<Func<T, DateTime?>> expression) 
        {//will get all the objects of a collection and for each object check a property.
            return ruleBuilder.SetValidator(new GreaterThanNullableDate<T>(expression, pCollectionProperty, pPropertyOfCollection));
        }
    }


use it like this:
RuleFor(p => p.Master.IncidentDateTime).GreaterThanNullableDateList( p => p ,"NIBRSArrests", "ArrestDateTime").WithMessage("Arrest Date time must smaller than incident datetime.");

Initial URL

                                

Initial Description

                                

Initial Title
additonal fluent validation extension method for working with collection

Initial Tags
validation, extension

Initial Language
C#