c# - Azure TableEntity - Hook 读/写操作

标签 c# azure azure-table-storage azure-tablequery

背景

假设我有一个 Azure 表实体

class MyEntity : TableEntity
{
    public string LongString { get; set; }
    public bool IsCompressed { get; set; }
}

如果 LongString > 64KB(Azure 属性限制),我想保存压缩的 LongString。 为此,我有 2 个函数:压缩(字符串)解压缩(字符串)

目前,在每次插入之前,我都会检查 LongString 的长度,如果它 > 64KB,我会设置 LongString = compress(LongString) IsCompressed = true。 每次 Azure get 操作后都会发生相反的情况。

我想在整个代码中隐藏压缩选项,并将压缩和解压缩自身包含在 MyEntity 类中。

问题

是否有一个选项可以在从 azure 表获取和设置实体之前和之后进行自定义操作?类似...

class MyEntity : TableEntity
{
    public string LongString { get; set; }
    public string IsCompressed { get; set; }

    public override void BeforeInsert()
    {
        if (LongString.Length > 64KB)
        {
            LongString = Compress(LongString);
            IsCompressed = true;
        }
    }

    public override void AfterGet()
    {
        if (IsCompressed)
        {
            LongString = Decompress(LongString);
            IsCompressed = false;
        }
    }
}

最佳答案

一种可能性可能类似于以下代码:

public bool IsCompressed;

public string StoredString { get; set; }

private string longString;

[IgnoreProperty] 
public string LongString
{
    get
    {
        if (this.longString == null)
        {
            if (IsCompressed)
            {
                this.longString = DeCompress(StoredString);
            }
            else
            {
                this.longString = StoredString;
            }
        }
        return this.longString;
    }
    set
    {
        if (longString != value)
        {
            this.longString = value;
            if (this.longString.Length > 64KB)
            {
                IsCompressed = true;
                this.StoredString = Compress(this.longString);
            }
        }
    }
}

但是问问自己,总是保存压缩字符串不是更容易吗?

关于c# - Azure TableEntity - Hook 读/写操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636464/

相关文章:

azure - Windows Azure 云服务访问

azure - Azure 事件中心事件属性如何序列化?

azure - 为什么 CosmosDB 在通过 ARM 模板创建文档集合时忽略索引?

azure - 执行 Azure 表存储不区分大小写的查询

Azure 表存储升级

c# - 如何使用 URI 片段在 C# 中签署 XML

c# - 自定义配置部分属性不是配置元素

c# - 无法让模板缓冲区在 OpenTK 中工作,简单的 2D 四边形

c# - 是否有任何库可以从 C# 访问 OLE 结构化存储?

Azure表存储异常: 409 Conflict unexpected?