/// <summary> /// Enumerates over the set of instances represented by the current step. /// </summary> /// <param name="instance"></param> /// <returns></returns> public IEnumerable<ModelInstance> GetInstances(ModelInstance instance) { // Stop loading if the step is null or represents a value if (Property is ModelValueProperty || !((ModelReferenceProperty)Property).DeclaringType.IsInstanceOfType(instance)) yield break; // Cast the property to the correct type var reference = (ModelReferenceProperty)Property; // Return each instance exposed by a list property if (reference.IsList) { ModelInstanceList children = instance.GetList(reference); if (children != null) { foreach (ModelInstance child in instance.GetList(reference)) { if (Filter == null || Filter.IsInstanceOfType(child)) yield return child; } } } // Return the instance exposed by a reference property else { ModelInstance child = instance.GetReference(reference); if (child != null && (Filter == null || Filter.IsInstanceOfType(child))) yield return child; } }
/// <summary> /// Enumerates over the set of instances represented by the current step. /// </summary> /// <param name="instance"></param> /// <returns></returns> public IEnumerable<ModelInstance> GetInstances(ModelInstance instance) { // Exit immediately if the property is not valid for the specified instance if (!DeclaringType.IsInstanceOfType(instance)) throw new ArgumentException("The current property is not valid for the specified instance."); // Return each instance exposed by a list property if (IsList) { ModelInstanceList children = instance.GetList(this); if (children != null) { foreach (ModelInstance child in children) yield return child; } } // Return the instance exposed by a reference property else { ModelInstance child = instance.GetReference(this); if (child != null) yield return child; } }
/// <summary> /// Enumerates over the set of instances represented by the current step. /// </summary> /// <param name="instance"></param> /// <returns></returns> public IEnumerable <ModelInstance> GetInstances(ModelInstance instance) { // Exit immediately if the property is not valid for the specified instance if (!DeclaringType.IsInstanceOfType(instance)) { throw new ArgumentException("The current property is not valid for the specified instance."); } // Return each instance exposed by a list property if (IsList) { ModelInstanceList children = instance.GetList(this); if (children != null) { foreach (ModelInstance child in children) { yield return(child); } } } // Return the instance exposed by a reference property else { ModelInstance child = instance.GetReference(this); if (child != null) { yield return(child); } } }
protected override bool ConditionApplies(ModelInstance root) { // Get the allowed values var allowedValues = GetAllowedValues(root); // Always consider as valid when there are no allowed values if (allowedValues == null) { return(false); } // Value properties if (Property is ModelValueProperty) { // List if (Property.IsList) { // Get the current property value var values = root.GetValue((ModelValueProperty)Property) as IEnumerable; // Determine whether the property value is in the list of allowed values return(!(values == null || values.Cast <object>().All(value => allowedValues.Contains(value)))); } // Value else { // Get the current property value var value = root.GetValue((ModelValueProperty)Property); // Determine whether the property value is in the list of allowed values return(!(value == null || allowedValues.Contains(value))); } } // Reference properties else { // List Property if (Property.IsList) { // Get the current property value ModelInstanceList values = root.GetList((ModelReferenceProperty)Property); // Determine whether the property value is in the list of allowed values return(!(values == null || values.All(value => allowedValues.Contains(value)))); } // Reference Property else { // Get the current property value ModelInstance value = root.GetReference((ModelReferenceProperty)Property); // Determine whether the property value is in the list of allowed values return(!(value == null || allowedValues.Contains(value))); } } }
/// <summary> /// Gets the formatted value of the property for the specified instance. /// </summary> protected internal override string GetFormattedValue(ModelInstance instance, string format, IFormatProvider provider) { if (IsList) return instance.GetList(this).ToString(format ?? Format, provider); else { var reference = instance.GetReference(this); return reference != null ? reference.ToString(format ?? Format, provider) : ""; } }
public IModelPropertySource GetSource(ModelInstance root, Func <ModelInstance, ModelReferenceProperty, int, bool> whenNull, Action <ModelInstance, ModelReferenceProperty, int, ModelInstance> whenNotNull) { // Return the source type for static paths if (IsStatic) { return(ModelContext.Current.GetModelType(SourceType)); } //walk the path performing whenNull if an entity is null //if an entity is null and whenNull returns false //then exit out of SetValue returning false foreach (SourceStep step in this.steps.Take(this.steps.Length - 1)) { ModelReferenceProperty stepProp = (ModelReferenceProperty)step.DeclaringType.Properties[step.Property]; if (stepProp.IsList) { ModelInstanceList list = root.GetList(stepProp); if (list.Count < step.Index + 1 && (whenNull == null || !whenNull(root, stepProp, step.Index))) { return(null); } var item = list[step.Index]; if (whenNotNull != null) { whenNotNull(root, stepProp, step.Index, item); } root = item; } else { if (root.GetReference(stepProp) == null && (whenNull == null || !whenNull(root, stepProp, step.Index))) { return(null); } else { //advance to the next step in the chain. var child = root.GetReference(step.Property); if (whenNotNull != null) { whenNotNull(root, stepProp, step.Index, child); } root = child; } } } return(root); }
/// <summary> /// Gets the formatted value of the property for the specified instance. /// </summary> protected internal override string GetFormattedValue(ModelInstance instance, string format, IFormatProvider provider) { if (IsList) { return(instance.GetList(this).ToString(format ?? Format, provider)); } else { var reference = instance.GetReference(this); return(reference != null?reference.ToString(format ?? Format, provider) : ""); } }
/// <summary> /// Determines whether the rule should attach its condition to the given <see cref="ModelInstance"/>. /// </summary> /// <param name="root">The model instance to evaluate the rule for.</param> /// <returns>A boolean value indicating whether the state of the given <see cref="ModelInstance"/> violates the rule.</returns> protected override bool ConditionApplies(ModelInstance root) { if (RequiredValue != null) { return(root[Property] == null || !root[Property].Equals(RequiredValue)); } else { return (root[Property] == null || (Property is ModelReferenceProperty && Property.IsList && root.GetList((ModelReferenceProperty)Property).Count == 0)); } }
/// <summary> /// Enumerates over the set of instances represented by the current step. /// </summary> /// <param name="instance"></param> /// <returns></returns> public IEnumerable <ModelInstance> GetInstances(ModelInstance instance) { // Stop loading if the step is null or represents a value if (Property is ModelValueProperty || !((ModelReferenceProperty)Property).DeclaringType.IsInstanceOfType(instance)) { yield break; } // Cast the property to the correct type var reference = (ModelReferenceProperty)Property; // Return each instance exposed by a list property if (reference.IsList) { ModelInstanceList children = instance.GetList(reference); if (children != null) { foreach (ModelInstance child in instance.GetList(reference)) { if (Filter == null || Filter.IsInstanceOfType(child)) { yield return(child); } } } } // Return the instance exposed by a reference property else { ModelInstance child = instance.GetReference(reference); if (child != null && (Filter == null || Filter.IsInstanceOfType(child))) { yield return(child); } } }
/// <summary> /// Ensure the destination instance path is valid when nulls are encountered along the path. /// </summary> /// <param name="instance"></param> /// <param name="property"></param> /// <param name="index"></param> bool EnsureDestinationInstance(ModelInstance instance, ModelReferenceProperty property, int index) { if (property.IsList) { ModelInstanceList list = instance.GetList(property); for (int i = list.Count; i <= index; i++) { list.Add(property.PropertyType.Create()); } } else { instance.SetReference(property, property.PropertyType.Create()); } return(true); }
protected override bool ConditionApplies(ModelInstance root) { int value = root.GetList((ModelReferenceProperty)Property).Count; if (Minimum > 0 && Maximum > 0) { return(Minimum.CompareTo(value) > 0 && Maximum.CompareTo(value) < 0); } else if (Minimum > 0) { return(Minimum.CompareTo(value) > 0); } else if (Maximum > 0) { return(Maximum.CompareTo(value) < 0); } else { return(false); } }
/// <summary> /// Ensure the destination instance path is valid when nulls are encountered along the path. /// </summary> /// <param name="instance"></param> /// <param name="property"></param> /// <param name="index"></param> bool EnsureDestinationInstance(ModelInstance instance, ModelReferenceProperty property, int index) { if (property.IsList) { ModelInstanceList list = instance.GetList(property); for (int i = list.Count; i <= index; i++) list.Add(property.PropertyType.Create()); } else instance.SetReference(property, property.PropertyType.Create()); return true; }
protected override bool ConditionApplies(ModelInstance root) { int value = root.GetList((ModelReferenceProperty)Property).Count; if (Minimum > 0 && Maximum > 0) return Minimum.CompareTo(value) > 0 && Maximum.CompareTo(value) < 0; else if (Minimum > 0) return Minimum.CompareTo(value) > 0; else if (Maximum > 0) return Maximum.CompareTo(value) < 0; else return false; }
public IModelPropertySource GetSource(ModelInstance root, Func<ModelInstance, ModelReferenceProperty, int, bool> whenNull) { // Return the source type for static paths if (IsStatic) return ModelContext.Current.GetModelType(SourceType); //walk the path performing whenNull if an entity is null //if an entity is null and whenNull returns false //then exit out of SetValue returning false foreach (SourceStep step in this.steps.Take(this.steps.Length - 1)) { ModelReferenceProperty stepProp = (ModelReferenceProperty)ModelContext.Current.GetModelType(step.DeclaringType).Properties[step.Property]; if (stepProp.IsList) { ModelInstanceList list = root.GetList(stepProp); if (list.Count < step.Index + 1 && !whenNull(root, stepProp, step.Index)) return null; root = list[step.Index]; } else { if (root.GetReference(stepProp) == null && !whenNull(root, stepProp, step.Index)) return null; else { //advance to the next step in the chain. root = root.GetReference(step.Property); } } } return root; }
/// <summary> /// Determines whether the rule should attach its condition to the given <see cref="ModelInstance"/>. /// </summary> /// <param name="root">The model instance to evaluate the rule for.</param> /// <returns>A boolean value indicating whether the state of the given <see cref="ModelInstance"/> violates the rule.</returns> protected override bool ConditionApplies(ModelInstance root) { if (RequiredValue != null) return root[Property] == null || !root[Property].Equals(RequiredValue); else return root[Property] == null || (Property is ModelReferenceProperty && Property.IsList && root.GetList((ModelReferenceProperty)Property).Count == 0); }
protected override bool ConditionApplies(ModelInstance root) { var value = root[Property]; // Exit immediately if the target property has a value if (RequiredValue == null && (value != null && (!(Property is ModelReferenceProperty) || !Property.IsList || root.GetList((ModelReferenceProperty)Property).Count > 0))) { return(false); } // If the required value is specified then the value must equal the required value var requiredValueCondition = RequiredValue == null || !RequiredValue.Equals(value); // Invoke the ModelExpression if it exists if (RequiredExpression != null) { try { return((bool)RequiredExpression.Invoke(root) && requiredValueCondition); } catch { return(false); } } // If the value to compare is null, then evaluate whether the compare source has a value else if (CompareValue == null) { return((CompareOperator == CompareOperator.Equal ? !compareSource.HasValue(root) : compareSource.HasValue(root)) && requiredValueCondition); } // Otherwise, perform a comparison of the compare source relative to the compare value bool?result = CompareRule.Compare(compareSource.GetValue(root), CompareOperator, CompareValue); return(result.HasValue && result.Value && requiredValueCondition); }
protected override bool ConditionApplies(ModelInstance root) { var value = root[Property]; // Exit immediately if the target property has a value if (RequiredValue == null && (value != null && (!(Property is ModelReferenceProperty) || !Property.IsList || root.GetList((ModelReferenceProperty)Property).Count > 0))) return false; // If the required value is specified then the value must equal the required value var requiredValueCondition = RequiredValue == null || !RequiredValue.Equals(value); // Invoke the ModelExpression if it exists if (RequiredExpression != null) { try { return (bool)RequiredExpression.Invoke(root) && requiredValueCondition; } catch { return false; } } // If the value to compare is null, then evaluate whether the compare source has a value else if (CompareValue == null) return (CompareOperator == CompareOperator.Equal ? !compareSource.HasValue(root) : compareSource.HasValue(root)) && requiredValueCondition; // Otherwise, perform a comparison of the compare source relative to the compare value bool? result = CompareRule.Compare(compareSource.GetValue(root), CompareOperator, CompareValue); return result.HasValue && result.Value && requiredValueCondition; }
protected override bool ConditionApplies(ModelInstance root) { // Get the allowed values var allowedValues = GetAllowedValues(root); // Always consider as valid when there are no allowed values if (allowedValues == null) return false; // Value properties if (Property is ModelValueProperty) { // List if (Property.IsList) { // Get the current property value var values = root.GetValue((ModelValueProperty)Property) as IEnumerable; // Determine whether the property value is in the list of allowed values return !(values == null || values.Cast<object>().All(value => allowedValues.Contains(value))); } // Value else { // Get the current property value var value = root.GetValue((ModelValueProperty)Property); // Determine whether the property value is in the list of allowed values return !(value == null || allowedValues.Contains(value)); } } // Reference properties else { // List Property if (Property.IsList) { // Get the current property value ModelInstanceList values = root.GetList((ModelReferenceProperty)Property); // Determine whether the property value is in the list of allowed values return !(values == null || values.All(value => allowedValues.Contains(value))); } // Reference Property else { // Get the current property value ModelInstance value = root.GetReference((ModelReferenceProperty)Property); // Determine whether the property value is in the list of allowed values return !(value == null || allowedValues.Contains(value)); } } }