Exemple #1
0
        /// <summary>
        /// calculate all validations
        /// </summary>
        /// <returns>return list of validation errors</returns>
        public IEnumerable <BaseValidationRuleInfoAttribute> CalculateValidationsOfTask(Action <string, object> changeParameterValueAction, Action <BaseValidationRuleInfoAttribute> fillValidationParameterAction)
        {
            if (!Task.CurrentId.HasValue)
            {
                throw new Exception("cannot calculate rules without any task!");
            }
            int currentId = Task.CurrentId.GetValueOrDefault();

            if (CurrentValidationRules.TryGetValue(currentId, out List <BaseValidationRuleInfoAttribute> validationRuleInfoAttributes))
            {
                foreach (BaseValidationRuleInfoAttribute validation in validationRuleInfoAttributes)
                {
                    fillValidationParameterAction(validation);
                    if (validation.TaskType == ValidationRuleInfoTaskType.Error)
                    {
                        if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                        {
                            yield return(validation);
                        }
                    }
                    else if (validation.TaskType == ValidationRuleInfoTaskType.ChangeValue)
                    {
                        if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                        {
                            object changedValue = BaseValidationRuleInfoAttribute.GetChangedValue(validation);
                            if (validation.PropertyInfo != null)
                            {
                                System.Reflection.PropertyInfo findProperty = validation.Object.GetType().GetPropertyInfo(validation.PropertyInfo.Name);
                                findProperty.SetValue(validation.Object, changedValue, null);
                            }
                            else
                            {
                                changeParameterValueAction(validation.ParameterInfo.Name, changedValue);
                            }
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            if (CurrentDetectedObjects.TryGetValue(currentId, out ConcurrentDictionary <Type, ConcurrentDictionary <object, List <string> > > types))
            {
                foreach (KeyValuePair <Type, ConcurrentDictionary <object, List <string> > > tkv in types)
                {
                    Type type = tkv.Key;
                    if (types.TryGetValue(type, out ConcurrentDictionary <object, List <string> > objects))
                    {
                        foreach (KeyValuePair <object, List <string> > okv in objects)
                        {
                            if (objects.TryGetValue(okv.Key, out List <string> properties))
                            {
                                foreach (BaseValidationRuleInfoAttribute item in CalculateArrays(okv.Key, properties, currentId))
                                {
                                    yield return(item);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// remove tasks from memory
 /// </summary>
 /// <param name="TaskId"></param>
 public void RemoveTask(int TaskId)
 {
     CurrentDetectedObjects.Remove(TaskId);
     CurrentValidationRules.Remove(TaskId);
 }
Exemple #3
0
        /// <summary>
        /// add object properties as check in current task
        /// </summary>
        /// <param name="currentTaskId"></param>
        /// <param name="type"></param>
        /// <param name="instance"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyInfo"></param>
        public void AddObjectPropertyAsChecked(int?currentTaskId, Type type, object instance, string propertyName, PropertyInfo propertyInfo, object currentValue)
        {
            if (!currentTaskId.HasValue || instance == null)
            {
                return;
            }
            int currentId = currentTaskId.GetValueOrDefault();

            if (CurrentDetectedObjects.TryGetValue(currentId, out ConcurrentDictionary <Type, ConcurrentDictionary <object, List <string> > > types))
            {
                if (types.TryGetValue(type, out ConcurrentDictionary <object, List <string> > objects))
                {
                    if (objects.TryGetValue(instance, out List <string> properties))
                    {
                        if (!properties.Contains(propertyName) && !string.IsNullOrEmpty(propertyName))
                        {
                            properties.Add(propertyName);
                        }
                    }
                    else
                    {
                        properties = new List <string>();
                        if (!string.IsNullOrEmpty(propertyName))
                        {
                            properties.Add(propertyName);
                        }
                        objects.TryAdd(instance, properties);
                    }
                }
                else
                {
                    objects = new ConcurrentDictionary <object, List <string> >();
                    List <string> properties = new List <string>();
                    if (!string.IsNullOrEmpty(propertyName))
                    {
                        properties.Add(propertyName);
                    }
                    objects.TryAdd(instance, properties);
                    types.TryAdd(type, objects);
                }
            }
            else
            {
                types = new ConcurrentDictionary <Type, ConcurrentDictionary <object, List <string> > >();
                ConcurrentDictionary <object, List <string> > objects = new ConcurrentDictionary <object, List <string> >();
                List <string> properties = new List <string>();
                if (!string.IsNullOrEmpty(propertyName))
                {
                    properties.Add(propertyName);
                }
                objects.TryAdd(instance, properties);
                types.TryAdd(type, objects);
                CurrentDetectedObjects.TryAdd(currentId, types);
            }

            if (propertyName != null && FluentValidationRules.TryGetValue(type, out Dictionary <string, List <object> > validations))
            {
                if (validations.TryGetValue(propertyName, out List <object> attributes))
                {
                    foreach (object attribute in attributes)
                    {
                        BaseValidationRuleInfoAttribute attributeInstance = null;
                        if (attribute is Type attributeType)
                        {
                            try
                            {
                                attributeInstance = (BaseValidationRuleInfoAttribute)Activator.CreateInstance(attributeType);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception($"I went to create instance of your attribute by type {attributeType.FullName} but it had Exception, are you made constructor for that? see the inner exception for more details", ex);
                            }
                        }
                        else
                        {
                            attributeInstance = (BaseValidationRuleInfoAttribute)attribute;
                        }

                        attributeInstance.PropertyInfo = propertyInfo;
                        attributeInstance.Object       = instance;
                        attributeInstance.CurrentValue = currentValue;
                        AddRule(currentTaskId, attributeInstance);
                    }
                }
            }
        }