Created
May 31, 2011 18:56
-
-
Save onefloridacoder/1001056 to your computer and use it in GitHub Desktop.
Steel Thread Artifacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Borrowed from PnP | |
* | |
*/ | |
public abstract class AzureQueueMessage | |
{ | |
public string Id { get; set; } | |
public string PopReceipt { get; set; } | |
public int DequeueCount { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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