Azure TableEntity - 挂钩读/写操作

Azure TableEntity - Hooking to Read / Write Operations

背景

假设我有一个 Azure Table 实体

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

如果 LongString > 64KB(属性 的 Azure 限制),我想保存压缩的 LongString。 为此,我有 Compress(string)Decompress(string)

两个函数

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

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

问题

是否可以选择在从 azure table 获取和设置实体之前和之后进行自定义操作?像...

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);
            }
        }
    }
}

但问问自己,总是保存压缩字符串是否更容易。

使用 ReadEntityWriteEntity 函数找到了解决方案。

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

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
    {
        base.ReadEntity(properties, operationContext);

        if (IsCompressed)
        {
            LongString = Decompress(LongString);
            IsCompressed = false;
        }
    }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        if (LongString.Length > 64KB)
        {
            LongString = Compress(LongString);
            IsCompressed = true;
        }

        return base.WriteEntity(operationContext);
    }
}

您不需要任何复杂性,只需创建计算属性即可写入 table 存储。而且您也不需要多个属性。因此不需要第二个 属性 带有 IgnoreProperty 属性。我在文本编辑器中输入了以下内容,但这应该可以让您了解。

private string longString;

public bool IsCompressed { get; set; }

public string LongString
{
    get
    {
        return IsCompressed ? DeCompress(longString) : longString;
    }
    set
    {
       if (String.IsNullOrWhiteSpace(value) || value.Length < 64KB)
       {
           IsCompressed = false;
           longString = value;
       }
       else (value.Length > 64KB)
       {
           IsCompressed = true;
           longString = Compress(value);
       }
    }
}