Tag Archives: c#

Lunch’n’Learns – Aspect Oriented Programming and Jon Skeet Abusing C#

Covering the last two lunch’n’learns in one post here, our January video of choice was about Aspect Oriented Programming. Put forward by one of our team in the end the general feeling about the approach was a muted and a challenge to use it was put forward. The challenge has not yet been taken up – make of that what you will!

The next lunch’n’learn we did was a C# in depth one – we had two videos lined up for this with the final choice down to a vote on the day. The team chose a Jon Skeet talk called “Abusing C# More”. As ever, Jon is very interesting and when you twist the language like this you do have to think about the internals in greater depth than you usually do. Very little of practical use in this talk however, it was great to have something a little lighter this time.

Abusing C# More – Jon Skeet from NDC Conferences on Vimeo.

Lunch’n’Learn – async/await

We’ve recently started doing more work with the async/await keywords (a side-effect of using MassTransit) and we thought it was a good idea to go through the basics again. Here’s the talk I found which is very easy to follow, full of useful information and well-recorded (if a little dry) – it focuses on usage within ASP.net but that’s useful for most of us.

Additional bonus video about whether you should always be using an O/RMs (9 minutes, funny): https://vimeo.com/28885655

Keeping it simple – sometimes you don’t need to call .Any()

Something I’ve seen quite a lot of in the codebase I’m working on…

If a collection is empty, the count is 0 and the enumerator is valid but won’t return any items – so there is no need to call .Any() before summing or enumerating.

Count() example

articleTagListForTagGroup
    .Where(tag => tag.ChildTags.Any())
    .Sum(tag => tag.ChildTags.Count());

Because .Count() returns 0 if there aren’t any, this can be reduced to:

articleTagListForTagGroup
     .Sum(tag => tag.ChildTags.Count());

Enumerator / foreach example

if (tag.ChildTags.Any()) {
    foreach (var childTag in tag.ChildTags) {
        // Display

If there aren’t any ChildTags then the foreach has nothing to iterate over – but it won’t throw an error, it just jumps over the code block. It can be simplified to:

foreach (var childTag in tag.ChildTags) {
    // Display

Storing arbitrary key/value pairs (like hashtable or dictionary) in Azure table storage

Azure table storage allows you to store different data structures in the same table, but the standard routines for getting data in and out require you to know that structure at compile time – as you have to define the entity (class) to be persisted.

There are some very clever implementations that use the dynamic object features of C# to create objects with run-time defined properties. But what if you just want to store “some additional data” in key/value pairs but you don’t really need them to be properties on an object? There’s nothing out of the box for that, but you can override that by altering the Read/WriteEntity methods on TableEntity.

Here’s a naive implementation (it assumes string data, doesn’t limit number of data items, etc.)

public class TestEntity : TableEntity {

	public TestEntity(string a, string b) {
		PartitionKey = a;
		RowKey = b;
		DataItems = new Dictionary<string, string>();
		DataItems.Add("foo", "bar");
		DataItems.Add("Jacob", "Xander");
	}

	public Dictionary<string, string> DataItems { get; set; }

	public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext) {
		var results = base.WriteEntity(operationContext);
		foreach (var item in DataItems) {
			results.Add("D_" + item.Key, new EntityProperty(item.Value));
		}
		return results;
	}

	public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) {
		DataItems = new Dictionary<string, string>();

		foreach (var item in properties) {
			if (item.Key == "Timestamp") {
				Timestamp = item.Value.DateTimeOffsetValue.Value;
			} else if (item.Key == "RowKey") {
				RowKey = item.Value.StringValue;
			} else if (item.Key == "PartitionKey") {
				PartitionKey = item.Value.StringValue;
			} else if (item.Key.StartsWith("D_")) {
				string realKey = item.Key.Substring(2);
				ItemData[realKey] = item.Value.StringValue;
			}
		}
	}
}

First key thing to remember – Azure table storage can only store a total of 255 key/value pairs – or 252 custom ones once you’ve taken PartitionKey, etc. into account.

I’d welcome any comments, improvements, or just drop me a note if you’ve used this in your own project – thanks!