Skip to content

Commit

Permalink
ElCamino.Azure.Data.Tables refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmelendez committed Apr 27, 2022
1 parent 4b50e1e commit d809f1a
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 246 deletions.
8 changes: 8 additions & 0 deletions ElCamino.AspNetCore.Identity.AzureTable.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElCamino.AspNetCore.Identit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElCamino.AspNetCore.Identity.AzureTable.Model", "src\ElCamino.AspNetCore.Identity.AzureTable.Model\ElCamino.AspNetCore.Identity.AzureTable.Model.csproj", "{C90FB0E3-5BE2-41E7-9DA3-9083C7700F91}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElCamino.Azure.Data.Tables", "src\ElCamino.Azure.Data.Tables\ElCamino.Azure.Data.Tables.csproj", "{DAF57676-8534-4A62-BC7B-317753F2A275}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +60,12 @@ Global
{C90FB0E3-5BE2-41E7-9DA3-9083C7700F91}.Release|Any CPU.Build.0 = Release|Any CPU
{C90FB0E3-5BE2-41E7-9DA3-9083C7700F91}.Signed|Any CPU.ActiveCfg = Debug|Any CPU
{C90FB0E3-5BE2-41E7-9DA3-9083C7700F91}.Signed|Any CPU.Build.0 = Debug|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Release|Any CPU.Build.0 = Release|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Signed|Any CPU.ActiveCfg = Debug|Any CPU
{DAF57676-8534-4A62-BC7B-317753F2A275}.Signed|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 3 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
identityazuretable
ElCamino.Azure.Data.Tables
==================

This project provides a high performance cloud solution for ASP.NET Identity Core using Azure Table storage replacing the Entity Framework / MSSQL provider.
Azure Table Storage odata query building and operators from the older Azure Storage SDKs and some other async, mapping, and batch helpers and/or extensions.

[![Build Status](https://dev.azure.com/elcamino/Azure%20OpenSource/_apis/build/status/IdentityAzureTableCore?branchName=master)](https://dev.azure.com/elcamino/Azure%20OpenSource/_build/latest?definitionId=4&branchName=master)
[![NuGet Badge](https://buildstats.info/nuget/ElCamino.AspNetCore.Identity.AzureTable)](https://www.nuget.org/packages/ElCamino.AspNetCore.Identity.AzureTable/)
[![NuGet Badge](https://buildstats.info/nuget/ElCamino.AspNet.Identity.AzureTable)](https://www.nuget.org/packages/ElCamino.AspNet.Identity.AzureTable/)
[![NuGet Badge](https://buildstats.info/nuget/ElCamino.Azure.Data.Tables)](https://www.nuget.org/packages/ElCamino.Azure.Data.Tables/)

Project site at https://dlmelendez.github.io/identityazuretable/.

Identity Core latest template
```
dotnet new --install ElCamino.AspNetCore.Identity.AzureTable.Templates
#MVC Template
dotnet new mvc-id-azure-tables
#Razor Pages Template
dotnet new rzp-id-azure-tables
```

Identity Core 3.x (uses PageModel - latest) - Use ElCamino.AspNetCore.Identity.AzureTable, sample mvc app: https://github.com/dlmelendez/identityazuretable/tree/master/sample/samplemvccore4

Identity Core 2.x (uses PageModel - latest) - Use ElCamino.AspNetCore.Identity.AzureTable, sample mvc app: https://github.com/dlmelendez/identityazuretable/tree/master/sample/samplemvccore3

Identity Core 2.x (uses MVC - older) - Use ElCamino.AspNetCore.Identity.AzureTable, sample mvc app: https://github.com/dlmelendez/identityazuretable/tree/master/sample/samplemvccore2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyTitle>Azure Table Storage Provider for ASP.NET Identity Core</AssemblyTitle>
<Authors>David Melendez</Authors>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<AssemblyName>ElCamino.AspNetCore.Identity.AzureTable</AssemblyName>
<AssemblyOriginatorKeyFile>../../tools/Key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down Expand Up @@ -57,6 +57,7 @@

<ItemGroup>
<ProjectReference Include="..\ElCamino.AspNetCore.Identity.AzureTable.Model\ElCamino.AspNetCore.Identity.AzureTable.Model.csproj" />
<ProjectReference Include="..\ElCamino.Azure.Data.Tables\ElCamino.Azure.Data.Tables.csproj" />
</ItemGroup>


Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// MIT License Copyright 2020 (c) David Melendez. All rights reserved. See License.txt in the project root for license information.
using System;
using ElCamino.AspNetCore.Identity.AzureTable.Model;
using Azure.Data.Tables;
using ElCamino.AspNetCore.Identity.AzureTable.Model;

namespace ElCamino.AspNetCore.Identity.AzureTable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,83 @@
// MIT License Copyright 2020 (c) David Melendez. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;

namespace ElCamino.AspNetCore.Identity.AzureTable.Helpers
{
/// <summary>
/// Used to instantiate multiple TableBatchOperations when the
/// TableOperation maximum is reached on a single TableBatchOperation
/// </summary>
public class BatchOperationHelper
{

private readonly Dictionary<string, List<TableTransactionAction>> _batches = new();

private readonly TableClient _table;
public BatchOperationHelper(TableClient table)
{
_table = table;
}

public virtual void AddEntities<T>(IEnumerable<T> entities) where T : class, ITableEntity, new()
{
foreach(T entity in entities)
{
AddEntity<T>(entity);
}
}
public virtual void AddEntity<T>(T entity) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(TableTransactionActionType.Add, entity));
}
public virtual void DeleteEntity(string partitionKey, string rowKey, ETag ifMatch = default)
{
GetCurrent(partitionKey).Add(new TableTransactionAction(TableTransactionActionType.Delete, new TableEntity(partitionKey, rowKey),ifMatch));
}

public virtual async Task<IEnumerable<Response>> SubmitBatchAsync(CancellationToken cancellationToken = default)
{
ConcurrentBag<Response> bag = new ConcurrentBag<Response>();
List<Task> batches = new List<Task>(_batches.Count);
foreach(KeyValuePair<string, List<TableTransactionAction>> kv in _batches)
{
batches.Add(_table.SubmitTransactionAsync(kv.Value, cancellationToken)
.ContinueWith((result) =>
{
foreach (var r in result.Result.Value)
{
bag.Add(r);
}
}, cancellationToken));
}
await Task.WhenAll(batches).ConfigureAwait(false);
Clear();
return bag;
}

public virtual void UpdateEntity<T>(T entity, ETag ifMatch, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(mode == TableUpdateMode.Merge? TableTransactionActionType.UpdateMerge : TableTransactionActionType.UpdateReplace, entity, ifMatch));
}

public virtual void UpsertEntity<T>(T entity, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(mode == TableUpdateMode.Merge ? TableTransactionActionType.UpsertMerge : TableTransactionActionType.UpsertReplace, entity));
}

public void Clear()
{
_batches.Clear();
}

private List<TableTransactionAction> GetCurrent(string partitionKey)
{
if(!_batches.ContainsKey(partitionKey))
{
_batches.Add(partitionKey, new List<TableTransactionAction>());
}

return _batches[partitionKey];
}
}
}
// MIT License Copyright 2020 (c) David Melendez. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Concurrent;

namespace Azure.Data.Tables
{
/// <summary>
/// Used to instantiate multiple TableBatchOperations when the
/// TableOperation maximum is reached on a single TableBatchOperation
/// </summary>
public class BatchOperationHelper
{
private readonly Dictionary<string, List<TableTransactionAction>> _batches = new();

private readonly TableClient _table;

public BatchOperationHelper(TableClient table)
{
_table = table;
}

public virtual void AddEntities<T>(IEnumerable<T> entities) where T : class, ITableEntity, new()
{
foreach(T entity in entities)
{
AddEntity<T>(entity);
}
}
public virtual void AddEntity<T>(T entity) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(TableTransactionActionType.Add, entity));
}
public virtual void DeleteEntity(string partitionKey, string rowKey, ETag ifMatch = default)
{
GetCurrent(partitionKey).Add(new TableTransactionAction(TableTransactionActionType.Delete, new TableEntity(partitionKey, rowKey),ifMatch));
}

public virtual async Task<IEnumerable<Response>> SubmitBatchAsync(CancellationToken cancellationToken = default)
{
ConcurrentBag<Response> bag = new ConcurrentBag<Response>();
List<Task> batches = new List<Task>(_batches.Count);
foreach(KeyValuePair<string, List<TableTransactionAction>> kv in _batches)
{
batches.Add(_table.SubmitTransactionAsync(kv.Value, cancellationToken)
.ContinueWith((result) =>
{
foreach (var r in result.Result.Value)
{
bag.Add(r);
}
}, cancellationToken));
}
await Task.WhenAll(batches).ConfigureAwait(false);
Clear();
return bag;
}

public virtual void UpdateEntity<T>(T entity, ETag ifMatch, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(mode == TableUpdateMode.Merge? TableTransactionActionType.UpdateMerge : TableTransactionActionType.UpdateReplace, entity, ifMatch));
}

public virtual void UpsertEntity<T>(T entity, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
GetCurrent(entity.PartitionKey).Add(new TableTransactionAction(mode == TableUpdateMode.Merge ? TableTransactionActionType.UpsertMerge : TableTransactionActionType.UpsertReplace, entity));
}

public void Clear()
{
_batches.Clear();
}

private List<TableTransactionAction> GetCurrent(string partitionKey)
{
if(!_batches.ContainsKey(partitionKey))
{
_batches.Add(partitionKey, new List<TableTransactionAction>());
}

return _batches[partitionKey];
}
}
}
Loading

0 comments on commit d809f1a

Please sign in to comment.