Tag Archives: .net

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

MassTransit / RabbitMQ lunch’n’learn

Today I ran a lunch’n’learn around MassTransit and event driven architecture. In addition to some demo code, we watched the video below. It covers things quite nicely, not a lot of detail in the demonstration but as it’s MassTransit 2.x (not 3.x) that was OK. Quality is good, speaker is a little drone-y so a bit of snooze-danger if you’ve had too much pizza!

Loosely coupled applications with MassTransit and RabbitMq – Roland Guijt from NDC Conferences on Vimeo.

RESTful API lunch’n’learn

A little while ago we did a RESTful API lunch’n’learn session. These are the videos that we used.

This video from Stormpath covers a lot from basics to good patterns to use – it’s also good quality and as a speaker he’s quite animated so this kept our attention:

For those people who’ve used more SOAP services this video has been good to help compare approaches – however it does seem as if the presenter prefers SOAP (what else would you expect from Oracle?) so you have to apply your own salt:

Other notes

Here’s a quick overview of the properties of the HTTP verbs (this applies to more than just REST APIs!):

GET – Safe, Idempotent
PUT – Idempotent
DELETE – Idempotent
HEAD – Safe, Idempotent
POST – (none of these)

Safe: Makes no change to the server / performs no action. For e.g. reading an entity is a safe, incrementing a counter or sending an e-mail is not.

Idempotent: Doing this multiple times is the same as it doing it just once. For e.g. “a = 1” is idempotent. “a += 1” is not.

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!