Graham
Graham

Reputation: 7802

How can I model key/value attributes in Azure Table Storage?

I'd appreciate some input on how this should be implemented in Azure Table Storage. I have read the design guides a few times but this scenario doesn't seem to be covered.

The generic use case is to attach 0...n key/value pairs to an object in the system, where all possible values are predefined.

Let's say our application is used by a number of schools to show students all of the available classes they can choose in the current semester. Before the start of the semester, the administrator logs in to the system to create the new classes and attach attributes to them so the students can filter the list of classes based on their preferences. The administrator manages all valid attributes and valid attribute values.

The first class added to the new semester is "Arts & Crafts" and the administrator sees that they need to add a new attribute to indicate that there is an additional cost to taking this class. They go into the attribute management page and add "Additional Cost" as an attribute with three possible attribute values: "No Additional Cost", "Under $20", and "Over $20". They then go and attach the "Additional Cost" attribute to the new "Arts & Crafts" class and assign a value of "Over $20".

Upvotes: 0

Views: 602

Answers (2)

Dogu Arslan
Dogu Arslan

Reputation: 3384

You can use the Merge method from the Storage SDK to add additional properties to the existing entity, in this case "Additional Cost" attribute will be appended to the "Arts & Crafts" entity.

On the client side, you can use DynamicTableEntity as your most flexible option which is a key/value dictionary with Partition and Row keys.

Upvotes: 0

Fei Han
Fei Han

Reputation: 27793

It seems that you’d like to dynamically define and add properties to the table when you insert the entities, please refer to this sample.

Besides, you could store these three possible attribute values ("No Additional Cost", "Under $20", and "Over $20") in a list and serialize the list to a JSON string and store it as entity’s attribute value.

List<string> values = new List<string>() { "No Additional Cost", "Under $20", "Over $20" };

var jsonSerialiser = new JavaScriptSerializer();
string jsonval = jsonSerialiser.Serialize(values);

Upvotes: 1

Related Questions