Ejemplo n.º 1
0
        /// <summary>
        /// Перебирает по одному ключу, пока не получится вставить запись в таблицу
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <param name="entity"></param>
        /// <param name="dateTime"></param>
        /// <param name="rowKeyDateTimeFormat">Формат ключа</param>
        /// <returns></returns>
        public static T InsertAndGenerateRowKeyAsDateTime <T>(this INoSQLTableStorage <T> table, T entity, DateTime dateTime, RowKeyDateTimeFormat rowKeyDateTimeFormat = RowKeyDateTimeFormat.Iso) where T : ITableEntity, new()
        {
            var dt = dateTime.ToString(rowKeyDateTimeFormat.ToDateTimeMask());
            var no = 0;

            while (true)
            {
                entity.RowKey = dt + no.ToDateTimeSuffix(rowKeyDateTimeFormat);

                try
                {
                    table.Insert(entity, Conflict);
                    return(entity);
                }
                catch (StorageException e)
                {
                    if (e.RequestInformation.HttpStatusCode != Conflict)
                    {
                        throw;
                    }
                }

                if (no == 999)
                {
                    throw new Exception("Can not insert record: " + PrintItem(entity));
                }
                no++;
            }
        }
        public static async Task <T> InsertAndGenerateRowKeyAsDateTimeAsync <T>(this IAzureTableStorage <T> table, T entity, DateTime dateTime, RowKeyDateTimeFormat rowKeyDateTimeFormat = RowKeyDateTimeFormat.Iso) where T : ITableEntity, new()
        {
            var dt = dateTime.ToString(rowKeyDateTimeFormat.ToDateTimeMask());
            var no = 0;

            while (true)
            {
                entity.RowKey = dt + no.ToDateTimeSuffix(rowKeyDateTimeFormat);

                try
                {
                    await table.InsertAsync(entity, Conflict);

                    return(entity);
                }
                catch (AggregateException e)
                {
                    if (e.InnerExceptions[0] is StorageException)
                    {
                        var se = e.InnerExceptions[0] as StorageException;
                        if (se.RequestInformation.HttpStatusCode != Conflict)
                        {
                            throw;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (StorageException e)
                {
                    if (e.RequestInformation.HttpStatusCode != Conflict)
                    {
                        throw;
                    }
                }

                if (no == 999)
                {
                    throw new Exception("Can not insert record: " + PrintItem(entity));
                }
                no++;
            }
        }