另一种使用Azure表存储的方法?(An alternative way to use Azure Table Storage?)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
另一种使用Azure表存储的方法?(An alternative way to use Azure Table Storage?)

我想用表格存储一个这样的实体:

public class MyEntity { public String Text { get; private set; } public Int32 SomeValue { get; private set; } public MyEntity(String text, Int32 someValue) { Text = text; SomeValue = someValue; } }

但这不可能,因为ATS需要

无参数构造函数 所有属性公开和读/写。 从TableServiceEntity继承;

前两项是我不想做的两件事。 我为什么要让任何人可以改变一些应该只读的数据? 或者以不一致的方式创建这种对象(那么.ctor的作用是什么?),或者甚至最糟糕的是,改变PartitionKey或RowKey。 为什么我们仍然受到这些反序列化要求的制约?

我不喜欢以这种方式开发软件,我怎样才能使用表格存储库,使我可以序列化和反序列化自己的对象? 我认为,只要从TableServiceEntity继承的对象不应该是一个问题。

到目前为止,我必须保存一个对象,但我不知道如何检索它:

Message m = new Message("message XXXXXXXXXXXXX"); CloudTableClient tableClient = account.CreateCloudTableClient(); tableClient.CreateTableIfNotExist("Messages"); TableServiceContext tcontext = new TableServiceContext(account.TableEndpoint.AbsoluteUri, account.Credentials); var list = tableClient.ListTables().ToArray(); tcontext.AddObject("Messages", m); tcontext.SaveChanges();

有什么方法可以避免这些反序列化要求或获取原始对象吗?

干杯。

I'd like to use for table storage an entity like this:

public class MyEntity { public String Text { get; private set; } public Int32 SomeValue { get; private set; } public MyEntity(String text, Int32 someValue) { Text = text; SomeValue = someValue; } }

But it's not possible, because the ATS needs

Parameterless constructor All properties public and read/write. Inherit from TableServiceEntity;

The first two, are two things I don't want to do. Why should I want that anybody could change some data that should be readonly? or create objects of this kind in a inconsistent way (what are .ctor's for then?), or even worst, alter the PartitionKey or the RowKey. Why are we still constrained by these deserialization requirements?

I don't like develop software in that way, how can I use table storage library in a way that I can serialize and deserialize myself the objects? I think that as long the objects inherits from TableServiceEntity it shouldn't be a problem.

So far I got to save an object, but I don't know how retrieve it:

Message m = new Message("message XXXXXXXXXXXXX"); CloudTableClient tableClient = account.CreateCloudTableClient(); tableClient.CreateTableIfNotExist("Messages"); TableServiceContext tcontext = new TableServiceContext(account.TableEndpoint.AbsoluteUri, account.Credentials); var list = tableClient.ListTables().ToArray(); tcontext.AddObject("Messages", m); tcontext.SaveChanges();

Is there any way to avoid those deserialization requirements or get the raw object?

Cheers.

最满意答案

表格存储的ADO.NET包装器的约束确实有些痛苦。 您也可以采用Lokad.Cloud中实施的Fat Entity方法。 这将为您在实体的序列化方面提供更多的灵活性。

The constraints around that ADO.NET wrapper for the Table Storage are indeed somewhat painful. You can also adopt a Fat Entity approach as implemented in Lokad.Cloud. This will give you much more flexibility concerning the serialization of your entities.

更多推荐

本文发布于:2023-04-13 12:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/31ac48cdc8ac9a12099dd8c2c7a0c7db.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Azure   alternative   Storage   Table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!