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> /// 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) { // 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); } } }
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))); } } }
protected internal override void OnInvoke(ModelInstance root, ModelEvent modelEvent) { // Marks the instance as pending delete if an owner property is set to null. if (Property is ModelReferenceProperty && !Property.IsList) { root.IsPendingDelete = root.GetReference((ModelReferenceProperty)Property) == null; } }
/// <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> /// 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> /// 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); } } }
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; }
protected internal override void OnInvoke(ModelInstance root, ModelEvent modelEvent) { // Marks the instance as pending delete if an owner property is set to null. if (Property is ModelReferenceProperty && !Property.IsList) root.IsPendingDelete = root.GetReference((ModelReferenceProperty)Property) == null; }
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)); } } }