- azurelight_sourcecode.zip
- Azurelight
- Azure
- Azurelight.sln
- Azurelight
- Azurelight
- Azurelight.ccproj
- Azurelight_WebRole
- ServiceConfiguration.cscfg
- ServiceDefinition.csdef
- Azurelight_WebRole
- AzurelightNav
|
// DataContext.cs
//
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data.Services.Client;
using Microsoft.Azure.StorageClient;
namespace Microsoft.Azure.Linq {
public abstract class DataContext {
private static readonly Dictionary<string, StorageAccountInfo> _accountInfoMap =
new Dictionary<string, StorageAccountInfo>();
private static readonly HashSet<Type> _initializedEntitySets = new HashSet<Type>();
private string _connectionStringName;
private StorageAccountInfo _accountInfo;
private TableStorageDataServiceContext _dataServiceContext;
private string _partitionKey;
private int _retryCount;
private TimeSpan _retryDelay;
protected DataContext(string connectionStringName) {
if (String.IsNullOrEmpty(connectionStringName)) {
throw new ArgumentNullException("connectionStringName");
}
_connectionStringName = connectionStringName;
_retryDelay = RetryPolicies.StandardMinBackoff;
}
internal TableStorageDataServiceContext DataServiceContext {
get {
if (_dataServiceContext == null) {
_accountInfo = GetAccountInfo(_connectionStringName);
_dataServiceContext = new TableStorageDataServiceContext(_accountInfo);
_dataServiceContext.SaveChangesDefaultOptions = SaveChangesOptions.ReplaceOnUpdate;
// Initialize the Azure storage, by creating entity sets based on the set
// of properties defined on this DataContext instance.
// Do it once per appdomain instance, as this incurs the overhead of HTTP
// requests sent to the store.
Type modelType = this.GetType();
if (_initializedEntitySets.Contains(modelType) == false) {
TableStorage.CreateTablesFromModel(modelType, _accountInfo);
_initializedEntitySets.Add(modelType);
}
}
return _dataServiceContext;
}
}
public string PartitionKey {
get {
return _partitionKey;
}
set {
_partitionKey = value;
}
}
public int RetryCount {
get {
return _retryCount;
}
set {
_retryCount = value;
}
}
public TimeSpan RetryDelay {
get {
return _retryDelay;
}
set {
_retryDelay = value;
}
}
protected internal virtual IQueryable<TEntity> CreateEntityQuery<TEntity>(string name) where TEntity : Entity, new() {
IQueryable<TEntity> query = DataServiceContext.CreateQuery<TEntity>(name);
if (String.IsNullOrEmpty(PartitionKey) == false) {
query = query.Where(entity => entity.PartitionKey == PartitionKey);
}
return query;
}
private static StorageAccountInfo GetAccountInfo(string connectionStringName) {
if (String.IsNullOrEmpty(connectionStringName)) {
throw new ArgumentNullException("connectionStringName");
}
StorageAccountInfo accountInfo;
if (_accountInfoMap.TryGetValue(connectionStringName, out accountInfo)) {
return accountInfo;
}
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
if (connectionString == null) {
throw new ArgumentOutOfRangeException("connectionStringName");
}
string name = null;
string key = null;
string uri = "http://table.core.windows.net";
bool pathStyleUri = false;
string[] parts = connectionString.ConnectionString.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts) {
int index = part.IndexOf('=');
if (index >= 0) {
string partName = part.Substring(0, index);
string partValue = part.Substring(index + 1);
if (String.CompareOrdinal(partName, "name") == 0) {
name = partValue;
continue;
}
else if (String.CompareOrdinal(partName, "key") == 0) {
key = partValue;
continue;
}
else if (String.CompareOrdinal(partName, "uri") == 0) {
uri = partValue;
continue;
}
else if (String.CompareOrdinal(partName, "pathStyleUri") == 0) {
pathStyleUri = Boolean.Parse(partValue);
continue;
}
}
throw new ArgumentException("Invalid connection string.");
}
accountInfo = new StorageAccountInfo(new Uri(uri, UriKind.Absolute), pathStyleUri, name, key);
_accountInfoMap[connectionStringName] = accountInfo;
return accountInfo;
}
public void SubmitChanges() {
string partitionKey = PartitionKey;
if (String.IsNullOrEmpty(partitionKey) == false) {
foreach (EntityDescriptor entityDescriptor in DataServiceContext.Entities) {
Entity entity = (Entity)entityDescriptor.Entity;
if (String.IsNullOrEmpty(entity.PartitionKey)) {
entity.PartitionKey = partitionKey;
}
}
}
if (_retryCount == 0) {
DataServiceContext.SaveChanges();
}
else {
DataServiceContext.RetryPolicy = RetryPolicies.RetryN(_retryCount, _retryDelay);
DataServiceContext.SaveChangesWithRetries();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.