/// <summary> /// Removes the specified object from the RelatedObjectList. /// </summary> /// <param name="item">The object to remove from the RelatedObjectList.</param> /// <returns></returns> public bool Remove(TypeProjection item) { if (IsReadOnly) { throw new InvalidOperationException("Cannot remove from a read-only list."); } if (item == null) { throw new ArgumentNullException("Cannot remove a null value on a RelatedObjectList."); } if (!IsOfType(typeof(T), item.GetType())) { throw new InvalidOperationException("This RelatedObjectList only supports removing objects of type " + typeof(T).Name); } foreach (ExpandoObject obj in ProjectionList) { if (AreProjectionsEqual(obj, item.CurrentObject)) { bool success = ProjectionList.Remove(obj); if (success) { Owner.IsDirty = true; } return(success); } } return(false); }
/// <summary> /// Serializes a TypeProjection to a JSON string /// </summary> /// <param name="writer">JsonWriter to use</param> /// <param name="value">Object to convert</param> /// <param name="serializer">JsonSerializer to use</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { TypeProjection projection = (TypeProjection)value; writer.WriteStartObject(); #region formJson object { writer.WritePropertyName("formJson"); writer.WriteStartObject(); writer.WritePropertyName("isDirty"); writer.WriteValue(projection.IsDirty); //"current" object writer.WritePropertyName("current"); writer.WriteRawValue(JsonConvert.SerializeObject(projection.CurrentObject)); //"original" object writer.WritePropertyName("original"); writer.WriteRawValue(JsonConvert.SerializeObject(projection.OriginalObject)); writer.WriteEndObject(); // formJson } #endregion formJsonObject writer.WriteEndObject(); // JSON }
/// <summary> /// Determines whether an object is in the RelatedObjectList. /// </summary> /// <param name="item">The object to locate in the RelatedObjectList.</param> /// <returns></returns> public bool Contains(TypeProjection item) { foreach (ExpandoObject obj in ProjectionList) { if (AreProjectionsEqual(obj, item.CurrentObject)) { return(true); } } return(false); }
/// <summary> /// Inserts an object into the RelatedObjectList at the specified index. /// </summary> /// <param name="index">The zero-based index at which an object should be inserted.</param> /// <param name="item">The object to insert. Duplicates will be ignored.</param> public void Insert(int index, TypeProjection item) { if (IsReadOnly) { throw new InvalidOperationException("Cannot insert into a read-only list."); } if (item == null) { throw new ArgumentNullException("Cannot insert a null value on a RelatedObjectList."); } if (!IsOfType(typeof(T), item.GetType())) { throw new InvalidOperationException("This RelatedObjectList only supports inserting objects of type " + typeof(T).Name); } if (!Contains(item)) { ProjectionList.Insert(index, item.CurrentObject); Owner.IsDirty = true; } }
/// <summary> /// Searches for the specified object and returns the zero-based index of the object's occurance within the entire RelatedObjectList. /// </summary> /// <param name="item">The object to locate in the RelatedObjectList.</param> /// <returns></returns> public int IndexOf(TypeProjection item) { return(ProjectionList.IndexOf(item.CurrentObject)); }
/// <summary> /// Sets up a new RelatedObjectList based on the specified type projection's model property. /// </summary> /// <param name="owner">TypeProjection object that owns the related object list</param> /// <param name="modelProperty">Data model property that contains the list of related objects</param> internal RelatedObjectList(TypeProjection owner, string modelProperty) { Owner = owner; ModelProperty = modelProperty; }