背景
假设我有一个 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/