/// <summary> /// Refresh the current instance with the latest values from the /// table. /// </summary> /// <param name="instance">The instance to refresh.</param> /// <param name="parameters">A dictionary of user-defined parameters and values to include in the request URI query string.</param> /// <returns> /// A task that will complete when the refresh has finished. /// </returns> public Task RefreshAsync(T instance, IDictionary <string, string> parameters) { if (instance == null) { throw new ArgumentNullException("instance"); } // Only refresh if it's already on the server SerializableType type = SerializableType.Get(typeof(T)); object id = type.IdMember.GetValue(instance); TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); if (!SerializableType.IsDefaultIdValue(id)) { // Get the latest version of this element GetSingleValueAsync(id, parameters).ContinueWith(t => { // Deserialize that value back into the current instance MobileServiceTableSerializer.Deserialize(t.Result, instance); tcs.SetResult(true); }); } else { tcs.SetResult(true); } return(tcs.Task); }
/// <summary> /// Refresh the current instance with the latest values from the /// table. /// </summary> /// <param name="instance">The instance to refresh.</param> /// <returns> /// A task that will complete when the refresh has finished. /// </returns> public async Task RefreshAsync(T instance) { if (instance == null) { throw new ArgumentNullException("instance"); } // Only refresh if it's already on the server SerializableType type = SerializableType.Get(typeof(T)); object id = type.IdMember.GetValue(instance); if (!SerializableType.IsDefaultIdValue(id)) { // Get the latest version of this element JObject obj = await this.GetSingleValueAsync(id); // Deserialize that value back into the current instance MobileServiceTableSerializer.Deserialize(obj, instance); } }
/// <summary> /// Serialize an instance to a JSON value. /// </summary> /// <param name="instance">The instance to serialize.</param> /// <returns>The serialized JSON value.</returns> /// <param name="ignoreCustomSerialization"> /// A value to indicate whether or not custom serialization should be /// ignored if the instance implements /// ICustomMobileServiceTableSerialization. This flag is used by /// implementations of ICustomMobileServiceTableSerialization that /// want to invoke the default serialization behavior. /// </param> public static JToken Serialize(object instance, bool ignoreCustomSerialization) { if (instance == null) { throw new ArgumentNullException("instance"); } // If the instance implements // ICustomMobileServiceTableSerialization, allow it to handle its // own serialization. if (!ignoreCustomSerialization) { ICustomMobileServiceTableSerialization custom = instance as ICustomMobileServiceTableSerialization; if (custom != null) { return(custom.Serialize()); } } // Get the Mobile Services specific type info SerializableType type = SerializableType.Get(instance.GetType()); // Create a new JSON object to represent the instance JObject obj = new JObject(); foreach (SerializableMember member in type.Members.Values.OrderBy(m => m.Order)) { // Get the value to serialize object value = member.GetValue(instance); if (member.Converter != null) { // If there's a user defined converter, we can apply that // and set the value directly. obj.Set(member.Name, member.Converter.ConvertToJson(value)); } else if (member == type.IdMember && SerializableType.IsDefaultIdValue(value)) { // Special case the ID member so we don't write out any // value if wasn't set (i.e., has a 0 or null value). This // allows us flexibility for the type of the ID column // which is currently a long in SQL but could someday be a // GUID in something like a document database. At some // point we might also change the server to quietly ignore // default values for the ID column on insert. } else if (!obj.TrySet(member.Name, value)) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTableSerializer_Serialize_UnknownType, member.Name, member.Type.FullName, type.Type.Name), "instance"); } } return(obj); }