public void Validate(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute)
 {
     if (!IsValid(entity, property, attribute))
     {
         entity.Validator.AddFailure(property.Name, attribute);
     }
 }
 public void Validate(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute)
 {
     if (!IsValid(entity, property, attribute))
     {
         entity.Validator.AddFailure(property.Name, attribute);
     }
 }
        public override bool IsValid(IEntity entity, System.Reflection.PropertyInfo property, IValidatePropertyAttribute attribute)
        {
            object value = property.GetValue(entity, null);

            if (property.PropertyType == typeof(string))
                return (string)value != String.Empty;
            else
                return value != null;

            // TODO: Add support for more types such as
            // int, DateTime, Guid, etc.
        }
        public void Test_IsValid_False_String()
        {
            MockRequiredEntity entity = new MockRequiredEntity();

            ValidateRequiredStrategy strategy = new ValidateRequiredStrategy();

            PropertyInfo property = entity.GetType().GetProperty("TestProperty");

            object[] objs = property.GetCustomAttributes(typeof(RequiredAttribute), true);

            IValidatePropertyAttribute attribute = (IValidatePropertyAttribute)objs[0];

            bool isValid = strategy.IsValid(entity, property, attribute);

            Assert.IsFalse(isValid, "Returned true when it should have been false.");
        }
        /// <summary>
        /// Checks whether the specified property of the provided entity is unique.
        /// </summary>
        /// <param name="entity">The entity to validate by checking whether the provided property value is unique.</param>
        /// <param name="property">The property that is to remain unique.</param>
        /// <param name="attribute">The validate property attribute that caused the validation.</param>
        /// <returns>A value to indicate whether the provided value is unique to the provided property.</returns>
        public override bool IsValid(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute)
        {
            bool isTaken = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Validating the provided entity by ensuring the specified property value is unique."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (property == null)
                {
                    throw new ArgumentNullException("property");
                }

                LogWriter.Debug("Property name: " + property.Name);

                LogWriter.Debug("Entity: " + entity.GetType().FullName);

                IRetrieveStrategy strategy = RetrieveStrategy.New(entity.ShortTypeName, false);

                object propertyValue = property.GetValue(entity, null);

                LogWriter.Debug("Property value: " + (propertyValue != null ? propertyValue.ToString() : String.Empty));

                IEntity existingEntity = (IEntity)strategy.Retrieve(property.Name, propertyValue);

                LogWriter.Debug("Existing entity found: " + (existingEntity != null).ToString());

                LogWriter.Debug("Provided entity ID: " + entity.ID.ToString());
                LogWriter.Debug("Existing entity ID: " + (existingEntity == null ? "[null]" : existingEntity.ID.ToString()));

                isTaken = (existingEntity != null && !existingEntity.ID.Equals(entity.ID));

                LogWriter.Debug("Is taken: " + isTaken);
            }

            return(!isTaken);
        }
        /// <summary>
        /// Checks whether the specified property of the provided entity is unique.
        /// </summary>
        /// <param name="entity">The entity to validate by checking whether the provided property value is unique.</param>
        /// <param name="property">The property that is to remain unique.</param>
        /// <param name="attribute">The validate property attribute that caused the validation.</param>
        /// <returns>A value to indicate whether the provided value is unique to the provided property.</returns>
        public override bool IsValid(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute)
        {
            bool isTaken = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Validating the provided entity by ensuring the specified property value is unique."))
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                if (property == null)
                    throw new ArgumentNullException("property");

                LogWriter.Debug("Property name: " + property.Name);

                LogWriter.Debug("Entity: " + entity.GetType().FullName);

                IRetrieveStrategy strategy = RetrieveStrategy.New(entity.ShortTypeName, false);

                object propertyValue = property.GetValue(entity, null);

                LogWriter.Debug("Property value: " + (propertyValue != null ? propertyValue.ToString() : String.Empty));

                IEntity existingEntity = (IEntity)strategy.Retrieve(property.Name, propertyValue);

                LogWriter.Debug("Existing entity found: " + (existingEntity != null).ToString());

                LogWriter.Debug("Provided entity ID: " + entity.ID.ToString());
                LogWriter.Debug("Existing entity ID: " + (existingEntity == null ? "[null]" : existingEntity.ID.ToString()));

                isTaken = (existingEntity != null && !existingEntity.ID.Equals(entity.ID));

                LogWriter.Debug("Is taken: " + isTaken);
            }

            return !isTaken;
        }
        public override bool IsValid(IEntity entity, System.Reflection.PropertyInfo property, IValidatePropertyAttribute attribute)
        {
            object value = property.GetValue(entity, null);

            if (property.PropertyType == typeof(string))
            {
                return((string)value != String.Empty);
            }
            else
            {
                return(value != null);
            }

            // TODO: Add support for more types such as
            // int, DateTime, Guid, etc.
        }
 /// <summary>
 /// Adds the provided property name and attribute type to the list of validation failures.
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="attribute"></param>
 public void AddFailure(string propertyName, IValidatePropertyAttribute attribute, Dictionary<string, IValidatePropertyAttribute> failures)
 {
     failures[propertyName + "_" + attribute.ValidatorName] = attribute;
 }
 /// <summary>
 /// Adds the provided property name and attribute type to the list of validation failures.
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="attribute"></param>
 public void AddFailure(string propertyName, IValidatePropertyAttribute attribute)
 {
     AddFailure(propertyName, attribute, Failures);
 }
 public abstract bool IsValid(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute);
 /// <summary>
 /// Adds the provided property name and attribute type to the list of validation failures.
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="attribute"></param>
 public void AddFailure(string propertyName, IValidatePropertyAttribute attribute, Dictionary <string, IValidatePropertyAttribute> failures)
 {
     failures[propertyName + "_" + attribute.ValidatorName] = attribute;
 }
 /// <summary>
 /// Adds the provided property name and attribute type to the list of validation failures.
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="attribute"></param>
 public void AddFailure(string propertyName, IValidatePropertyAttribute attribute)
 {
     AddFailure(propertyName, attribute, Failures);
 }
 public abstract bool IsValid(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute);