示例#1
0
        /// <summary>
        /// Evaluate a query and return its results.
        /// </summary>
        /// <typeparam name="U">
        /// The type of element returned by the query.
        /// </typeparam>
        /// <param name="table">
        /// Access to MobileServices table operations.
        /// </param>
        /// <param name="query">
        /// The description of the query to evaluate and get the results for.
        /// </param>
        /// <returns>Results of the query.</returns>
        internal static Task <IEnumerable <U> > EvaluateQueryAsync <U>(IMobileServiceTable <T> table, MobileServiceTableQueryDescription query)
        {
            Debug.Assert(query != null, "query cannot be null!");
            Debug.Assert(table != null, "table cannot be null!");

            // Send the query
            string odata = query.ToString();

            return(table.ReadAsync(odata, query.Parameters).ContinueWith(t =>
            {
                // Parse the results
                long totalCount;
                JsonArray values = MobileServiceTable <T> .GetResponseSequence(t.Result, out totalCount);
                return (IEnumerable <U>) new TotalCountEnumerable <U>(
                    totalCount,
                    values.Select(
                        item =>
                {
                    // Create and fill a new instance of the type we should
                    // deserialize (which is either T or the input param
                    // to the projection that will modify T).
                    object obj = Activator.CreateInstance(query.ProjectionArgumentType ?? typeof(U));
                    MobileServiceTableSerializer.Deserialize(item, obj);

                    // Apply the projection to the instance transforming it
                    // as desired
                    if (query.Projection != null)
                    {
                        obj = query.Projection.DynamicInvoke(obj);
                    }

                    return (U)obj;
                }));
            }));
        }
示例#2
0
        /// <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>
        /// Delete an instance from the table.
        /// </summary>
        /// <param name="instance">The instance to delete.</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 delete has finished.
        /// </returns>
        public Task DeleteAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JsonObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            // Send the request
            return(DeleteAsync(value, parameters)
                   .ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    throw t.Exception.InnerException;
                }

                // Clear the instance ID since it's no longer associated with that
                // ID on the server (note that reflection is goodly enough to turn
                // null into the correct value for us).
                SerializableType type = SerializableType.Get(typeof(T));
                type.IdMember.SetValue(instance, null);
            }));
        }
        /// <summary>
        /// Get an element from a table by its ID.
        /// </summary>
        /// <param name="id">The ID of the element.</param>
        /// <returns>The desired element.</returns>
        public Task <T> LookupAsync(object id)
        {
            // TODO: At some point in the future this will be involved in our
            // caching story and relationships across tables via foreign
            // keys.

            return(SendLookupAsync(id).ContinueWith(t => MobileServiceTableSerializer.Deserialize <T> (t.Result.AsObject())));
        }
示例#5
0
        /// <summary>
        /// Get an element from a table by its ID.
        /// </summary>
        /// <param name="id">The ID of the element.</param>
        /// <returns>The desired element.</returns>
        public async Task <T> LookupAsync(object id)
        {
            // TODO: At some point in the future this will be involved in our
            // caching story and relationships across tables via foreign
            // keys.

            JToken value = await this.SendLookupAsync(id);

            return(MobileServiceTableSerializer.Deserialize <T>(value.AsObject()));
        }
示例#6
0
        /// <summary>
        /// Get an element from a table by its ID.
        /// </summary>
        /// <param name="id">The ID of the element.</param>
        /// <param name="parameters">
        /// A dictionary of user-defined parameters and values to include in the request URI query string.
        /// </param>
        /// <returns>The desired element.</returns>
        public new async Task <T> LookupAsync(object id, IDictionary <string, string> parameters)
        {
            // TODO: At some point in the future this will be involved in our
            // caching story and relationships across tables via foreign
            // keys.

            IJsonValue value = await this.SendLookupAsync(id, parameters);

            return(MobileServiceTableSerializer.Deserialize <T>(value.AsObject()));
        }
示例#7
0
        /// <summary>
        /// Insert a new instance into the table.
        /// </summary>
        /// <param name="instance">The instance to insert.</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 insertion has finished.
        /// </returns>
        public Task InsertAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JsonObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            return(InsertAsync(value, parameters)
                   .ContinueWith(t => MobileServiceTableSerializer.Deserialize(t.Result, instance)));
        }
        /// <summary>
        /// Updates an instance in the table.
        /// </summary>
        /// <param name="instance">The instance to update.</param>
        /// <returns>
        /// A task that will complete when the update has finished.
        /// </returns>
        public Task UpdateAsync(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JsonObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            // Send the request
            return(UpdateAsync(value)
                   .ContinueWith(t => MobileServiceTableSerializer.Deserialize(t.Result, instance)));
        }
示例#9
0
        /// <summary>
        /// Updates an instance in the table.
        /// </summary>
        /// <param name="instance">The instance to update.</param>
        /// <returns>
        /// A task that will complete when the update has finished.
        /// </returns>
        public async Task UpdateAsync(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            // Send the request
            JToken response = await this.UpdateAsync(value);

            // Deserialize the response back into the instance in case any
            // server scripts changed values of the instance.
            MobileServiceTableSerializer.Deserialize(response, instance);
        }
示例#10
0
        /// <summary>
        /// Updates an instance in the table.
        /// </summary>
        /// <param name="instance">The instance to update.</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 update has finished.
        /// </returns>
        public async Task UpdateAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JsonObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            // Send the request
            IJsonValue response = await this.UpdateAsync(value, parameters);

            // Deserialize the response back into the instance in case any
            // server scripts changed values of the instance.
            MobileServiceTableSerializer.Deserialize(response, instance);
        }
示例#11
0
        /// <summary>
        /// Delete an instance from the table.
        /// </summary>
        /// <param name="instance">The instance to delete.</param>
        /// <returns>
        /// A task that will complete when the delete has finished.
        /// </returns>
        public async Task DeleteAsync(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Serialize the instance
            JObject value = MobileServiceTableSerializer.Serialize(instance).AsObject();

            // Send the request
            await this.DeleteAsync(value);

            // Clear the instance ID since it's no longer associated with that
            // ID on the server (note that reflection is goodly enough to turn
            // null into the correct value for us).
            SerializableType type = SerializableType.Get(typeof(T));

            type.IdMember.SetValue(instance, null);
        }
示例#12
0
        /// <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);
            }
        }