【Azure Services Platform Step by Step-第9篇】Windows Azure Storage概览中,我们已经讨论过Table Storage的作用和特点。本篇将以搭建简单的聊天室为例,演示如果使用最简单的代码,将C#实体类(Entity)直接存入Table Storage中,彻底告别SQL Server 200x和ORM工具。

最终效果: (已部署到云端的Demo :http://ibm.cloudapp.net/ChatMain.aspx)

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

 

首先让我们一起回顾一下Table Storage的结构。

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

每行都是一个独立的Entity。

Partition Key和RowKey起到了类似于主键的作用,它们用来标识Entity,同时也可以在实际使用中作为分类查询条件。

TimeStamp属性(上图没画出)是每行都默认有的,它自动记录该行数据最后更新的时间。

 

接下来我们来看看StorageClient中是怎样使用TableStorage的

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage 

看着这图,单看文件名,觉得很奇怪吧? Blob和Queue都使用了Rest来实现,唯独Table没有一个对应REST的类。那它是怎么做的呢?

查看源代码可以发现,原来,它使用的是System.Data.Services.Client里的DataServiceQuery和DataServiceContext这两个ADO.NET 数据服务的关键类来实现的。拿TableStorageDataServiceContext类来说,它继承自DataServiceContext,或者说,它把DataServiceContext封装成了Azure版!

(对ADO.NET数据服务Client不了解的朋友请查阅http://msdn.microsoft.com/zh-cn/library/system.data.services.client.dataservicecontext.aspx

呵呵,不管怎么样,我们使用方便就好了。

了解完了原理,我们来进入正题吧。

 

第一步:

在VS008中新建Web Cloud Service、配置ServiceConfiguration.cscfg、ServiceDefinition.csdef ,添加对StorageClient项目的引用。这里不再重复了,请直接参考上一篇的内容或者本文篇末附件源代码。

直接使用上一节里的ServiceConfiguration.cscfg和ServiceDefinition.csdef也行,因为账户信息是一样的。

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage 

 

第二步:

拖入控件,制作简单的登录界面和主聊天界面。这不是重点也不是难点,请大家直接参看本文篇末附件源代码。其实聊天室和留言吧的区别不大,使用ASP.NET Ajax的Timer和UpdatePanel让它每两秒刷新一次就行。

 

 

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

 

 

第三步:

建立一个Message实体类。

与传统代码或由ORM工具生成的实体代码不同,它需要继承自TableStorageEntity.

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage public class Message : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
    }

 

 

第四步:

建立一个MessageDataServiceContext实体类。该类继承自TableStorageDataServiceContext,也就是间接继承自DataServiceContext。它的作用是获得一个对数据服务上下文的引用。此外,定义一个公共属性Messages:可返回所有Message类型实体; 增加AddMessage方法:将Message实体存入Table Storage。

 

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage public class MessageDataServiceContext : TableStorageDataServiceContext
    }

 

第五步:

取实体:

 

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage  //初始化账户信息
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
                StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage               
// 自动根据实体类的结构生成Table
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
                TableStorage.CreateTablesFromModel(typeof(MessageDataServiceContext), accountInfo);
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage               
// 获取数据服务上下文的引用
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
                MessageDataServiceContext context = new MessageDataServiceContext(accountInfo);
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage               
// 取前150条Message实体,作为数据源绑定到messageList中。
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage
                IQueryable<Message> data = context.Messages.Take(150);
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage            
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage               
this.messageList.DataSource = data;
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage             
【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage               
this.messageList.DataBind();

 

存入实体:

 

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage  protected void SubmitButton_Click(object sender, EventArgs e)
        }

 

OK,搞定了!

F5一下看看运行效果吧!

 

            ______————————____忐忑不安的分割线______————————______

通过REST方法获得实体的真实相貌:

【Azure Services Platform Step by Step-第12篇】实现Windows Azure聊天室-使用Table Storage

可以清楚地看到,这个实体有5个属性。其中有3个是默认必须有的属性,只有Body和Name是我们在实体类里自己定义的。

 

【附件】

传说中的StorageClient:StorageClient.rar

本篇源代码:AzureChatRoom.rar (已与前面章节的源代码整合)

相关文章:

  • 2021-12-04
  • 2021-11-03
  • 2021-06-24
  • 2022-01-03
  • 2021-10-06
  • 2021-05-29
猜你喜欢
  • 2021-11-18
  • 2022-02-28
  • 2021-11-19
  • 2022-02-05
  • 2021-08-16
相关资源
相似解决方案