示例#1
0
        /*[Obsolete("Dependancy injection for legacy settings has been implemented and below parameters do not need to be explicitly passed. Use parameterless constructor")]
         * private SQLiteDataService(LanguagesEnum lang = default(LanguagesEnum), CountryCodes country = default(CountryCodes))
         * {
         * }*/

        /// <summary>
        /// Saves a model
        /// </summary>
        /// <typeparam name="T">The type of model to be saved</typeparam>
        /// <param name="model">The model</param>
        /// <returns>The saved model</returns>
        /// <exception cref="TimeoutException">Throws a TimeoutException if call doesn't complete within specified time duration</exception>
        public virtual async Task <SaveResponse <T> > SaveAsync(T model)
        {
            try
            {
                SaveResponse <T> result = default(SaveResponse <T>);
                await DataAccess.Instance.Connection.RunInTransactionAsync(
                    async connTran =>
                {
                    DataAccess.Instance.StartTransaction(connTran);
                    result = await this.SaveAsync(connTran, model);
                });

                DataAccess.Instance.CommitTransaction();
                this.Logger.Debug("Code after transaction call");
                return(result);
            }
            catch (Exception exception)
            {
                this.Logger.Debug(exception);
                throw;
            }
        }
示例#2
0
        private async Task <SaveResponse <T> > OverwriteAsync(T model, CriteriaBuilder criteriaBuilder)
        {
            try
            {
                this.Logger.Debug("Overwriting items on table " + model.TableName);
                List <T> previousItems = await this.GetManyByCriteria(criteriaBuilder);

                if (previousItems == null)
                {
                    previousItems = new List <T>();
                }

                this.Logger.Debug("Found ~ previous items to be overwritten".GetFormated(previousItems.Count));

                await DataAccess.Instance.Connection.RunInTransactionAsync(
                    async tran =>
                {
                    DataAccess.Instance.StartTransaction(tran);
                    foreach (var item in previousItems)
                    {
                        this.Logger.Debug("Deleting previous item");
                        await this.DeleteAsync(tran, item.Id);
                    }
                });

                DataAccess.Instance.CommitTransaction();
                SaveResponse <T> response = await this.SaveAsync(model);

                this.Logger.Debug("Exiting overwrite");
                return(response);
            }
            catch (Exception e)
            {
                this.Logger.Error(e);
                throw;
            }
        }