LCOV - code coverage report
Current view: top level - Azure/Cqrs.Azure.Storage/DataStores - TableStorageDataStore.cs Hit Total Coverage
Test: doc-coverage.info Lines: 1 8 12.5 %
Date: 2017-07-26

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="cdmdotnet Limited">
       4             : // //   Copyright cdmdotnet Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections.Generic;
      11             : using cdmdotnet.Logging;
      12             : using Cqrs.Azure.BlobStorage;
      13             : using Cqrs.Azure.BlobStorage.DataStores;
      14             : using Cqrs.Entities;
      15             : using Microsoft.WindowsAzure.Storage;
      16             : using Microsoft.WindowsAzure.Storage.Table;
      17             : 
      18             : namespace Cqrs.Azure.Storage.DataStores
      19             : {
      20             :         public class TableStorageDataStore<TData>
      21             :                 : BlobStorage.DataStores.TableStorageDataStore<TData>
      22             :                 where TData : Entity
      23           0 :         {
      24             :                 /// <summary>
      25             :                 /// Initializes a new instance of the <see cref="BlobStorage"/> class using the specified container.
      26             :                 /// </summary>
      27           1 :                 public TableStorageDataStore(ILogger logger, ITableStorageDataStoreConnectionStringFactory tableStorageDataStoreConnectionStringFactory)
      28             :                         : base(logger, tableStorageDataStoreConnectionStringFactory)
      29             :                 {
      30             :                 }
      31             : 
      32             :                 #region Overrides of TableStorageStore<TData>
      33             : 
      34           0 :                 protected override ITableEntity CreateTableEntity(TData data)
      35             :                 {
      36             :                         var tableEntity = new EntityTableEntity<TData>(data);
      37             :                         //Flatten object of type TData and convert it to EntityProperty Dictionary
      38             :                         Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data, new OperationContext());
      39             : 
      40             :                         // Create a DynamicTableEntity and set its PK and RK
      41             :                         DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(tableEntity.PartitionKey, tableEntity.RowKey)
      42             :                         {
      43             :                                 Properties = flattenedProperties
      44             :                         };
      45             : 
      46             :                         return dynamicTableEntity;
      47             :                 }
      48             : 
      49           0 :                 protected override TableOperation GetUpdatableTableEntity(TData data)
      50             :                 {
      51             :                         return TableOperation.Retrieve<DynamicTableEntity>(data.GetType().FullName, data.Rsn.ToString("N"));
      52             :                 }
      53             : 
      54             :                 #endregion
      55             : 
      56             :                 #region Overrides of TableStorageStore<EntityTableEntity<TData>,TData>
      57             : 
      58           0 :                 protected override ITableEntity ReplaceValues(TableResult retrievedResult, EntityTableEntity<TData> data)
      59             :                 {
      60             :                         ITableEntity tableEntity = (ITableEntity)retrievedResult.Result;
      61             :                         // Events aren't updated
      62             :                         var dynamicTableEntity = tableEntity as DynamicTableEntity;
      63             :                         if (dynamicTableEntity == null)
      64             :                         {
      65             :                                 base.ReplaceValues(retrievedResult, data);
      66             :                                 return tableEntity;
      67             :                         }
      68             : 
      69             :                         //Flatten object of type TData and convert it to EntityProperty Dictionary
      70             :                         Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data.Entity, new OperationContext());
      71             :                         dynamicTableEntity.Properties = flattenedProperties;
      72             : 
      73             :                         return dynamicTableEntity;
      74             :                 }
      75             : 
      76           0 :                 public override EntityTableEntity<TData> GetByKeyAndRow(Guid rsn)
      77             :                 {
      78             :                         TableOperation searchQuery = TableOperation.Retrieve<DynamicTableEntity>(typeof(TData).FullName, rsn.ToString("N"));
      79             : 
      80             :                         TableResult searchResult = ReadableSource.Execute(searchQuery);
      81             : 
      82             :                         var dynamicTableEntity = searchResult.Result as DynamicTableEntity;
      83             :                         if (dynamicTableEntity == null)
      84             :                                 return base.GetByKeyAndRow(rsn);
      85             : 
      86             :                         //Convert the DynamicTableEntity back to original complex object.
      87             :                         TData result = EntityPropertyConverter.ConvertBack<TData>(dynamicTableEntity.Properties, new OperationContext());
      88             :                         return new EntityTableEntity<TData>(result);
      89             :                 }
      90             : 
      91           0 :                 public override IEnumerable<EntityTableEntity<TData>> GetByKey()
      92             :                 {
      93             :                         // Create the table query.
      94             :                         var rangeQuery = Collection.Where
      95             :                         (
      96             :                                 TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, StorageStore<object, object>.GetSafeStorageKey(typeof(TData).FullName))
      97             :                         );
      98             : 
      99             :                         IEnumerable<EntityTableEntity<TData>> results = ReadableSource.ExecuteQuery(rangeQuery);
     100             : 
     101             :                         return results;
     102             :                 }
     103             : 
     104             :                 #endregion
     105             : 
     106           0 :                 public override void Update(TData data)
     107             :                 {
     108             :                         DynamicTableEntity dynamicTableEntity = CreateTableEntity(data) as DynamicTableEntity;
     109             :                         if (dynamicTableEntity == null)
     110             :                         {
     111             :                                 base.Update(data);
     112             :                                 return;
     113             :                         }
     114             :                         //Convert the DynamicTableEntity back to original complex object.
     115             :                         TData result = EntityPropertyConverter.ConvertBack<TData>(dynamicTableEntity.Properties, new OperationContext());
     116             :                         Update(new EntityTableEntity<TData>(result));
     117             :                 }
     118             :         }
     119             : }

Generated by: LCOV version 1.10