Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Steel Thread Artifacts
/*
*
* Borrowed from PnP
*
*/
public abstract class AzureQueueMessage
{
public string Id { get; set; }
public string PopReceipt { get; set; }
public int DequeueCount { get; set; }
}
/*
*
* Borrowed from PnP
*
*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using System;
using System.Globalization;
public abstract class Entity : TableServiceEntity
{
protected Entity()
{
}
protected Entity(string partitionKey, string rowKey)
: base(partitionKey, rowKey)
{
}
}
/*
*
* Pattern borrowed from PnP
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public static class EntityExtensions
{
public static RegistrationEntity ToRegistrationEntity(this Registration registration)
{
var re = new RegistrationEntity("Registrations", DateTime.UtcNow.Ticks.ToString());
re.Email = registration.Email;
re.UserHandle = registration.UserHandle;
return re;
}
public static RegistrationEntity ToRegistrationEntity(this NewRegistrationMessage registration)
{
var re = new RegistrationEntity("Registrations", DateTime.UtcNow.Ticks.ToString());
re.Email = registration.Email;
re.UserHandle = registration.UserHandle;
re.PartitionKey = string.Format("R_{0}", (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString());
re.RowKey = string.Format("{0}_{1}", re.Email, re.PartitionKey);
return re;
}
}
using System.Runtime.Serialization;
[DataContract]
public class NewRegistrationMessage : AzureQueueMessage
{
[DataMember]
public string RegistrationId {get;set;}
[DataMember]
public string UserHandle { get; set; }
[DataMember]
public string Email { get; set; }
}
using System;
public class Registration
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserHandle { get; set; }
public string Id { get; set; }
public DateTime TimeStamp { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.Globalization;
public class RegistrationContext : TableServiceContext
{
public RegistrationContext(CloudStorageAccount account)
:this(account.TableEndpoint.ToString(), account.Credentials)
{
}
public RegistrationContext(string baseAddress, StorageCredentials credentials)
:base(baseAddress, credentials)
{
ResolveType = ResolveEntityType;
}
public void AddRegistration(RegistrationEntity registration)
{
var account = CloudConfiguration.GetStorageAccount(AzureConnectionStrings.DataConnection);
TableServiceContext tableServiceContext =
new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);
tableServiceContext.AddObject(AzureStorageNames.RegistrationTable, registration);
tableServiceContext.SaveChanges();
}
private static Type ResolveEntityType(string name)
{
var tableName = name.Split(new[] { '.' }).Last();
switch (tableName)
{
case AzureStorageNames.RegistrationTable:
return typeof(RegistrationEntity);
}
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
"Could not resolve the table name '{0}' to a known entity type.",
name));
}
}
using System;
using Microsoft.WindowsAzure.StorageClient;
using System.Web.Script.Serialization;\
using Microsoft.WindowsAzure;
public class RegistrationRepository : IRegistrationRepository
{
public void Push(Registration registration)
{
RegistrationEntity entity = registration.ToRegistrationEntity();
this.SaveRegistration(registration);
}
public void Push(RegistrationEntity registration)
{
var context = new RegistrationContext(CloudConfiguration.GetStorageAccount(AzureConnectionStrings.DataConnection));
context.AddRegistration(registration);
}
private void SaveRegistration(Registration registration)
{
// Get client
CloudQueueClient client = GetQueueClient();
// Create the two queues
CloudQueue registrationQueue = client.GetQueueReference(AzureStorageNames.NewRegistrationMessage);
registrationQueue.CreateIfNotExist();
var registrationMessage = new NewRegistrationMessage();
registrationMessage.Id = StorageKey.Now.InvertedTicks;
string serializedMessage = new JavaScriptSerializer().Serialize(registration);
registrationQueue.AddMessage(new CloudQueueMessage(serializedMessage));
}
static CloudQueueClient GetQueueClient()
{
CloudStorageAccount account = CloudConfiguration.GetStorageAccount(AzureConnectionStrings.DataConnection);
CloudQueueClient client = account.CreateCloudQueueClient();
client.RetryPolicy = RetryPolicies.NoRetry();
return client;
}
}
public class RegistrationEntity : Entity
{
public const string RowKeyPrefix = "R_";
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserHandle { get; set; }
public RegistrationEntity() : base()
{
}
public RegistrationEntity(string partitionKey, string rowKey)
: base(partitionKey, rowKey)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment