/// <summary>
        /// Inserts an <paramref name="instance"/> into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert into the table.
        /// </param>
        /// <param name="parameters">
        /// A dictionary of user-defined parameters and values to include in
        /// the request URI query string.
        /// </param>
        /// <param name="features">
        /// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
        /// </param>
        /// <returns>
        /// A task that will complete when the insert finishes.
        /// </returns>
        internal async Task <JToken> InsertAsync(JObject instance, IDictionary <string, string> parameters, MobileServiceFeatures features)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Make sure the instance doesn't have an int id set for an insertion
            object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: true);
            bool   isStringIdOrDefaultIntId = id is string || MobileServiceSerializer.IsDefaultId(id);

            if (!isStringIdOrDefaultIntId)
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Cannot insert if the {0} member is already set.",
                              MobileServiceSystemColumns.Id),
                          "instance");
            }

            features = this.AddRequestFeatures(features, parameters);
            string uriString = GetUri(this.TableName, null, parameters);

            return(await this.TransformHttpException(async() =>
            {
                MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Post, uriString, this.MobileServiceClient.CurrentUser, instance.ToString(Formatting.None), true, features: this.Features | features);
                return GetJTokenFromResponse(response);
            }));
        }
Beispiel #2
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");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance);

            if (serializer.IsDefaultId(id))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_UpdateWithoutId,
                              MobileServiceUrlBuilder.IdPropertyName),
                          "instance");
            }

            string value        = serializer.Serialize(instance);
            string updatedValue = await this.SendUpdateAsync(id, value, parameters);

            serializer.Deserialize <T>(updatedValue, instance);
        }
Beispiel #3
0
        /// <summary>
        /// Deletes 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 async Task DeleteAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance);

            if (serializer.IsDefaultId(id))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_DeleteWithoutId,
                              MobileServiceUrlBuilder.IdPropertyName),
                          "instance");
            }

            // Send the request
            await this.SendDeleteAsync(id, parameters);

            // 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).
            serializer.ClearId(instance);
        }
Beispiel #4
0
        /// <summary>
        /// Inserts 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 async Task InsertAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;

            object id = serializer.GetId(instance);

            // Make sure the instance doesn't have an id set for an insertion
            if (!serializer.IsDefaultId(id))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_InsertWithExistingId,
                              MobileServiceUrlBuilder.IdPropertyName),
                          "instance");
            }

            string value         = serializer.Serialize(instance);
            string insertedValue = await this.SendInsertAsync(value, parameters);

            serializer.Deserialize <T>(insertedValue, instance);
        }
Beispiel #5
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 async Task RefreshAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance, allowDefault: true);

            if (MobileServiceSerializer.IsDefaultId(id))
            {
                return;
            }
            if (id is string)
            {
                MobileServiceSerializer.EnsureValidStringId(id, allowDefault: true);
            }

            // Get the latest version of this element
            JObject refreshed = await this.GetSingleValueAsync(id, parameters);

            // Deserialize that value back into the current instance
            serializer.Deserialize <T>(refreshed, instance);
        }
        /// <summary>
        /// Inserts an <paramref name="instance"/> into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert into the table.
        /// </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 insert finishes.
        /// </returns>
        public async Task <JToken> InsertAsync(JObject instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Make sure the instance doesn't have an id set for an insertion
            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance, true);

            if (!serializer.IsDefaultId(id))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_InsertWithExistingId,
                              MobileServiceUrlBuilder.IdPropertyName),
                          "instance");
            }

            string response = await this.SendInsertAsync(instance.ToString(), parameters);

            return(response.ParseToJToken());
        }
        /// <summary>
        /// Inserts an <paramref name="instance"/> into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert into the table.
        /// </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 insert finishes.
        /// </returns>
        public async Task <JToken> InsertAsync(JObject instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // Make sure the instance doesn't have an int id set for an insertion
            object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: true);
            bool   isStringIdOrDefaultIntId = id is string || MobileServiceSerializer.IsDefaultId(id);

            if (!isStringIdOrDefaultIntId)
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_InsertWithExistingId,
                              MobileServiceSerializer.IdPropertyName),
                          "instance");
            }

            parameters = AddSystemProperties(this.SystemProperties, parameters);

            string uriString = GetUri(this.TableName, null, parameters);
            MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Post, uriString, this.MobileServiceClient.CurrentUser, instance.ToString(Formatting.None), true);

            return(GetJTokenFromResponse(response));
        }
        /// <summary>
        /// Returns the id of the <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">
        /// The instance that should have an id.
        /// </param>
        /// <returns>
        /// The id of the instance.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown if the <paramref name="instance"/> does not have an id.
        /// </exception>
        private object GetIdFromInstance(JObject instance)
        {
            Debug.Assert(instance != null);

            // Get the value of the object (as a primitive JSON type)
            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance);

            if (serializer.IsDefaultId(id))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.MobileServiceTable_IdNotFound,
                              MobileServiceUrlBuilder.IdPropertyName),
                          "instance");
            }

            return(id);
        }