Beispiel #1
0
        public bool Evaluate(DataObject dataObject)
        {
            List <string> additionalFields = Expressions.SelectMany(exp => exp.GetAdditionalFieldsForDataObject()).ToList();

            Dictionary <string, object> parameters = new Dictionary <string, object>();

            SchemaObject schemaObject            = Schema.Schema.GetSchemaObject(dataObject.GetType());
            DataObject   dataObjectForValidation = DataObject.GetEditableByPrimaryKey(dataObject.GetType(), schemaObject.PrimaryKeyField.GetValue(dataObject) as long?, null, additionalFields);

            dataObject.Copy(dataObjectForValidation);

            parameters[schemaObject.ObjectName] = dataObjectForValidation;

            return(Evaluate(parameters));
        }
Beispiel #2
0
 public override void CopyObject(DataObject dataObject)
 {
     // Do repository specific work
     dataObject.Copy();
 }
Beispiel #3
0
        public static bool Validate(DataObject dataObject, SaveModes saveMode, List <Guid> saveFlags = null)
        {
            if (validationDefinitionsBySchemaObject == null)
            {
                InitializeRules();
            }

            SchemaObject thisSchemaObject = Schema.Schema.GetSchemaObject(dataObject.GetType());
            List <IValidationDefinition> validationDefinitionsForObject = validationDefinitionsBySchemaObject.GetOrDefault(thisSchemaObject);

            if (validationDefinitionsBySchemaObject == null)
            {
                return(true);
            }

            Func <ValidationRule, bool> validRulesDelegate = vr => saveFlags == null ? true : !saveFlags.Contains(vr.ID);

            DataObject           objectToValidate = dataObject;
            IEnumerable <string> extraFields      = validationDefinitionsForObject.SelectMany(def => def.ValidationRules.Where(validRulesDelegate).SelectMany(vr => vr.Condition.AdditionalDataObjectFields));

            if (extraFields.Any())
            {
                objectToValidate = DataObject.GetEditableByPrimaryKey(dataObject.GetType(), objectToValidate.PrimaryKeyField.GetValue(objectToValidate) as long?);
                if (objectToValidate == null)
                {
                    objectToValidate = dataObject;
                }
                else
                {
                    dataObject.Copy(objectToValidate);
                }
            }

            bool success = true;

            foreach (ValidationRule validationRule in validationDefinitionsForObject.SelectMany(vd => vd.ValidationRules.Where(vr =>
            {
                switch (saveMode)
                {
                case SaveModes.Delete:
                    return(vr.ApplyOnDelete);

                case SaveModes.Insert:
                    return(vr.ApplyOnInsert);

                case SaveModes.Update:
                    return(vr.ApplyOnUpdate);
                }

                return(false);
            })))
            {
                bool result = validationRule.Condition.Evaluate(objectToValidate);
                if (!result)
                {
                    dataObject.Errors.Add(validationRule.Field, validationRule.Message);
                }

                success = result && success;
            }

            return(success);
        }