Backgroung
Assuming I have an Azure Table Entity
class MyEntity : TableEntity
{
public string LongString { get; set; }
public bool IsCompressed { get; set; }
}
And if the LongString
> 64KB (Azure limitation for property), I want to save the LongString
compressed.
For this I have 2 functions of Compress(string)
and Decompress(string)
Currently, before every insert I'm checking the length of LongString
and if it's > 64KB, I'm setting LongString = Compress(LongString)
and IsCompressed = true
.
The opposite after every Azure get operation.
I want to hide the option of compression from the entire code and to have the compression and decompression to be self contained in the MyEntity
class.
Question
Is there an option to make custom operation before and after getting and setting entities from azure table? Something like ...
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;
}
}
}
Found a solution using the ReadEntity
and WriteEntity
functions.
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);
}
}