public async Task <Tuple <T, CMPStorageError> > FetchAsync <T>(string tableNameString, string partitionKeyString,
                                                                       string rowKeyString,
                                                                       CMPTableStorageOptions tableStorageOptions = null)
            where T : CMPTableStorageModel, new()
        {
            if (_cloudTableClient == null)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            if (string.IsNullOrEmpty(tableNameString) == true)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(new Tuple <T, CMPStorageError>(null, null));
                }

                var tableOperation = TableOperation.Retrieve <T>(partitionKeyString, rowKeyString);

                TableResult fetchedResultInfo = null;
                if (tableStorageOptions == null)
                {
                    fetchedResultInfo = await tableReference.ExecuteAsync(tableOperation);
                }
                else
                {
                    fetchedResultInfo = await tableReference.ExecuteAsync(tableOperation,
                                                                          tableStorageOptions.TableRequestOptions,
                                                                          tableStorageOptions.OperationContext,
                                                                          _tokenSource.Token);
                }

                return(new Tuple <T, CMPStorageError>(fetchedResultInfo.Result as T, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <T, CMPStorageError>(null, error));
            }
        }
        public async Task <Tuple <bool, CMPStorageError> > CreateTableAsync(string tableNameString,
                                                                            CMPTableStorageOptions tableStorageOptions =
                                                                            null)
        {
            if (_cloudTableClient == null)
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(new Tuple <bool, CMPStorageError>(false, null));
                }

                var couldCreate = false;
                if (tableStorageOptions == null)
                {
                    couldCreate = await tableReference.CreateIfNotExistsAsync();
                }
                else
                {
                    couldCreate = await tableReference.CreateIfNotExistsAsync(tableStorageOptions.TableRequestOptions,
                                                                              tableStorageOptions.OperationContext,
                                                                              _tokenSource.Token);
                }

                return(new Tuple <bool, CMPStorageError>(true, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
        }
        public async Task <List <Tuple <T, CMPStorageError> > > DeleteRowsAsync <T>(string tableNameString,
                                                                                    string partitionKeyString,
                                                                                    CMPTableStorageOptions
                                                                                    tableStorageOptions = null)
            where T : CMPTableStorageModel, new()
        {
            if (_cloudTableClient == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(tableNameString) == true)
            {
                return(null);
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(null);
                }

                string filterConditionString = TableQuery.GenerateFilterCondition(KPartitionKeystring,
                                                                                  QueryComparisons.Equal,
                                                                                  partitionKeyString);

                var fetchedResultsInfo = await FetchForFilterAsync <T>(tableReference, filterConditionString);

                var fetchedResultsList = fetchedResultsInfo.Item1;

                if ((fetchedResultsList == null) || (fetchedResultsList.Count == 0))
                {
                    return(null);
                }

                var deleteResultsInfoList = new List <Tuple <T, CMPStorageError> >();
                var deleteTasksArray      = fetchedResultsList.Select(async(T deleteModel) =>
                {
                    var deleteResultInfo = await DeleteRowAsync <T>(tableNameString, deleteModel, tableStorageOptions);
                    deleteResultsInfoList.Add(deleteResultInfo);
                }).ToArray();

                await Task.WhenAll(deleteTasksArray);

                return(deleteResultsInfoList);
            }
            catch (StorageException exception)
            {
                var error     = CMPStorageError.CreateErrorFromException(exception);
                var errorInfo = new Tuple <T, CMPStorageError>(null, error);
                return(new List <Tuple <T, CMPStorageError> >()
                {
                    errorInfo
                });
            }
            catch (ArgumentException exception)
            {
                var error     = CMPStorageError.CreateErrorFromException(exception);
                var errorInfo = new Tuple <T, CMPStorageError>(null, error);
                return(new List <Tuple <T, CMPStorageError> >()
                {
                    errorInfo
                });
            }
        }
        public async Task <Tuple <T, CMPStorageError> > DeleteRowAsync <T>(string tableNameString, T deleteModel,
                                                                           CMPTableStorageOptions tableStorageOptions =
                                                                           null)
            where T : CMPTableStorageModel, new()
        {
            if (_cloudTableClient == null)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            if (string.IsNullOrEmpty(tableNameString) == true)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            if (deleteModel == null)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(new Tuple <T, CMPStorageError>(null, null));
                }

                var retrieveOperation = TableOperation.Retrieve <T>(deleteModel.PartitionKey, deleteModel.RowKey);
                var retrieveResult    = await tableReference.ExecuteAsync(retrieveOperation);

                var retrievedModel = retrieveResult?.Result as T;
                if (retrievedModel == null)
                {
                    return(new Tuple <T, CMPStorageError>(null, null));
                }

                var deleteOperation = TableOperation.Delete(retrievedModel);

                TableResult deleteResult = null;
                if (tableStorageOptions == null)
                {
                    deleteResult = await tableReference.ExecuteAsync(deleteOperation);
                }
                else
                {
                    deleteResult = await tableReference.ExecuteAsync(deleteOperation,
                                                                     tableStorageOptions.TableRequestOptions,
                                                                     tableStorageOptions.OperationContext,
                                                                     _tokenSource.Token);
                }

                return(new Tuple <T, CMPStorageError>(deleteResult?.Result as T, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <T, CMPStorageError>(null, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <T, CMPStorageError>(null, error));
            }
        }
        public async Task <Tuple <List <T>, CMPStorageError> > InsertRowsInBatchAsync <T>(List <T> insertModelsList,
                                                                                          string tableNameString,
                                                                                          CMPTableStorageOptions
                                                                                          tableStorageOptions = null)
            where T : CMPTableStorageModel, new()

        {
            if (_cloudTableClient == null)
            {
                return(new Tuple <List <T>, CMPStorageError>(null, null));
            }

            if (string.IsNullOrEmpty(tableNameString) == true)
            {
                return(new Tuple <List <T>, CMPStorageError>(null, null));
            }

            if ((insertModelsList == null) || (insertModelsList.Count == 0) || (_cloudTableClient == null))
            {
                return(new Tuple <List <T>, CMPStorageError>(null, null));
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(new Tuple <List <T>, CMPStorageError>(null, null));
                }

                var insertInBatchOperation = new TableBatchOperation();
                foreach (var insertModel in insertModelsList)
                {
                    insertInBatchOperation.Insert(insertModel);
                }

                IList <TableResult> insertInBatchResult = null;
                if (tableStorageOptions == null)
                {
                    insertInBatchResult = await tableReference.ExecuteBatchAsync(insertInBatchOperation);
                }
                else
                {
                    insertInBatchResult = await tableReference.ExecuteBatchAsync(insertInBatchOperation,
                                                                                 tableStorageOptions.TableRequestOptions,
                                                                                 tableStorageOptions.OperationContext,
                                                                                 _tokenSource.Token);
                }

                var insertResultsList = insertInBatchResult.Select((TableResult insertResult) =>
                {
                    return(insertResult?.Result as T);
                }).ToList();

                return(new Tuple <List <T>, CMPStorageError>(insertResultsList, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <List <T>, CMPStorageError>(null, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <List <T>, CMPStorageError>(null, error));
            }
        }
        public async Task <Tuple <T, CMPStorageError> > InsertRowAsync <T>(T insertModel, string tableNameString,
                                                                           CMPTableStorageOptions tableStorageOptions =
                                                                           null)
            where T : CMPTableStorageModel, new()
        {
            if (_cloudTableClient == null)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            if (string.IsNullOrEmpty(tableNameString) == true)
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            if ((insertModel == null) || (_cloudTableClient == null))
            {
                return(new Tuple <T, CMPStorageError>(null, null));
            }

            try
            {
                var tableReference = _cloudTableClient.GetTableReference(tableNameString);
                if (tableReference == null)
                {
                    return(new Tuple <T, CMPStorageError>(null, null));
                }

                var         insertOperation = TableOperation.Insert(insertModel);
                TableResult insertResult    = null;

                if (tableStorageOptions == null)
                {
                    insertResult = await tableReference.ExecuteAsync(insertOperation);
                }
                else
                {
                    insertResult = await tableReference.ExecuteAsync(insertOperation,
                                                                     tableStorageOptions.TableRequestOptions,
                                                                     tableStorageOptions.OperationContext,
                                                                     _tokenSource.Token);
                }

                CMPStorageError error = null;
                if (insertResult.HttpStatusCode != 200)
                {
                    error = CMPStorageError.CreateError(insertResult.HttpStatusCode.ToString(), string.Empty);
                }

                return(new Tuple <T, CMPStorageError>(insertResult.Result as T,
                                                      error));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <T, CMPStorageError>(null, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <T, CMPStorageError>(null, error));
            }
        }