Ejemplo n.º 1
0
            public TableResult Execute(IStorageTableOperation operation)
            {
                TableOperationType     operationType = operation.OperationType;
                ITableEntity           entity        = operation.Entity;
                Tuple <string, string> key;
                IDictionary <string, EntityProperty> writeProperties;

                if (operation.OperationType != TableOperationType.Retrieve)
                {
                    key             = new Tuple <string, string>(entity.PartitionKey, entity.RowKey);
                    writeProperties = entity.WriteEntity(operationContext: null);
                }
                else
                {
                    key             = new Tuple <string, string>(operation.RetrievePartitionKey, operation.RetrieveRowKey);
                    writeProperties = null;
                }

                switch (operation.OperationType)
                {
                case TableOperationType.Retrieve:
                    TableItem item = _entities[key];
                    return(new TableResult
                    {
                        Result = operation.RetrieveEntityResolver.Resolve(operation.RetrievePartitionKey,
                                                                          operation.RetrieveRowKey, item.Timestamp, item.CloneProperties(), eTag: item.ETag)
                    });

                case TableOperationType.Insert:
                    if (!_entities.TryAdd(key, new TableItem(writeProperties)))
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                          "Entity PK='{0}',RK='{1}' already exists.", entity.PartitionKey, entity.RowKey));
                    }
                    return(new TableResult());

                case TableOperationType.Replace:
                    if (entity.ETag == null)
                    {
                        throw new InvalidOperationException("Replace requires an ETag.");
                    }
                    else if (!_entities.TryUpdate(key, new TableItem(writeProperties), new TableItem(entity.ETag)))
                    {
                        if (entity.ETag == "*")
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                              "Entity PK='{0}',RK='{1}' does not exist.", entity.PartitionKey, entity.RowKey));
                        }
                        else
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                              "Entity PK='{0}',RK='{1}' does not match eTag '{2}'.", entity.PartitionKey,
                                                                              entity.RowKey, entity.ETag));
                        }
                    }
                    return(new TableResult());

                default:
                    throw new InvalidOperationException(
                              "Unsupported operation type " + operationType.ToString());
                }
            }