0

背景

假设我有一个 Azure 表实体

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

如果LongString> 64KB(Azure 属性限制),我想保存LongString压缩文件。为此,我有两个功能Compress(string)Decompress(string)

LongString目前,在每次插入之前,我都会LongString = Compress(LongString)检查IsCompressed = true. 每次 Azure 获取操作后都相反。

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

问题

在从 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;
        }
    }
}
4

3 回答 3

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

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

于 2016-09-22T10:42:16.663 回答
1

ReadEntity使用和WriteEntity函数找到了解决方案。

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);
    }
}
于 2016-09-22T11:29:51.463 回答
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);
       }
    }
}
于 2017-03-22T15:29:33.153 回答