抽象Azure表服务实体
我想抽象我的Azure TableServiceEntities的实现,这样我就有了一个实体,它将接受任何类型的对象,并将该对象的属性用作TableServiceEntity中的属性 所以我的基本对象是抽象Azure表服务实体,azure,azure-storage,azure-table-storage,Azure,Azure Storage,Azure Table Storage,我想抽象我的Azure TableServiceEntities的实现,这样我就有了一个实体,它将接受任何类型的对象,并将该对象的属性用作TableServiceEntity中的属性 所以我的基本对象是 public class SomeObject { [EntityAttribute(PartitionKey=true)] public string OneProperty {get; set:} [EntityAttribute(RowKey=true)]
public class SomeObject
{
[EntityAttribute(PartitionKey=true)]
public string OneProperty {get; set:}
[EntityAttribute(RowKey=true)]
public string TwoProperty {get; set;}
public string SomeOtherProperty {get;set;}
}
public class SomeEntity<T> : TableServiceEntity
{
public SomeEntity(T obj)
{
foreach (var propertyInfo in properties)
{
object[] attributes = propertyInfo.GetCustomAttributes(typeof (DataObjectAttributes), false);
foreach (var attribute in attributes)
{
DataObjectAttributes doa = (DataObjectAttributes) attribute;
if (doa.PartitionKey)
PartitionKey = propertyInfo.Name;
}
}
}
}
公共类SomeObject
{
[EntityAttribute(PartitionKey=true)]
公共字符串OneProperty{get;set:}
[EntityAttribute(RowKey=true)]
公共字符串属性{get;set;}
公共字符串SomeOtherProperty{get;set;}
}
公共类SomeEntity:TableServiceEntity
{
公共实体(T obj)
{
foreach(属性中的var propertyInfo)
{
object[]attributes=propertyInfo.GetCustomAttributes(typeof(DataObjectAttributes),false);
foreach(属性中的var属性)
{
DataObjectAttributes doa=(DataObjectAttributes)属性;
if(doa.PartitionKey)
PartitionKey=propertyInfo.Name;
}
}
}
}
然后我可以在这样的上下文中访问实体
var objects =
(from entity in context.CreateQuery<SomeEntity>("SomeEntities") select entity);
var entityList = objects.ToList();
foreach (var obj in entityList)
{
var someObject = new SomeObject();
SomeObject.OneProperty = obj.OneProperty;
SomeObject.TwoProperty = obj.TwoProperty;
}
var对象=
(从context.CreateQuery(“SomeEntities”)中的实体选择实体);
var entityList=objects.ToList();
foreach(entityList中的var obj)
{
var someObject=new someObject();
SomeObject.OneProperty=obj.OneProperty;
SomeObject.TwoProperty=obj.TwoProperty;
}
这看起来并没有那么困难,但我感觉我一直在寻找太多可能的解决方案,只是设法让自己感到困惑
感谢您的指点。我认为源代码模仿了您的尝试,但对Azure表存储的不同方法有着深刻的理论基础
我在Lucifure Stash的F#中编写了另一个Azure表存储客户端,它支持许多抽象,包括持久化字典对象。Lucifure Stash还支持大于64K的大型数据列、数组和列表、枚举、开箱即用序列化、用户定义的变形、公共和私有属性以及字段等。 可通过NuGet.com或通过NuGet.com免费提供给个人使用
您试图实现的是,任何实体的单一泛型类都可以在Lucifure Stash中通过使用字典类型的[StashPool]属性来实现 我写了一篇关于表存储上下文的博文,通过指定实体类型来定义实体。也许它可以帮助您您似乎仍然希望使用具体类型。因此,SomeEntity有点冗余。实际上,TableServiceEntity已经是一个抽象类了。您可以从TableServiceEntity派生SomeObject。根据我的经验,这不会给您的场景带来任何问题 此外,即使使用自定义SomeEntity,也无法在最后一段代码中消除对具体SomeObject类的依赖 致以最良好的祝愿
Xu Ming。我已经看过Lokad,但看起来他们使用自定义序列化来实现我在设置实体时想要做的事情。