Azure TableEntity - 挂钩读/写操作

时间:2016-09-22 10:21:36

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

Backgroung

假设我有一个Azure表实体

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

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

两个函数

目前,在每次插入之前,我都会检查LongString的长度以及是否< 1> 64KB,我设置LongString = Compress(LongString)IsCompressed = true。 每次Azure获取操作后都相反。

我想隐藏整个代码中的压缩选项,并让压缩和解压缩自包含在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;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

可能性类似于以下代码:

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您不需要任何复杂性,只需创建计算属性即可写入表存储。而且您也不需要多个属性。因此,不需要具有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);
       }
    }
}