Esempio n. 1
0
        public async Task Save(TEntity entity, CancellationToken cancellationToken)
        {
            //Flatten object of type Order) and convert it to EntityProperty Dictionary
            Dictionary <string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(entity, new OperationContext());
            var rowKey       = GetEntityId(entity);
            var partitionKey = GetEntityPartitionKey(entity);

            // Create a DynamicTableEntity and set its PK and RK
            DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);

            dynamicTableEntity.Properties = flattenedProperties;

            var existingEntity = await Get(dynamicTableEntity.PartitionKey, dynamicTableEntity.RowKey, cancellationToken);

            TableOperation createOrReplaceOperation;

            if (existingEntity == null)
            {
                createOrReplaceOperation = TableOperation.Insert(dynamicTableEntity);
            }
            else
            {
                dynamicTableEntity.ETag  = existingEntity.ETag;
                createOrReplaceOperation = TableOperation.Replace(dynamicTableEntity);
            }

            await _cloudTable.ExecuteAsync(createOrReplaceOperation);
        }
Esempio n. 2
0
        public static async Task UpdateAsync <TPayload>(
            this CloudTable table, TPayload payload,
            KeysPair keys)
            where TPayload : class, new()
        {
            Ensure.ArgumentNotNull(keys, nameof(keys));
            Ensure.ArgumentNotNull(payload, nameof(payload));
            Ensure.ArgumentNotNull(table, nameof(table));

            var entity = new DynamicTableEntity(keys.PartitionKey, keys.RowKey);

            entity.Timestamp  = DateTime.UtcNow;
            entity.Properties = EntityPropertyConverter.Flatten(payload, new OperationContext());
            entity.ETag       = "*";

            var batch = new TableBatchOperation();

            batch.Replace(entity);

            await table.ExecuteBatchAsync(batch)
            .ConfigureAwait(false);

            await table.ExecuteBatchAsync(batch)
            .ConfigureAwait(false);
        }
        public virtual void Add(T obj)
        {
            //Table Exists?
            _cloudTable = _tableClient.GetTableReference(obj.GetType().Name);
            _cloudTable.CreateIfNotExistsAsync().Wait();

            //Obtain Partition Key and Row Key
            //SetPartitionRowKeys(obj);
            PropertyInfo[] props = obj.GetType().GetProperties();
            foreach (var p in props)
            {
                if (p.Name != nameof(RowKey) && p.Name != nameof(PartitionKey) && p.Name != nameof(ETag) && p.Name != nameof(Timestamp))
                {
                    obj.GetType().GetProperty("RowKey").SetValue(obj, p.Name.ToString(), null);

                    //TODO: Create Object with PPartition, Row and Properties
                    string rowkey       = obj.GetType().GetProperty("RowKey").GetValue(obj).ToString();
                    string partitionKey = obj.GetType().GetProperty("PartitionKey").GetValue(obj).ToString();


                    if (p.PropertyType.Name.Contains("String"))
                    {
                        var entity = new DynamicTableEntity(partitionKey, rowkey);
                        //entity.Properties = EntityPropertyConverter.Flatten(p.GetValue(obj), null);

                        entity.Properties.Add("Value", new EntityProperty(p.GetValue(obj).ToString()));
                        InsertIntoTable(entity);
                    }
                    else if (!p.PropertyType.Name.Contains("List"))
                    {
                        var entity = new DynamicTableEntity(partitionKey, rowkey);
                        entity.Properties = EntityPropertyConverter.Flatten(p.GetValue(obj), null);

                        InsertIntoTable(entity);
                    }
                    else
                    {
                        int i = 1;
                        foreach (var f in (IEnumerable)p.GetValue(obj))
                        {
                            var entity = new DynamicTableEntity(partitionKey.ToUpper(), rowkey + EntityPropertyConverter.DefaultPropertyNameDelimiter + i++);
                            entity.Properties = EntityPropertyConverter.Flatten(f, null);

                            InsertIntoTable(entity);
                        }
                    }

                    //_tableOperation = TableOperation.InsertOrMerge((ITableEntity)entity);
                    //_cloudTable.ExecuteAsync(_tableOperation).Wait();
                }
            }

            //Insert or Upsert
            //_tableOperation = TableOperation.InsertOrMerge((ITableEntity)obj);
            //_cloudTable.ExecuteAsync(_tableOperation).Wait();
        }
Esempio n. 4
0
        private DynamicTableEntity Convert <T>(T item, string partitionKey)
        {
            DynamicTableEntity dynamicTableEntity =
                new DynamicTableEntity(partitionKey, item.GetKey())
            {
                Properties = EntityPropertyConverter.Flatten(item, new OperationContext())
            };

            dynamicTableEntity.ETag = "*";
            return(dynamicTableEntity);
        }
Esempio n. 5
0
        public async Task AddCoin(Coin coin)
        {
            var flattenedObject = EntityPropertyConverter.Flatten(coin, new OperationContext());
            var tableEntity     = new DynamicTableEntity("coin", coin.Id);

            tableEntity.Properties = flattenedObject;

            TableOperation insertOperation = TableOperation.InsertOrReplace(tableEntity);

            await this.coinsTable.ExecuteAsync(insertOperation);
        }
Esempio n. 6
0
        public async Task AddOrUpdateRating(Rating rating)
        {
            var flattenedObject = EntityPropertyConverter.Flatten(rating, new OperationContext());
            var tableEntity     = new DynamicTableEntity(rating.CoinId, rating.UserId);

            tableEntity.Properties = flattenedObject;

            TableOperation insertOperation = TableOperation.InsertOrReplace(tableEntity);

            await this.ratingsTable.ExecuteAsync(insertOperation);
        }
Esempio n. 7
0
        public async Task AddDataPoint(DataPoint dataPoint)
        {
            var flattenedObject = EntityPropertyConverter.Flatten(dataPoint, new OperationContext());
            var tableEntity     = new DynamicTableEntity(dataPoint.CoinId, dataPoint.Date);

            tableEntity.Properties = flattenedObject;

            TableOperation insertOperation = TableOperation.InsertOrReplace(tableEntity);

            await this.dataPointsTable.ExecuteAsync(insertOperation);
        }
Esempio n. 8
0
        public async Task AddMasternodeStats(MasternodeStats masternodeStats)
        {
            var flattenedObject = EntityPropertyConverter.Flatten(masternodeStats, new OperationContext());
            var tableEntity     = new DynamicTableEntity("coin", masternodeStats.CoinId);

            tableEntity.Properties = flattenedObject;

            TableOperation insertOperation = TableOperation.InsertOrReplace(tableEntity);

            await this.masternodeStatsTable.ExecuteAsync(insertOperation);
        }
Esempio n. 9
0
 public static async Task <DynamicTableEntity> Wrap <TPayload>(this KeysPair keyPair, TPayload payload)
 {
     return(await Task.Factory.StartNew(() =>
     {
         return new DynamicTableEntity(keyPair.PartitionKey, keyPair.RowKey)
         {
             Timestamp = DateTime.UtcNow,
             Properties = EntityPropertyConverter.Flatten(payload, new OperationContext()),
             ETag = "*"
         };
     }).ConfigureAwait(false));
 }
Esempio n. 10
0
        public async Task AddCoinExchange(CoinExchange coinExchange)
        {
            var flattenedObject = EntityPropertyConverter.Flatten(coinExchange, new OperationContext());
            var rowKey          = $"{coinExchange.ExchangeIdentifier}-{coinExchange.Base}-{coinExchange.Target}";
            var tableEntity     = new DynamicTableEntity(coinExchange.CoinId, rowKey);

            tableEntity.Properties = flattenedObject;

            TableOperation insertOperation = TableOperation.InsertOrReplace(tableEntity);

            await this.coinExchangesTable.ExecuteAsync(insertOperation);
        }
Esempio n. 11
0
        private TableOperation BuildOperation <TModel>(TModel source, string partitionKey, string rowKey, Func <DynamicTableEntity, TableOperation> action)
        {
            var context             = new OperationContext();
            var flattenedProperties = EntityPropertyConverter.Flatten(source, context);
            var model = new DynamicTableEntity(partitionKey, rowKey)
            {
                Properties = flattenedProperties
            };

            var operation = action(model);

            return(operation);
        }
Esempio n. 12
0
            protected override ITableEntity CreateTableEntity(EventData data)
            {
                var tableEntity = new EventDataTableEntity <EventData>(data, IsCorrelationIdTableStorageStore);
                //Flatten object of type TData and convert it to EntityProperty Dictionary
                Dictionary <string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data, new OperationContext());

                // Create a DynamicTableEntity and set its PK and RK
                DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(tableEntity.PartitionKey, tableEntity.RowKey)
                {
                    Properties = flattenedProperties
                };

                return(dynamicTableEntity);
            }
Esempio n. 13
0
        /// <summary>
        /// Creates a new instance of <see cref="DynamicTableEntity"/> populating it with the provided <paramref name="data"/>
        /// </summary>
        /// <param name="data">The data to store in <see cref="DynamicTableEntity.Properties"/>.</param>
        protected override ITableEntity CreateTableEntity(TData data)
        {
            var tableEntity = new EntityTableEntity <TData>(data);

            //Flatten object of type TData and convert it to EntityProperty Dictionary
#pragma warning disable 0436
            Dictionary <string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data, new OperationContext());
#pragma warning restore 0436
            // Create a DynamicTableEntity and set its PK and RK
            DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(tableEntity.PartitionKey, tableEntity.RowKey)
            {
                Properties = flattenedProperties
            };

            return(dynamicTableEntity);
        }
Esempio n. 14
0
        public static async Task AddAsync <TPayload>(
            this CloudTable table, TPayload payload,
            KeysPair keys)
        {
            Ensure.ArgumentNotNull(keys, nameof(keys));
            Ensure.ArgumentNotNull(payload, nameof(payload));
            Ensure.ArgumentNotNull(table, nameof(table));

            await table.ExecuteAsync(
                TableOperation.Insert(
                    new DynamicTableEntity(keys.PartitionKey, keys.RowKey)
            {
                Timestamp  = DateTime.UtcNow,
                Properties = EntityPropertyConverter.Flatten(payload, new OperationContext())
            }))
            .ConfigureAwait(false);
        }
        public void Should_gather_Blob_and_CollectionRefs_recursively_correctly_and_EntityPropertyConverter_should_successfully_flatten()
        {
            var testObject = new TableRow()
            {
                Name        = "Outer",
                Document    = new LargeBlob(),
                RelatedItem = new Attachment()
                {
                    Name             = "Att-01",
                    File             = new LargeBlob(),
                    NestedAttachment = new Attachment()
                    {
                        Name             = "Att01-Inner",
                        File             = new LargeBlob(),
                        NestedAttachment = null,
                        Keywords         = new List <string>()
                        {
                            "alpha", "bravo"
                        }
                    }
                },
                Dict = new Dictionary <string, int>()
                {
                    { "Key", 123 }
                }
            };

            var collectedBlobPropertyRefs       = ReflectionUtils.GatherPropertiesWithBlobsRecursive(testObject, new EntityPropertyConverterOptions());
            var collectedCollectionPropertyRefs = ReflectionUtils.GatherPropertiesWithCollectionsRecursive(testObject, new EntityPropertyConverterOptions());

            foreach (var blobPropRef in collectedBlobPropertyRefs)
            {
                blobPropRef.Property.SetValue(blobPropRef.SourceObject, null);
            }

            foreach (var collectionPropertyRef in collectedCollectionPropertyRefs)
            {
                collectionPropertyRef.Property.SetValue(collectionPropertyRef.SourceObject, null);
            }

            var flattened = EntityPropertyConverter.Flatten(testObject, new EntityPropertyConverterOptions(), null);

            collectedBlobPropertyRefs.Count.Should().Be(3);
            collectedCollectionPropertyRefs.Count.Should().Be(2);
        }
Esempio n. 16
0
        protected override ITableEntity ReplaceValues(TableResult retrievedResult, EntityTableEntity <TData> data)
        {
            ITableEntity tableEntity = (ITableEntity)retrievedResult.Result;
            // Events aren't updated
            var dynamicTableEntity = tableEntity as DynamicTableEntity;

            if (dynamicTableEntity == null)
            {
                base.ReplaceValues(retrievedResult, data);
                return(tableEntity);
            }

            //Flatten object of type TData and convert it to EntityProperty Dictionary
            Dictionary <string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data.Entity, new OperationContext());

            dynamicTableEntity.Properties = flattenedProperties;

            return(dynamicTableEntity);
        }
        private static DynamicTableEntity ToTableEntity(string key, object value)
        {
            // Flatten properties
            var properties = EntityPropertyConverter.Flatten(value, new OperationContext());

            // Add Type information
            var type = value.GetType();
            var typeQualifiedName = type.AssemblyQualifiedName;

            properties.Add("__type", EntityProperty.GeneratePropertyForString(typeQualifiedName));

            // Etag and PK/RK
            var etag = (value as IStoreItem)?.eTag;
            var ek   = new EntityKey(key);

            return(new DynamicTableEntity(ek.PartitionKey, ek.RowKey)
            {
                ETag = etag,
                Properties = properties
            });
        }