Posterous theme by Cory Watilo

Filed under: code

MessagePack parser for Objective-C / iPhone

This is a wrapper for the C MessagePack parser, as an easy to use bridge to Objective-C, which my employer has graciously permitted us to open source:

https://github.com/chrishulbert/msgpack/tree/master/objectivec

In a similar way to the JSON framework, this parses MessagePack into NSDictionaries, NSArrays, NSNumbers, NSStrings, and NSNulls.

It contains a small patch to the C library so that it doesn't segfault with a byte alignment error when running on the iPhone in armv7 mode.

If you're using JSON or XML to communicate from your servers to your iPhone app, you should strongly consider using MessagePack for size and speed: in our tests, gzipped MessagePack was roughly half the size of gzipped JSON, and is much faster to parse to boot.

HttpContext vs HttpContextBase vs HttpContextWrapper

Http Context is handled a little differently in C# 3.5 onwards, its a little hard to get the full picture by looking at the msdn docs ("whats with these 2 new context classes???" is what i thought) so here's my take on it.

HttpContext

This is the vintage asp.net context. The problem with this is that it has no base class and isn't virtual, and hence is unusable for testing (cannot mock it). It's recommended to not pass it around as function arguments, instead pass around variables of type HttpContextBase.

HttpContextBase

This is the (new to c# 3.5) replacement to HttpContext. Since it is abstract, it is now mockable. The idea is that your functions that expect to be passed a context should expect to receive one of these. It is concretely implemented by HttpContextWrapper

HttpContextWrapper

Also new in C# 3.5 - this is the concrete implementation of HttpContextBase. To create one of these in a normal webpage, use new HttpContextWrapper(HttpContext.Current).

The idea is that to make your code unit-testable, you declare all your variables and function parameters to be of type HttpContextBase, and use an IOC framework eg Castle Windsor to get it injected. In normal code, castle is to inject the equivalent of 'new HttpContextWrapper(HttpContext.Current)', whereas in test code you're to be given a mock of HttpContextBase.