// Currently used by InMemoryTable.
 public static TElement CopyEntity <TElement>(ITableEntity entity) where TElement : ITableEntity, new()
 {
     // Copy mutable data in case both entities are DynamicTableEntity.
     // https://github.com/Azure/azure-storage-net/issues/154
     // We could equally well do "new TElement" and use the setters directly.
     return((TElement)AzureTableAccessors.GetRetrieveResolver(TableOperation.Retrieve <TElement>("1", "1"))(
                entity.PartitionKey, entity.RowKey, entity.Timestamp, CopyPropertyDict(entity.WriteEntity(null)), entity.ETag));
 }
        // Single in terms of batch
        // FIXME: IChainTable2 (following IChainTable) allows Retrieve in
        // ExecuteAsync but not ExecuteBatchAsync.  What do we want to do?
        public virtual async Task <TableResult> ExecuteAsync(TableOperation operation,
                                                             TableRequestOptions requestOptions = null, OperationContext operationContext = null)
        {
            if (operation.GetOperationType() == TableOperationType.Retrieve)
            {
                // Retrieve supports custom entity resolvers but query doesn't.
                // Work around this by querying as DynamicTableEntity and then
                // running the custom resolver.  XXX: Fix IChainTable2 so we can
                // skip this step and avoid the cost.
                var query = new TableQuery <DynamicTableEntity> {
                    FilterString = ChainTableUtils.GeneratePointRetrievalFilterCondition(operation.GetRetrievePrimaryKey())
                };
                DynamicTableEntity entity = (await ExecuteQueryAtomicAsync(query, requestOptions, operationContext)).SingleOrDefault();
                if (entity == null)
                {
                    return new TableResult {
                               HttpStatusCode = (int)HttpStatusCode.NotFound
                    }
                }
                ;
                else
                {
                    return new TableResult
                           {
                               HttpStatusCode = (int)HttpStatusCode.OK,
                               Result         = AzureTableAccessors.GetRetrieveResolver(operation)(
                                   entity.PartitionKey, entity.RowKey, entity.Timestamp, entity.WriteEntity(null), entity.ETag),
                               Etag = entity.ETag
                           }
                };
            }

            var batch = new TableBatchOperation();

            batch.Add(operation);
            try
            {
                IList <TableResult> resultList = await ExecuteBatchAsync(batch, requestOptions, operationContext);

                Debug.Assert(resultList.Count == 1);
                return(resultList[0]);
            }
            catch (ChainTableBatchException e)
            {
                // XXX: Does this lose the stack trace?
                throw new StorageException(e.RequestInformation, e.Message, e.InnerException);
            }
        }