Windows Azure Developer Challenge – YouConf – Day 6 (May 4)

More CSS and UI tidy-up. Things are starting to look better now – have a look at the live site to see it coming together.

JSON Serialization

When adding the functionality to delete a speaker, I ran into an issue where I would delete the speaker, but they would not be removed from the actual presentation. Here’s a snippet of the code from the Presentation class:

public IList<Speaker> Speakers { get; set; } 

Now in the Delete method of my speaker controller, I have code like this to delete the speaker:

//Remove the speaker
//Also remove them from any presentations...
foreach (var presentation in conference.Presentations)
    var speaker = presentation.Speakers.FirstOrDefault(x => x.Id == currentSpeaker.Id);
YouConfDataContext.UpsertConference(conferenceHashTag, conference);
return RedirectToAction("Details", "Conference", new { hashTag = conferenceHashTag }); 

Note that line which says Presentation.Speakers.Remove(speaker)… with my default setup this wasn’t actually deleting the speaker, because by default JSON.Net serializes all objects by reference (remember that we’re serializing the entire conference when we save it to table storage then deserializing it on the way back out). This means that the speaker object that I retrieved on the line beforehand is not actually the same instance as the one in the presentation.Speakers collection.

Initially I was going to override Equals on the Speaker class to have it compare them by Id, but then I did some googling and found that, sure enough, others had already run into this problem. And it turns out JSON.Net (written by the coding fiend aKa James Newton-King, who also happens to be in Wellington, NZ) already handles this situation and allows you to preserve object references! See for more. Basically I just had to specify the right option when serializing the conference before saving as follows, in my UpsertConference method:

var entity = new AzureTableEntity()
    PartitionKey = "Conferences",
    RowKey = conference.HashTag,
    //When serializing we want to make sure that object references are preserved
    Entity = JsonConvert.SerializeObject(conference, 
      <strong>new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }</strong>)
TableOperation upsertOperation = TableOperation.InsertOrReplace(entity);

Setting the width of textareas in MVC

Remember earlier how I said I could make MVC automatically render a textarea for a property by simple decorating the property with the [DataType(DataType.MultilineText)] attribute? Well, what if I want to specify the height/width of the textarea? CSS to the rescue!

The framework automatically adds the multi-line class to any textareas that it renders using the default editortemplate, which means I was able to add a style for this class and achieve the desired result. E.g.

.multi-line { height:15em; width:40em; }     

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s