Click here to Skip to main content
15,884,099 members
Articles / Web Development / CSS

Building a 3-Tier App with Silverlight 3, .NET RIA Services, and Azure Table Storage

Rate me:
Please Sign up or sign in to vote.
4.89/5 (28 votes)
11 Jul 2009CDDL19 min read 164K   1.7K   148  
This article presents the techniques and caveats of building a 3-tire Azure hosted application using Silverlight 3 (presentation tier), .NET RIA services (business logic and data access), and Windows Azure Table (data storage).
// 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.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Technical Lead
United States United States
https://github.com/modesty

https://www.linkedin.com/in/modesty-zhang-9a43771

https://twitter.com/modestyqz

Comments and Discussions