/// <summary>
        /// Defines a table to use for offline sync
        /// </summary>
        /// <param name="store">The offline store.</param>
        /// <param name="settings">The JSON Serializer settings</param>
        /// <typeparam name="T">The model type of the table</typeparam>
        public static void DefineTable <T>(this MobileServiceSQLiteStore store, MobileServiceJsonSerializerSettings settings)
        {
            string tableName = settings.ContractResolver.ResolveTableName(typeof(T));

            if (!(settings.ContractResolver.ResolveContract(typeof(T)) is JsonObjectContract contract))
            {
                throw new ArgumentException("The generic type T is not an object.");
            }
            if (contract.DefaultCreator == null)
            {
                throw new ArgumentException("The generic type T does not have parameterless constructor.");
            }

            // create an empty object
            object theObject = contract.DefaultCreator();

            SetEnumDefault(contract, theObject);

            JObject item = ConvertToJObject(settings, theObject);

            //// set default values so serialized version can be used to infer types
            SetIdDefault <T>(settings, item);
            SetNullDefault(contract, item);

            store.DefineTable(tableName, item);
        }
        private static JObject ConvertToJObject(MobileServiceJsonSerializerSettings settings, object theObject)
        {
            string  json = JsonConvert.SerializeObject(theObject, settings);
            JObject item = JsonConvert.DeserializeObject <JObject>(json, settings);

            return(item);
        }
Exemplo n.º 3
0
        public static void DefineTable <T>(this MobileServiceSQLiteStore store, MobileServiceJsonSerializerSettings settings)
        {
            string tableName = settings.ContractResolver.ResolveTableName(typeof(T));
            var    contract  = settings.ContractResolver.ResolveContract(typeof(T)) as JsonObjectContract;

            if (contract == null)
            {
                throw new ArgumentException(Properties.Resources.SQLiteStore_DefineTableTNotAnObject);
            }
            if (contract.DefaultCreator == null)
            {
                throw new ArgumentException(Properties.Resources.SQLiteStore_DefineTableEmptyCtorNotDefined);
            }

            // create an empty object
            object theObject = contract.DefaultCreator();

            SetEnumDefault(contract, theObject);

            JObject item = ConvertToJObject(settings, theObject);

            //// set default values so serialized version can be used to infer types
            SetIdDefault <T>(settings, item);
            SetNullDefault(contract, item);

            store.DefineTable(tableName, item);
        }
        internal static MobileServiceTableOperationError Deserialize(JObject obj, MobileServiceJsonSerializerSettings settings)
        {
            HttpStatusCode?status = null;

            if (obj["httpStatus"] != null)
            {
                status = (HttpStatusCode?)obj.Value <int?>("httpStatus");
            }
            string id = obj.Value <string>(MobileServiceSystemColumns.Id);
            long   operationVersion = obj.Value <long?>("operationVersion").GetValueOrDefault();
            MobileServiceTableOperationKind operationKind = (MobileServiceTableOperationKind)obj.Value <int>("operationKind");
            var tableName = obj.Value <string>("tableName");
            var tableKind = (MobileServiceTableKind)obj.Value <int?>("tableKind").GetValueOrDefault();

            string  itemStr   = obj.Value <string>("item");
            JObject item      = itemStr == null ? null : JObject.Parse(itemStr);
            string  rawResult = obj.Value <string>("rawResult");
            var     result    = rawResult.ParseToJToken(settings) as JObject;

            return(new MobileServiceTableOperationError(id,
                                                        operationVersion,
                                                        operationKind,
                                                        status,
                                                        tableName,
                                                        item,
                                                        rawResult,
                                                        result)
            {
                Id = id,
                TableKind = tableKind
            });
        }
        private static void DefineTable(MobileServiceLocalStore store, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("The data table type cannot be null");
            }

            System.Diagnostics.Debug.WriteLine($"AzureMobileClient.Helpers is defining a local store table for: {type.Name}");

            // Adopted from SQLite Store Generic Extensions
            var settings = new MobileServiceJsonSerializerSettings();
            var contract = settings.ContractResolver.ResolveContract(type) as JsonObjectContract;

            if (contract.DefaultCreator == null)
            {
                throw new ArgumentException($"The TableData type '{type.Name}' does not have parameterless constructor.");
            }

            object theObject = contract.DefaultCreator();

            SetEnumDefault(contract, theObject);
            var item = ConvertToJObject(settings, theObject);

            // set default values so serialized version can be used to infer types
            item["id"] = String.Empty;
            SetNullDefault(contract, item);

            store.DefineTable(type.Name, item);
        }
        private static void SetIdDefault <T>(MobileServiceJsonSerializerSettings settings, JObject item)
        {
            JsonProperty idProperty = settings.ContractResolver.ResolveIdProperty(typeof(T));

            if (idProperty.PropertyType == typeof(long) || idProperty.PropertyType == typeof(int))
            {
                item[MobileServiceSystemColumns.Id] = 0;
            }
            else
            {
                item[MobileServiceSystemColumns.Id] = String.Empty;
            }
        }
        /// <summary>
        /// Loads all the sync errors in local store that are recorded for this batch.
        /// </summary>
        /// <param name="serializerSettings">the serializer settings to use for reading the errors.</param>
        /// <returns>List of sync errors.</returns>
        public async Task<IList<MobileServiceTableOperationError>> LoadSyncErrorsAsync(MobileServiceJsonSerializerSettings serializerSettings)
        {
            var errors = new List<MobileServiceTableOperationError>();

            JToken result = await this.Store.ReadAsync(new MobileServiceTableQueryDescription(MobileServiceLocalSystemTables.SyncErrors));
            if (result is JArray)
            {
                foreach (JObject error in result)
                {
                    var obj = MobileServiceTableOperationError.Deserialize(error, serializerSettings);
                    obj.Context = this.context;
                    errors.Add(obj);
                }
            }            

            return errors;
        }
        /// <summary>
        /// Defines a table to use for offline sync
        /// </summary>
        /// <param name="store">The offline store.</param>
        /// <typeparam name="T">The model type of the table</typeparam>
        public static void DefineTable <T>(this MobileServiceSQLiteStore store)
        {
            var settings = new MobileServiceJsonSerializerSettings();

            DefineTable <T>(store, settings);
        }
        /// <summary>
        /// Loads all the sync errors in local store that are recorded for this batch.
        /// </summary>
        /// <param name="serializerSettings">the serializer settings to use for reading the errors.</param>
        /// <returns>List of sync errors.</returns>
        public async Task <IList <MobileServiceTableOperationError> > LoadSyncErrorsAsync(MobileServiceJsonSerializerSettings serializerSettings)
        {
            var errors = new List <MobileServiceTableOperationError>();

            JToken result = await this.Store.ReadAsync(new MobileServiceTableQueryDescription(MobileServiceLocalSystemTables.SyncErrors));

            if (result is JArray)
            {
                foreach (JObject error in result)
                {
                    var obj = MobileServiceTableOperationError.Deserialize(error, serializerSettings);
                    obj.Context = this.context;
                    errors.Add(obj);
                }
            }

            return(errors);
        }
        internal static MobileServiceTableOperationError Deserialize(JObject obj, MobileServiceJsonSerializerSettings settings)
        {
            HttpStatusCode? status = null;
            if (obj["httpStatus"] != null)
            {
                status = (HttpStatusCode?)obj.Value<int?>("httpStatus");
            }
            string id = obj.Value<string>(MobileServiceSystemColumns.Id);
            long operationVersion = obj.Value<long?>("operationVersion").GetValueOrDefault();
            MobileServiceTableOperationKind operationKind = (MobileServiceTableOperationKind)obj.Value<int>("operationKind");
            var tableName = obj.Value<string>("tableName");
            var tableKind = (MobileServiceTableKind)obj.Value<int?>("tableKind").GetValueOrDefault();

            string itemStr = obj.Value<string>("item");
            JObject item = itemStr == null ? null : JObject.Parse(itemStr);
            string rawResult = obj.Value<string>("rawResult");
            var result = rawResult.ParseToJToken(settings) as JObject;

            return new MobileServiceTableOperationError(id,
                                                        operationVersion,
                                                        operationKind,
                                                        status,
                                                        tableName,
                                                        item,
                                                        rawResult,
                                                        result)
            {
                Id = id,
                TableKind = tableKind
            };
        }