Skip to content

@vraposo /gist:6bd987874073c5434bba secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sample Using Count() on Azure TableStorage
void Main()
{
CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey("account", "key"), true);
AuthorizationRepository repository = new AuthorizationRepository(storageAccount);
Random random = new Random();
repository.Context.Count().Dump();
repository.Add(new AuthorizationEntity() { HasBeenUsed = false, PartitionKey = random.Next().ToString(), RowKey = random.Next().ToString()});
repository.Context.Count().Dump();
}
// Define other methods and classes here
/// <summary>
/// Represents the authorization repository.
/// </summary>
public sealed class AuthorizationRepository
{
#region Fields
private AuthorizationRepositoryServiceContext m_context;
#endregion
#region Initializers
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizationRepository"/> class.
/// </summary>
/// <param name="storageAccount">The storage account.</param>
/// <exception cref="E:System.ArgumentNullException">storageAccount is null.</exception>
/// <exception cref="E:Microsoft.WindowsAzure.StorageClient.StorageException">Repository is not available.</exception>
public AuthorizationRepository (CloudStorageAccount storageAccount)
{
if (storageAccount == null)
{
throw new ArgumentNullException("storageAccount");
}
CloudTableClient tableStorage = storageAccount.CreateCloudTableClient();
tableStorage.CreateTableIfNotExist(AuthorizationRepositoryServiceContext.RepositoryName);
this.m_context = new AuthorizationRepositoryServiceContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
}
#endregion
#region Properties
/// <summary>
/// Gets the current repository context.
/// </summary>
public IEnumerable<AuthorizationEntity> Context
{
get
{
return this.m_context.Authorizations;
}
}
#endregion
#region Methods
#region Private
/// <summary>
/// Finds the authorization entity for the specified token.
/// </summary>
/// <param name="token">The token.</param>
/// <param name="hasBeenUsed">if set to <c>true</c>, only searchs for a used one; if set to <c>false</c>, only searchs for an unused one.</param>
/// <returns>
/// The authorization entity for the specified token.<br />
/// -or-<br />
/// <c>null</c> if no authorization entity is found.
/// </returns>
private AuthorizationEntity FindAuthorizationEntity (string token, bool hasBeenUsed)
{
try
{
return this.Context.FirstOrDefault(entry => entry.PartitionKey.Equals(token, StringComparison.InvariantCultureIgnoreCase) && entry.HasBeenUsed == hasBeenUsed);
}
catch (Exception)
{
return null;
}
}
#endregion
#region Public
/// <summary>
/// Finds the authorization entity.
/// </summary>
/// <param name="token">The token.</param>
/// <returns>
/// The authorization entity for the specified token.<br />
/// -or-<br />
/// <c>null</c> if no authorization entity is found.
/// </returns>
public AuthorizationEntity FindAuthorizationEntity (string token)
{
return this.Context.FirstOrDefault(entry => entry.PartitionKey.Equals(token, StringComparison.InvariantCultureIgnoreCase));
}
/// <summary>
/// Finds the used authorization entity for the specified token.
/// </summary>
/// <param name="token">The token.</param>
/// <returns>
/// The authorization entity for the specified token.<br />
/// -or-<br />
/// <c>null</c> if no authorization entity is found.
/// </returns>
public AuthorizationEntity FindUsedAuthorizationEntity (string token)
{
return FindAuthorizationEntity(token, true);
}
/// <summary>
/// Finds the unused authorization entity for the specified token.
/// </summary>
/// <param name="token">The token.</param>
/// <returns>
/// The authorization entity for the specified token.<br />
/// -or-<br />
/// <c>null</c> if no authorization entity is found.
/// </returns>
public AuthorizationEntity FindUnusedAuthorizationEntity (string token)
{
return FindAuthorizationEntity(token, false);
}
/// <summary>
/// Adds the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>
/// <c>true</c> if the entity was added; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="E:System.ArgumentNullException">entity is null.</exception>
/// <exception cref="E:System.ArgumentException">entity.PartitionKey cannot be null or empty.</exception>
/// <exception cref="E:System.ArgumentException">entity.RowKey cannot be null or empty.</exception>
public bool Add (AuthorizationEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
if (entity.PartitionKey.IsNullOrEmptyOrWhitespace())
{
throw new ArgumentException("entity.PartitionKey cannot be null or empty.");
}
if (entity.RowKey.IsNullOrEmptyOrWhitespace())
{
throw new ArgumentException("entity.RowKey cannot be null or empty.");
}
try
{
this.m_context.AddObject(AuthorizationRepositoryServiceContext.RepositoryName, entity);
this.m_context.SaveChangesWithRetries();
return true;
}
catch (Exception)
{ }
return false;
}
/// <summary>
/// Adds the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
/// <returns>
/// <c>true</c> if the entities were added; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="E:System.ArgumentNullException">entities is null.</exception>
/// <exception cref="E:System.ArgumentException">entities cannot be empty.</exception>
/// <exception cref="E:System.ArgumentException">There can't be a entity.PartitionKey null or empty.</exception>
/// <exception cref="E:System.ArgumentException">There can't be a entity.RowKey null or empty.</exception>
public bool AddRange (IEnumerable<AuthorizationEntity> entities)
{
if (entities == null)
{
throw new ArgumentNullException("entities");
}
if (entities.Count(entity => entity != null) == 0)
{
throw new ArgumentException("entities cannot be empty.");
}
if (entities.Count(entity => entity.PartitionKey.IsNullOrEmptyOrWhitespace()) > 0)
{
throw new ArgumentException("There can't be a entity.PartitionKey null or empty.");
}
if (entities.Count(entity => entity.RowKey.IsNullOrEmptyOrWhitespace()) > 0)
{
throw new ArgumentException("There can't be a entity.RowKey null or empty.");
}
try
{
foreach (var entity in entities)
{
this.m_context.AddObject(AuthorizationRepositoryServiceContext.RepositoryName, entity);
}
this.m_context.SaveChangesWithRetries();
return true;
}
catch (Exception)
{ }
return false;
}
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>
/// <c>true</c> if the entity was updated; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="E:System.ArgumentNullException">entity is null.</exception>
/// <exception cref="E:System.ArgumentException">entity.PartitionKey cannot be null or empty.</exception>
/// <exception cref="E:System.ArgumentException">entity.RowKey cannot be null or empty.</exception>
public bool Update (AuthorizationEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
if (entity.PartitionKey.IsNullOrEmptyOrWhitespace())
{
throw new ArgumentException("entity.PartitionKey cannot be null or empty.");
}
if (entity.RowKey.IsNullOrEmptyOrWhitespace())
{
throw new ArgumentException("entity.RowKey cannot be null or empty.");
}
try
{
this.m_context.UpdateObject(entity);
this.m_context.SaveChangesWithRetries();
return true;
}
catch (Exception)
{ }
return false;
}
#endregion
#endregion
#region Events
#endregion
#region Event Handlers
#endregion
}
/// <summary>
/// Represents an authorization repository service context.
/// </summary>
public sealed class AuthorizationRepositoryServiceContext : TableServiceContext
{
#region Fields
public const string RepositoryName = "AuthorizationRepository";
#endregion
#region Initializers
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizationRepositoryServiceContext"/> class.
/// </summary>
/// <param name="baseAddress">The Table service endpoint to use create the service context.</param>
/// <param name="credentials">The account credentials.</param>
public AuthorizationRepositoryServiceContext (string baseAddress, StorageCredentials credentials)
: base(baseAddress, credentials)
{
this.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
}
#endregion
#region Properties
/// <summary>
/// Gets the authorizations available in the repository.
/// </summary>
public IEnumerable<AuthorizationEntity> Authorizations
{
get
{
return this.CreateQuery<AuthorizationEntity>(AuthorizationRepositoryServiceContext.RepositoryName);
}
}
#endregion
#region Methods
#endregion
#region Events
#endregion
#region Event Handlers
#endregion
}
/// <summary>
/// Represents an authorization repository entity.
/// </summary>
public sealed class AuthorizationEntity : TableServiceEntity
{
#region Fields
#endregion
#region Initializers
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizationEntity"/> class.
/// </summary>
public AuthorizationEntity ()
{
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the partition key token of this entity.
/// </summary>
/// <value>
/// The partition key token.
/// </value>
public override string PartitionKey
{
get
{
return base.PartitionKey;
}
set
{
base.PartitionKey = value;
}
}
/// <summary>
/// Gets or sets the row key authorization code of this entity.
/// </summary>
/// <value>
/// The row key authorization code.
/// </value>
public override string RowKey
{
get
{
return base.RowKey;
}
set
{
base.RowKey = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether this entity has been used.
/// </summary>
/// <value>
/// <c>true</c> if this entity has been used; otherwise, <c>false</c>.
/// </value>
public bool HasBeenUsed
{
get;
set;
}
#endregion
#region Methods
#endregion
#region Events
#endregion
#region Event Handlers
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.