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++;
            }
        }
 public static string ToDateTimeSuffix(this int value, RowKeyDateTimeFormat format)
 {
     return(format == RowKeyDateTimeFormat.Short ? value.ToString("000") : '.' + value.ToString("000"));
 }
 public static string ToDateTimeSuffix(this int value, RowKeyDateTimeFormat format)
 {
     return(value.ToString("000"));
 }
 public static string ToDateTimeMask(this RowKeyDateTimeFormat format)
 {
     return(format == RowKeyDateTimeFormat.Short ? "yyyyMMddHHmmss" : "yyyy-MM-dd HH:mm:ss");
 }
Example #5
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++;
            }
        }