private async Task <List <TableOperation> > BuildTableOperationUpdate(T entity)
        {
            var entityIdValues = EntityDefinition.GetIdValues(entity);
            var idString       = string.Join(StorageQueryBuilder.PARTITION_FIELD_SEPARATOR,
                                             entityIdValues.Select(StorageQueryBuilder.NormalizeStringValue));

            var serializedField = EntityProperty.GeneratePropertyForByteArray(BSonConvert.SerializeObject(entity));

            var fields = GetEntityFilterableFields(entity);

            var record = GenerateRecordMainPartition(entity, serializedField, fields);

            List <TableOperation> operations = new List <TableOperation>
            {
                TableOperation.Replace(record)
            };

            var old = await GetById(idString).ConfigureAwait(false);

            if (old != null)
            {
                var oldFields = GetEntityFilterableFields(old);
                operations.AddRange(GeneratePersistPartitionData(old, null, oldFields).Select(TableOperation.Delete));
            }
            return(operations);
        }
        private IEnumerable <DynamicTableEntity> GeneratePersistPartitionData(T entity, EntityProperty serializedField, IEnumerable <KeyValuePair <string, EntityProperty> > fields)
        {
            List <DynamicTableEntity> result = new List <DynamicTableEntity>();

            foreach (var partition in EntityDefinition.GetPartitionsValues(entity))
            {
                var entityIdValues = EntityDefinition.GetIdValues(entity);
                var idString       = string.Join(StorageQueryBuilder.PARTITION_FIELD_SEPARATOR, entityIdValues.Select(StorageQueryBuilder.NormalizeStringValue));

                var partitionKey = StorageQueryBuilder.GetPartitionKeyValue(partition.Key, partition.Value);
                partitionKey = string.Join(StorageQueryBuilder.PARTITION_NAME_SEPARATOR, partition.Key, partitionKey);

                DynamicTableEntity record = new DynamicTableEntity(partitionKey, StorageQueryBuilder.GetTableKeyNormalizedValue(idString));
                record.ETag = "*";
                foreach (var field in fields)
                {
                    if (field.Value != null)
                    {
                        record.Properties.Add(field);
                    }
                }
                record.Properties.Add("Content", serializedField);
                result.Add(record);
            }

            return(result);
        }
        private async Task <List <TableOperation> > BuildTableOperationUpsert(T entity)
        {
            var entityId        = EntityDefinition.GetIdValues(entity);
            var serializedField = EntityProperty.GeneratePropertyForByteArray(BSonConvert.SerializeObject(entity));
            var fields          = GetEntityFilterableFields(entity).ToList();
            var record          = GenerateRecordMainPartition(entity, serializedField, fields);

            List <TableOperation> operations = new List <TableOperation>
            {
                TableOperation.InsertOrReplace(record)
            };

            operations.AddRange(GeneratePersistPartitionData(entity, serializedField, fields)
                                .Select(TableOperation.InsertOrReplace));
            var idValue = string.Join(StorageQueryBuilder.PARTITION_FIELD_SEPARATOR,
                                      entityId.Select(StorageQueryBuilder.NormalizeStringValue));

            var old = await GetById(StorageQueryBuilder.GetTableKeyNormalizedValue(idValue)).ConfigureAwait(false);

            if (old != null)
            {
                var oldFields = GetEntityFilterableFields(old);
                IEnumerable <TableOperation> deleteOperations = GeneratePersistPartitionData(old, null, oldFields)
                                                                .Select(TableOperation.Delete)
                                                                .Where(d => !operations.Any(o =>
                                                                                            o.Entity.PartitionKey == d.Entity.PartitionKey && o.Entity.RowKey == d.Entity.RowKey))
                                                                .ToList();
                operations.AddRange(deleteOperations);
            }
            return(operations);
        }
        private DynamicTableEntity GenerateRecordMainPartition(T entity, EntityProperty serializedField, IEnumerable <KeyValuePair <string, EntityProperty> > fields)
        {
            var entityIdValues = EntityDefinition.GetIdValues(entity);
            var idString       = string.Join(StorageQueryBuilder.PARTITION_FIELD_SEPARATOR, entityIdValues.Select(StorageQueryBuilder.NormalizeStringValue));

            DynamicTableEntity record = new DynamicTableEntity(StorageQueryBuilder.MAIN_PARTITION_NAME, StorageQueryBuilder.GetTableKeyNormalizedValue(idString))
            {
                ETag = "*"
            };

            if (fields != null)
            {
                foreach (var field in fields)
                {
                    if (field.Value != null)
                    {
                        record.Properties.Add(field);
                    }
                }
            }

            record.Properties.Add("Content", serializedField);
            return(record);
        }