Skip to content

Commit

Permalink
Added read support based on the new Azure SDK!
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Sep 3, 2022
1 parent d89fded commit d7c68fb
Show file tree
Hide file tree
Showing 40 changed files with 946 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
<None Include="..\LICENSE" Pack="true" PackagePath="" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,52 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;

namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public interface IStorageContext : IDisposable
{
void AddAttributeMapper();

void AddAttributeMapper(Type type);

void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, String tableName);

IStorageContext CreateChildContext();

IStorageContext EnableAutoCreateTable();

Task InsertOrReplaceAsync<T>(T model) where T : new();
bool IsAutoCreateTableEnabled();

void SetDelegate(IStorageContextDelegate delegateModel);

IStorageContextDelegate GetDelegate();

Task<T> QueryAsync<T>(string partitionKey, string rowKey, int maxItems = 0) where T : new();
Task InsertOrReplaceAsync<T>(T model) where T : class, new();

Task<IQueryable<T>> QueryAsync<T>(string partitionKey, int maxItems = 0) where T : new();
IStorageContextQueryWithPartitionKey<T> Query<T>() where T : class, new();

Task<IQueryable<T>> QueryAsync<T>(string partitionKey, IEnumerable<QueryFilter> queryFilters, int maxItems = 0)
where T : new();
Task<T> QueryAsync<T>(string partitionKey, string rowKey, int maxItems = 0) where T : class, new();

Task<IQueryable<T>> QueryAsync<T>(int maxItems = 0) where T : new();
Task<IEnumerable<T>> QueryAsync<T>(string partitionKey, int maxItems = 0) where T : class, new();

Task<IEnumerable<T>> QueryAsync<T>(string partitionKey, IEnumerable<QueryFilter> queryFilters, int maxItems = 0)
where T : class, new();

Task<IEnumerable<T>> QueryAsync<T>(int maxItems = 0) where T : class, new();

Task DeleteAsync<T>(T model) where T : new();
Task DeleteAsync<T>(T model) where T : class, new();

Task DeleteAsync<T>(IEnumerable<T> models, bool allowMultiPartionRemoval = false) where T : new();
Task DeleteAsync<T>(IEnumerable<T> models, bool allowMultiPartionRemoval = false) where T : class, new();

void SetTableNamePrefix(string tableNamePrefix);

void OverrideTableName<T>(string table) where T : new();
void OverrideTableName<T>(string table) where T : class, new();

Task MergeOrInsertAsync<T>(IEnumerable<T> models) where T : new();
Task MergeOrInsertAsync<T>(IEnumerable<T> models) where T : class, new();

Task MergeOrInsertAsync<T>(T model) where T : new();
Task MergeOrInsertAsync<T>(T model) where T : class, new();

Task CreateTableAsync<T>(bool ignoreErrorIfExists = true);

Expand All @@ -44,7 +57,6 @@ Task<IQueryable<T>> QueryAsync<T>(string partitionKey, IEnumerable<QueryFilter>

Task DropTableAsync<T>(bool ignoreErrorIfNotExists = true);

void DropTable<T>(bool ignoreErrorIfNotExists = true);

void DropTable<T>(bool ignoreErrorIfNotExists = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace CoreHelpers.WindowsAzure.Storage.Table
{
public interface IStorageContextDelegate
{
void OnQuerying(Type modelType, string partitionKey, string rowKey, int maxItems, bool isContinuationQuery);
void OnQuerying(Type modelType, string filter, int maxItems, bool isContinuationQuery);

void OnQueryed(Type modelType, string partitionKey, string rowKey, int maxItems, bool isContinuationQuery, Exception e);
void OnQueryed(Type modelType, string filter, int maxItems, bool isContinuationQuery, Exception e);

void OnStoring(Type modelType, nStoreOperation storaeOperationType);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
{
public interface IStorageContextQueryNow<T>
{
Task<IEnumerable<T>> Now();
}

public interface IStorageContextQueryWithFilter<T> : IStorageContextQueryNow<T>
{
IStorageContextQueryWithFilter<T> Filter(string filter);
IStorageContextQueryWithFilter<T> Filter(IEnumerable<QueryFilter> filters);

IStorageContextQueryWithFilter<T> LimitTo(int maxItems);
}

public interface IStorageContextQueryWithRowKey<T> : IStorageContextQueryWithFilter<T>
{
IStorageContextQueryWithFilter<T> GetItem(string rowKey);
}

public interface IStorageContextQueryWithPartitionKey<T> : IStorageContextQueryNow<T>
{
IStorageContextQueryWithRowKey<T> InPartition(string partitionKey);

IStorageContextQueryWithRowKey<T> InAllPartitions();
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public class QueryFilter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public enum QueryFilterOperator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public enum QueryFilterType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public enum nStoreOperation
{
insertOperation,
insertOrReplaceOperation,
mergeOperation,
mergeOrInserOperation,
delete
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions
{
public static class StorageContextExtensions
Expand All @@ -8,7 +9,7 @@ public static string BuildTableContext()
return $"T{Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8)}";
}

public static string SetTableContext(this StorageContext context)
public static string SetTableContext(this IStorageContext context)
{
var contextValue = BuildTableContext();
context.SetTableNamePrefix(contextValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
[Collection("Sequential")]
public class ITS001StoreWithStaticEntityMapper
{
private readonly ITestEnvironment env;
private readonly IStorageContext _rootContext;

public ITS001StoreWithStaticEntityMapper(ITestEnvironment env)
public ITS001StoreWithStaticEntityMapper(IStorageContext context)
{
this.env = env;
}
_rootContext = context;

}

[Fact]
public async Task VerifyTableExists()
{
using (var scp = new StorageContext(env.ConnectionString))
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();

// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), new DynamicTableEntityMapper() { TableName = "UserProfiles", PartitionKeyFormat = "Contact", RowKeyFormat = "Contact" });
scp.AddEntityMapper(typeof(UserModel), "Contact", "Contact", "UserProfiles");

Assert.False(await scp.ExistsTableAsync<UserModel>());

Expand All @@ -39,7 +40,7 @@ public async Task VerifyTableExists()
[Fact]
public async Task VerifyStaticEntityMapperOperations()
{
using (var scp = new StorageContext(env.ConnectionString))
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
Expand All @@ -52,10 +53,10 @@ public async Task VerifyStaticEntityMapperOperations()
user.Contact = $"{user.Contact}.{runId}";

// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), new DynamicTableEntityMapper() { TableName = "UserProfiles", PartitionKeyFormat = "Contact", RowKeyFormat = "Contact" });
scp.AddEntityMapper(typeof(UserModel), tableName: "UserProfiles", partitionKeyFormat: "Contact", rowKeyFormat: "Contact" );

// execute the store operation
using (var sc = new StorageContext(scp))
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<UserModel>();
Expand All @@ -71,7 +72,7 @@ public async Task VerifyStaticEntityMapperOperations()
// verify if the model was created
var result = await scp.QueryAsync<UserModel>();
Assert.NotNull(result);
Assert.Equal(1, result.Count());
Assert.Single(result);
Assert.Equal("Egon", result.First().FirstName);
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal($"[email protected].{runId}", result.First().Contact);
Expand All @@ -85,7 +86,7 @@ public async Task VerifyStaticEntityMapperOperations()
[Fact]
public async Task VerifyVirtualKeysPOCOModel()
{
using (var scp = new StorageContext(env.ConnectionString))
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
Expand All @@ -94,10 +95,10 @@ public async Task VerifyVirtualKeysPOCOModel()
var vpmodel = new VirtualPartitionKeyDemoModelPOCO() { Value1 = "abc", Value2 = "def", Value3 = "ghi" };

// configure the entity mapper
scp.AddEntityMapper(typeof(VirtualPartitionKeyDemoModelPOCO), new DynamicTableEntityMapper() { TableName = "VirtualPartitionKeyDemoModelPOCO", PartitionKeyFormat = "{{Value1}}-{{Value2}}", RowKeyFormat = "{{Value2}}-{{Value3}}" });
scp.AddEntityMapper(typeof(VirtualPartitionKeyDemoModelPOCO), tableName: "VirtualPartitionKeyDemoModelPOCO", partitionKeyFormat: "{{Value1}}-{{Value2}}", rowKeyFormat: "{{Value2}}-{{Value3}}");

// execute the store operation
using (var sc = new StorageContext(scp))
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<VirtualPartitionKeyDemoModelPOCO>();
Expand All @@ -109,7 +110,7 @@ public async Task VerifyVirtualKeysPOCOModel()
// verify if the model was created
var result = await scp.QueryAsync<VirtualPartitionKeyDemoModelPOCO>();
Assert.NotNull(result);
Assert.Equal(1, result.Count());
Assert.Single(result);
Assert.Equal("abc", result.First().Value1);
Assert.Equal("def", result.First().Value2);
Assert.Equal("ghi", result.First().Value3);
Expand All @@ -123,7 +124,7 @@ public async Task VerifyVirtualKeysPOCOModel()
[Fact]
public async Task VerifyStatsDelegate()
{
using (var scp = new StorageContext(env.ConnectionString))
using (var scp = _rootContext.CreateChildContext())
{
// set the tablename context
scp.SetTableContext();
Expand All @@ -140,10 +141,10 @@ public async Task VerifyStatsDelegate()
user.Contact = $"{user.Contact}.{runId}";

// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), new DynamicTableEntityMapper() { TableName = "UserProfiles", PartitionKeyFormat = "Contact", RowKeyFormat = "Contact" });
scp.AddEntityMapper(typeof(UserModel), tableName: "UserProfiles", partitionKeyFormat: "Contact", rowKeyFormat: "Contact");

// execute the store operation
using (var sc = new StorageContext(scp))
using (var sc = scp.CreateChildContext())
{
// ensure the table exists
await sc.CreateTableAsync<UserModel>();
Expand All @@ -168,9 +169,4 @@ public async Task VerifyStatsDelegate()
}
}
}
}



// PUT DElegates in another test

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
[Collection("Sequential")]
public class ITS002StoreWithAttributeMapper
{
private readonly ITestEnvironment env;
private readonly IStorageContext _rootContext;

public ITS002StoreWithAttributeMapper(ITestEnvironment env)
public ITS002StoreWithAttributeMapper(IStorageContext context)
{
this.env = env;
_rootContext = context;
}

[Fact]
public async Task VerifyAttributeMapper()
{
using (var storageContext = new StorageContext(env.ConnectionString))
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();
Expand All @@ -39,7 +39,7 @@ public async Task VerifyAttributeMapper()

// query all
var result = await storageContext.QueryAsync<UserModel2>();
Assert.Equal(1, result.Count());
Assert.Single(result);
Assert.Equal("Egon", result.First().FirstName);
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal("[email protected]", result.First().Contact);
Expand All @@ -48,7 +48,7 @@ public async Task VerifyAttributeMapper()
await storageContext.DeleteAsync<UserModel2>(result);
result = await storageContext.QueryAsync<UserModel2>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
Assert.Empty(result);

await storageContext.DropTableAsync<UserModel2>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
[Collection("Sequential")]
public class ITS003StoreWithAttributeMapperManualRegistration
{
private readonly ITestEnvironment env;
private readonly IStorageContext _rootContext;

public ITS003StoreWithAttributeMapperManualRegistration(ITestEnvironment env)
public ITS003StoreWithAttributeMapperManualRegistration(IStorageContext context)
{
this.env = env;
_rootContext = context;
}

[Fact]
public async Task VerifyManualRegistration()
{
using (var storageContext = new StorageContext(env.ConnectionString))
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();
Expand All @@ -45,15 +45,15 @@ public async Task VerifyManualRegistration()

// query all
var result = await storageContext.QueryAsync<UserModel2>();
Assert.Equal(1, result.Count());
Assert.Single(result);
Assert.Equal("Egon", result.First().FirstName);
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal("[email protected]", result.First().Contact);


var resultVP = await storageContext.QueryAsync<VirtualPartKeyDemoModel>();
Assert.NotNull(resultVP);
Assert.Equal(1, resultVP.Count());
Assert.Single(resultVP);
Assert.Equal("abc", resultVP.First().Value1);
Assert.Equal("def", resultVP.First().Value2);
Assert.Equal("ghi", resultVP.First().Value3);
Expand All @@ -62,12 +62,12 @@ public async Task VerifyManualRegistration()
await storageContext.DeleteAsync<UserModel2>(result);
result = await storageContext.QueryAsync<UserModel2>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
Assert.Empty(result);

await storageContext.DeleteAsync<VirtualPartKeyDemoModel>(resultVP);
resultVP = await storageContext.QueryAsync<VirtualPartKeyDemoModel>();
Assert.NotNull(resultVP);
Assert.Equal(0, resultVP.Count());
Assert.Empty(resultVP);

await storageContext.DropTableAsync<UserModel2>();
await storageContext.DropTableAsync<VirtualPartKeyDemoModel>();
Expand Down
Loading

0 comments on commit d7c68fb

Please sign in to comment.