Azure TableEntity, overriding Write & ReadEntity methods

Viewed 2350

I have an Azure Table some enumerations like this

pk   rk   en     fr     de   ...

foo  1  'E-f1' 'F-f1' 'D-f1' ...
foo  2  'E-f2' 'F-f2' 'D-f2' ...

bar  1  'E-b1' 'F-b1' 'D-b1' ...
bar  2  'E-b2' 'F-b2' 'D-b2' ...
bar  3  'E-b3' 'F-b3' 'D-b3' ...

en, fr, de etc... are the language codes, and respectively the column names in the table.

When the user select a language I need to select dropdowns for "foo" and "bar" I need to display dropdowns.

I created a DescriptionEntity, that has a Description that should keep the 'en' or 'fr' or 'de' text value, depending on the current user interface language.

Does the following code replace the Description property with the current language value in the Azure Table and read it respectively, or I misunderstand something (because there is not very clear for the in the documentation)...?

public class DescriptionEntity : TableEntity
{
    public string Description { get; set; }
    private string lang;

    public DescriptionEntity(string lang) {
        this.lang = lang;
    }

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) {
        base.ReadEntity(properties, operationContext);
        if (properties.ContainsKey(this.lang)) {
            this.Description = properties[this.lang].StringValue;
        }
    }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        var x = base.WriteEntity(operationContext);
        var descriptionValue = this.Description;
        if (x.ContainsKey(lang)) {
            x[lang].StringValue = this.Description;
        }
        else {
            x.Add(lang, new EntityProperty(this.Description));
        }
        x.Remove("Description");
        return x;
    }
}
1 Answers
Related