- azurelight_sourcecode.zip
- Azurelight
- Azure
- Azurelight.sln
- Azurelight
- Azurelight
- Azurelight.ccproj
- Azurelight_WebRole
- ServiceConfiguration.cscfg
- ServiceDefinition.csdef
- Azurelight_WebRole
- AzurelightNav
|
// AzureContext.cs
//
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Web.DomainServices;
using Microsoft.Azure.Linq;
namespace Microsoft.Azure.DomainServices {
[MetadataProvider(typeof(AzureTypeDescriptionProvider))]
public abstract class AzureDomainService<TDataContext> : DomainService where TDataContext : DataContext, new() {
private TDataContext _dataContext;
private Dictionary<object, string> _etagMap;
protected TDataContext DataContext {
get {
if (_dataContext == null) {
_dataContext = CreateDataContext();
_dataContext.PartitionKey = GetPartitionKey();
}
return _dataContext;
}
}
public override bool UsesUtcDateTimes {
get {
return true;
}
}
protected virtual TDataContext CreateDataContext() {
return new TDataContext();
}
protected override void PersistChangeSet(ChangeSet changeSet) {
_dataContext.SubmitChanges();
InitializeETagMap();
}
protected string GetETag(object entity) {
if (entity == null) {
throw new ArgumentNullException("entity");
}
string etag = null;
if ((_etagMap != null) && _etagMap.TryGetValue(entity, out etag)) {
return etag;
}
return null;
}
protected virtual string GetPartitionKey() {
return null;
}
private void InitializeETagMap() {
foreach (EntityDescriptor entityDescriptor in DataContext.DataServiceContext.Entities) {
_etagMap[entityDescriptor.Entity] = entityDescriptor.ETag;
}
}
protected override void OnDeserializingObject(object obj, IDictionary<string, object> state) {
if (_etagMap == null) {
_etagMap = new Dictionary<object, string>();
}
object etagValue = null;
if (state.TryGetValue("_ETag", out etagValue)) {
string etag = etagValue as string;
if (String.IsNullOrEmpty(etag) == false) {
_etagMap[obj] = etag;
}
}
base.OnDeserializingObject(obj, state);
}
protected override void OnSerializingObject(object obj, IDictionary<string, object> state) {
base.OnSerializingObject(obj, state);
if (_etagMap == null) {
_etagMap = new Dictionary<object, string>();
InitializeETagMap();
}
string etag = null;
if (_etagMap.TryGetValue(obj, out etag)) {
state["_ETag"] = etag;
}
}
}
}
|
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.