Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 11 Jul 2009 CDDL
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).
Azurelight_SourceCode.zip
Azurelight
Azure
DomainServices
Linq
Properties
StorageClient
Azurelight
Azurelight
Azurelight.ccproj
Azurelight_WebRole
ServiceConfiguration.cscfg
ServiceDefinition.csdef
Azurelight_WebRole
App_Data
Azurelight.mdf
Azurelight_log.ldf
App_GlobalResources
System.Web.Silverlight.dll
ClientBin
Global.asax
Model
Properties
Service
AzurelightNav
Command
Control
Generated_Code
Model
Properties
Resources
SilverlightCairngorm
Business
Command
Control
Model
Properties
Views
// EntitySet.cs
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Microsoft.Azure.Linq {

    public class EntitySet<TEntity> : IQueryable<TEntity> where TEntity : Entity, new() {

        private DataContext _context;
        private string _name;

        private List<TEntity> _list;

        private IQueryable<TEntity> _query;

        public EntitySet(DataContext context, string name) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
            if (String.IsNullOrEmpty(name)) {
                throw new ArgumentNullException("name");
            }

            _context = context;
            _name = name;
        }

        public EntitySet(List<TEntity> list) {
            if (list == null) {
                throw new ArgumentNullException("list");
            }
            _list = list;
        }

        private IQueryable<TEntity> Query {
            get {
                if (_query == null) {
                    if (_context != null) {
                        _query = _context.CreateEntityQuery<TEntity>(_name);
                    }
                    else {
                        _query = _list.AsQueryable();
                    }
                }
                return _query;
            }
        }

        private static void ApplyEntityState(TEntity entity, Dictionary<string, object> state) {
            foreach (KeyValuePair<string, object> propValue in state) {
                PropertyInfo prop = entity.GetType().GetProperty(propValue.Key);
                prop.SetValue(entity, propValue.Value, null);
            }
        }

        public void Attach(TEntity entity) {
            if (_context != null) {
                _context.DataServiceContext.AttachTo(_name, entity);
            }
            else {
                _list.Add(entity);
            }
        }

        public void Attach(TEntity entity, string etag) {
            if (_context != null) {
                _context.DataServiceContext.AttachTo(_name, entity, etag);
            }
            else {
                _list.Add(entity);
            }
        }

        public void Attach(TEntity entity, TEntity originalEntity) {
            if (_context != null) {
                Dictionary<string, object> state = RevertEntityState(entity, originalEntity);
                _context.DataServiceContext.AttachTo(_name, entity);

                ApplyEntityState(entity, state);
                _context.DataServiceContext.UpdateObject(entity);
            }
            else {
                _list.Add(entity);
            }
        }

        public void Attach(TEntity entity, TEntity originalEntity, string etag) {
            if (_context != null) {
                Dictionary<string, object> state = RevertEntityState(entity, originalEntity);
                _context.DataServiceContext.AttachTo(_name, entity, etag);

                ApplyEntityState(entity, state);
                _context.DataServiceContext.UpdateObject(entity);
            }
            else {
                _list.Add(entity);
            }
        }

        public void DeleteOnSubmit(TEntity entity) {
            if (_context != null) {
                _context.DataServiceContext.DeleteObject(entity);
            }
            else {
                _list.Remove(entity);
            }
        }

        public bool Detach(TEntity entity) {
            if (_context != null) {
                return _context.DataServiceContext.Detach(entity);
            }
            else {
                return _list.Remove(entity);
            }
        }

        public void InsertOnSubmit(TEntity entity) {
            if (_context != null) {
                _context.DataServiceContext.AddObject(_name, entity);
            }
            else {
                _list.Add(entity);
            }
        }

        private static Dictionary<string, object> RevertEntityState(TEntity entity, TEntity originalEntity) {
            Dictionary<string, object> state = new Dictionary<string, object>();
            PropertyInfo[] props = entity.GetType().GetProperties();

            foreach (PropertyInfo p in props) {
                if ((String.CompareOrdinal(p.Name, "PartitionKey") == 0) ||
                    (String.CompareOrdinal(p.Name, "RowKey") == 0) ||
                    (String.CompareOrdinal(p.Name, "Timestamp") == 0)) {
                    continue;
                }

                state[p.Name] = p.GetValue(entity, null);
                p.SetValue(entity, p.GetValue(originalEntity, null), null);
            }

            return state;
        }

        #region IEnumerable Members
        IEnumerator IEnumerable.GetEnumerator() {
            return Query.GetEnumerator();
        }
        #endregion

        #region IEnumerable<TEntity> Members
        IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator() {
            return Query.GetEnumerator();
        }
        #endregion

        #region IQueryable Members
        Type IQueryable.ElementType {
            get {
                return Query.ElementType;
            }
        }

        Expression IQueryable.Expression {
            get {
                return Query.Expression;
            }
        }

        IQueryProvider IQueryable.Provider {
            get {
                return Query.Provider;
            }
        }
        #endregion
    }
}

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)

Share

About the Author

Modesty Zhang
Technical Lead
United States United States
Tech Lead of large scale consumer facing software offerings, specializing in Web and Mobile application architecting and development.
 
Specialties:
Web App/ iOS / Cocoa Touch / HTML5 / CSS3 / Ajax / jQuery / jQuery Mobile / jQuery UI / Node.js / Rich JavaScript Application / RESTful Web Services / Java EE 6 / Java 7 / PHP / Ruby on Rails / and Windows / .NET / RIA / Flex / Flash / Silverlight / Software Architecting / Front End Design and Development

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 12 Jul 2009
Article Copyright 2009 by Modesty Zhang
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid