Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展
在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity。
我们可以通过这个类,我们无需为每一个表提前声明一个class,也能够对其进行操作。
但在我们要添加Entity到表中的时候会感觉到一点不舒服
以下我所希望使用的该类的方式:
dynamic entity = new DynamicTableEntity();
entity.Name=”sam”;
entity.Gender=true;
…
这样的形式
而实际代码却是
DynamicTableEntity entity1 = new DynamicTableEntity();
Dictionary<string, EntityProperty> data1 = new Dictionary<string, EntityProperty>();
data1.Add("Name", new EntityProperty("Sam"));
data1.Add("Gender", new EntityProperty(true));
data1.Add("Age", new EntityProperty(18));
entity1.Properties = data1;
entity1.PartitionKey = "Partition1";
entity1.RowKey = "1";
UserDataList.Add(entity1);
身为一个以dynamic开头的类居然不能够用dynamic关键字来初始化它,在实例化一个DynamicTableEntity类的时候居然要如此麻烦,实在让人不爽。
所以, 经过一番研究,发现了一个可以支持动态扩展的类,为了与DynamicTableEntity区分开来,所以取名DynamicObjectTableEntity。它实现了DynamicTableEntity相同的功能,而且继承了DynamicObject,从而具有动态语言的特性,更加方便 使用。
而且动态语言扩展一直以来除了在linq中使用比较多以外,其它地方基本看不到。这里正好通过对dynamicTableEntity的改造可以加深对动态语言扩展的理解。
实现代码如下:
public class DynamicObjectTableEntity : DynamicObject,ITableEntity
{
#region DynamicTableEntity's code
// Methods
public DynamicObjectTableEntity()
{
this.Properties = new Dictionary<string, EntityProperty>();
} public DynamicObjectTableEntity(string partitionKey, string rowKey) : this(partitionKey, rowKey, DateTimeOffset.MinValue, null, new Dictionary<string, EntityProperty>())
{
} public DynamicObjectTableEntity(string partitionKey, string rowKey, string etag, IDictionary<string, EntityProperty> properties) : this(partitionKey, rowKey, DateTimeOffset.MinValue, etag, properties)
{
} internal DynamicObjectTableEntity(string partitionKey, string rowKey, DateTimeOffset timestamp, string etag, IDictionary<string, EntityProperty> properties)
{
//CommonUtility.AssertNotNull("partitionKey", partitionKey);
//CommonUtility.AssertNotNull("rowKey", rowKey);
//CommonUtility.AssertNotNull("properties", properties);
this.PartitionKey = partitionKey;
this.RowKey = rowKey;
this.Timestamp = timestamp;
this.ETag = etag;
this.Properties = properties;
} public void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
{
this.Properties = properties;
} public IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
{
return this.Properties;
} // Properties
public string ETag { get; set; } public EntityProperty this[string key]
{
get
{
return this.Properties[key];
}
set
{
this.Properties[key] = value;
}
} public string PartitionKey { get; set; } public IDictionary<string, EntityProperty> Properties { get; set; } public string RowKey { get; set; } public DateTimeOffset Timestamp { get; set; } #endregion #region override DynamicObject's mehtods
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!Properties.ContainsKey(binder.Name))
Properties.Add(binder.Name, ConvertToEntityProperty(binder.Name, null));
result = Properties[binder.Name];
return true;
} public override bool TrySetMember(SetMemberBinder binder, object value)
{
EntityProperty property = ConvertToEntityProperty(binder.Name, value); if (Properties.ContainsKey(binder.Name))
Properties[binder.Name] = property;
else
Properties.Add(binder.Name, property); return true;
} /// <summary>
/// Convert object value to EntityProperty.
/// </summary>
private EntityProperty ConvertToEntityProperty(string key, object value)
{
if (value == null) return new EntityProperty((string)null);
if (value.GetType() == typeof(byte[]))
return new EntityProperty((byte[])value);
if (value.GetType() == typeof(bool))
return new EntityProperty((bool)value);
if (value.GetType() == typeof(DateTimeOffset))
return new EntityProperty((DateTimeOffset)value);
if (value.GetType() == typeof(DateTime))
return new EntityProperty((DateTime)value);
if (value.GetType() == typeof(double))
return new EntityProperty((double)value);
if (value.GetType() == typeof(Guid))
return new EntityProperty((Guid)value);
if (value.GetType() == typeof(int))
return new EntityProperty((int)value);
if (value.GetType() == typeof(long))
return new EntityProperty((long)value);
if (value.GetType() == typeof(string))
return new EntityProperty((string)value);
throw new Exception("This value type" + value.GetType() + " for " + key);
throw new Exception(string.Format("This value type {0} is not supported for {1}", key));
} /// <summary>
/// Get the edm type, if the type is not a edm type throw a exception.
/// </summary>
private Type GetType(EdmType edmType)
{
switch (edmType)
{
case EdmType.Binary:
return typeof(byte[]);
case EdmType.Boolean:
return typeof(bool);
case EdmType.DateTime:
return typeof(DateTime);
case EdmType.Double:
return typeof(double);
case EdmType.Guid:
return typeof(Guid);
case EdmType.Int32:
return typeof(int);
case EdmType.Int64:
return typeof(long);
case EdmType.String:
return typeof(string);
default: throw new TypeLoadException(string.Format("not supported edmType:{0}", edmType));
}
} #endregion
}
在使用了如下类之后,我们就可以用我预期的代码来操作table 了
代码如下:
static void Main(string[] args)
{
var connectionString = "DefaultEndpointsProtocol=https;AccountName=[Account];AccountKey=[Key]";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
try
{
var client = storageAccount.CreateCloudTableClient();
var table = client.GetTableReference("ShortMessages");
table.CreateIfNotExists();
dynamic entity = new DynamicObjectTableEntity("default", DateTime.Now.ToShortTimeString()); entity.Name = "pete";
entity.Message = "Hello";
table.Execute(TableOperation.Insert(entity));
Console.WriteLine("insert successfully!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
示例参考MSDN:http://code.msdn.microsoft.com/Dynamic-TableServiceEntity-151d661f
Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展的更多相关文章
- Windows Azure Table storage 之 动态Table类 DynamicTableEntity
在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示. public class CustomerEntity : ...
- Azure Table storage 基本用法 -- Azure Storage 之 Table
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table,其中的 Table 就是本文的主角 Azure Tabl ...
- 自定义 Azure Table storage 查询过滤条件
本文是在Azure Table storage 基本用法一文的基础上,介绍如何自定义 Azure Table storage 的查询过滤条件.如果您还不太清楚 Azure Table storage ...
- Windows Azure Table Storage 解决 Guid 查询问题
在使用 Windows Azure Table Storage 的 CloudTableClient 对Azure 进行数据查询时,会发现在自定义类的Guid类型始终无法去成功查询出数据,对比发现 G ...
- Azure Table Storage(一) : 简单介绍
Azure Table Storage是什么: Azure Table Storage是隶属于微软Azure Storage这个大服务下的一个子服务, 这个服务在Azure上算是老字号了, 个人大概在 ...
- Azure 基础:Table storage
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table.其中的 Table 就是本文的主角 Azure Tabl ...
- Azure 基础:自定义 Table storage 查询条件
本文是在 <Azure 基础:Table storage> 一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件.如果您还不太清楚 Azure Table s ...
- [Windows Azure] How to use the Table Storage Service
How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...
- Azure Storage 系列(四)在.Net 上使用Table Storage
一,引言 今天我们就不多说废话了,直接进入正题,Azure Table Storage.开始内容之前,我们先介绍一下Azure Table Storage. 1,什么是Azure Table Stor ...
随机推荐
- 升级WebService图形服务,将K10.2和K10.3写到一个类库,所有服务放在一个类库
问题描述: 平时负责电子政务和图形调用部分,凡是牵涉到图形的都需要调用WebService服务,因此很多工程都需要添加web服务引用,现在WebForm的工程一个是10.2版本,一个是10.3版本,区 ...
- js通过sessionStorage实现的返回上一页
通常,我们都会使用比较熟悉的javascript:history.go(-1)来实现返回上一页,此种方法有一个问题就是返回上一页后不能刷新当前页面,给我们的开发带来了一定的不便,显然有时这种方法就不是 ...
- ubuntu su Authentication failure
这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在终端中输入如下命令: sudo passwd Password:你当前的密码 Enter ...
- css圆角边框
一.CSS3圆角的优点 传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点: * 减少维护的工作量.图片文件的生成.更新. ...
- parentNode,offsetParent
元素.parentNode : 父节点 只读 属性 当前节点的父级节点 没有兼容性问题 可放心使用 <!DOCTYPE HTML> <html> <head> ...
- mimikatz不反弹读取密码
有些时候无法反弹shell执行mimikatz,虽然可以用procdump导出lsass的内存dump文件,之后本地获取明文密码,但多少有点麻烦,其实mimikatz也支持命令行直接导出 mimika ...
- zju(8)串口通信实验
1.实验目的 1.学习和掌握linux下串口的操作方法以及应用程序的编写: 二.实验内容 1.编写EduKit-IV实验箱Linux操作系统下串口的应用程序,运行时只需要将串口线的一端连接到开发板的c ...
- Web前端开发基础 第四课(CSS元素分类)
元素分类 在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <di ...
- 使用花生壳6.5客户端FTP设置
1.打开FTP客户端—选项—参数选择 2.设置为主动模式(PORT) 3.连接FTP服务器 4.FTP连接成功
- 示例-创建表格-指定行列&删除表格的行和列
<body> <script type="text/javascript"> /* *上面的方法和你麻烦. *既然操作的是表格, *那么最方便的方式就是使用 ...