Tuesday, 20 October 2009

Express 2010; Basic and Expert Settings

Or: “Where has Start Without Debugging gone?”

I'm a fan of C# Express; it is great tool for quick/scratch code, and if you find my machine without at least one Express window in the task-bar, then it must be Patch Tuesday.

It looks like the Express editions of 2010 have different menu configurations for basic and advanced users:

Tools/Settings/{Basic|Expert} Settings

This makes a lot of sense, but caught me off guard - especially since many of the key chords work in both modes.

I won’t attempt to list all the differences; but if you can’t find your menu items… try switching to Expert mode.

Lots of 2010 Shiny, and me feeling stupid

Lots of changes are underway:

And because you'll want somewhere to play with beta 2:

In particular, beta 2 now includes the “Express” line-up, and TFS now supports installation on client OS and has a simplified install; maybe getting a CI server (on just the Microsoft stack) will soon be practical for the individual / small team.

Feeling stupid – moving files between guest and host

I’ve downloaded the new VS2010 line-up – onto my host OS; obviously, this is beta software with the potential for mayhem (or more likely: not be 100% uninstallable), so I want to install it on the guest. In case you haven’t tried Windows Virtual PC yet (perhaps you haven’t got Windows 7 yet), then note that it loses the Microsoft Virtual PC feature to drag files between host and guest. And my guest can only really see http (no local network). So how to get the files there?

I sat there looking silly… USB was an option (Windows Virtual PC can grab USB devices), but seemed overkill. It turns out that the answer is blindingly obvious. So obvious that it didn’t even occur to me to try it. Copy… Paste. D’oh! (this does need the integration features enabled, but that isn’t a problem).

So there you go; I’ll admit to being a muppet, and hopefully it’ll save somebody a few minutes wondering.

Sunday, 18 October 2009

Anonymous Type Tricks - a clarification

The other day, I posted about using anonymous types to pass multiple pieces of state into a method - reminder:

byte[] resp = client.Post(destUri, new {
id = 100021,
dob = DateTime.Today,
caption = "Hello world"
});
About the same time Ayende Rahien posted about a related but different use in Entity Framework - example:

.Case(
e => new {
manager = e.Manager.Id
thisIsADiscriminator = “E”
}
)

There is an important distinction between the two; in the first example, the property names (in the anonymous type) are only relevant to the caller. In the second (EF), the names are relevant to the callee. This is a huge difference. I don't know enough about EF to say more, but just a note: if you are doing something like this for your own types, please use a named property object to pass expected state into a method. Think of the kittens.

Novel uses of anonymous type initializers

On a related note; there was an interesting stackoverflow question today that shows another use of the anonymous type syntax - for specifying multiple members in a type-safe way:

IncludeProperties<IUser>(u => new {
u.ID, u.LogOnName, u.HashedPassword });

The important thing to note is that the above is an Expression (not a delegate), and the code inside tears the Expression apart to find the properties that we are interested in. It never actually invokes the code as a delegate, nor does it ever actually instantiate the anonymous type. It might just be me, but I thought that was quite cute ;-p

Tuesday, 13 October 2009

Go! Dynamic

.NET 4.0 is looming (lurching?) ever-closer, and one of the many interesting developments in this release is the mainstream DLR. I’ve been trying very hard to find time to get into this, but I’ve failed horribly – every time I get some spare time, something comes up. Fortunate, then, that there are plenty of code-geeks around.

In particular, I urge you to have a look at Michael Foord’s recent articles; in particular Python for .NET Programmers (but a few more here). Rest assured that there is also plenty more in both “IronPython in Action” (from the IronPython side), and books like (not yet complete) “C# in Depth, Second Edition”  (from the C# consumer side of “dynamic” – lets face it, I’m primarily a C# developer).

I’m sure that Michael could suggest a hundred other uses, but I’ve got some configuration script scenarios in mind that I really need to find time to investigate.

Friday, 9 October 2009

Pass data simply; learning from jQuery and ASP.NET MVC

One of the really nice things that is common to both jQuery and ASP.NET MVC is how it passed multiple values around. They both avoid the complexity of things like dictionaries (or alternatively forcing specific types) by letting the caller use ad-hoc objects; by which I mean either a jQuery “post” method like:

    $.post("test.php", { name: "John", time: "2pm" } );

Or an ASP.NET MVC ActionLink of the kind:

    <%= Html.ActionLink(category.CategoryName,
new { action="List", id = category.CategoryName})%>

In both cases, we are passing caller-specific data (not callee-specific data, so optional / named parameters wouldn’t help), but without the pain that we might have used in the past. Of course, we could also pass it one of our domain entities, and as long as the names all matched up, it wouldn’t care. Which is nice.

I’m going to concentrate on the second of these mainly – this is a hugely versatile tool; and while it shouldn’t be used for every job, it sure is handy. Here’s an example of using the same approach for a more readable Format syntax:

    string s = Format("You are {age} years old and your last name is {name}",
new {age = 18, name = "Foo"});

There are a myriad of opportunities for this type of use. My main aim here is just to show some of the possibilities – and let your collective imaginations run wild. For example, to bring the jQuery “post” approach into regular C#:

    using (WebClient client = new WebClient())
{
byte[] resp = client.Post(destUri, new {
id = 100021,
dob = DateTime.Today,
caption = "Hello world"
});
string html = client.Encoding.GetString(resp);
}

With supporting code:

    public static class WebClientExtensions
{
public static byte[] Post(this WebClient client,
string address, object values)
{
if (client == null)
throw new ArgumentNullException("client");
if (values == null)
throw new ArgumentNullException("values");

NameValueCollection valueLookup =
new NameValueCollection();
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(values))
{
object val = prop.GetValue(values);
string sVal = prop.Converter
.ConvertToInvariantString(val);
valueLookup.Add(prop.Name, sVal);
}
return client.UploadValues(address,
WebRequestMethods.Http.Post, valueLookup);
}
}

Sure, this is just a basic example and there are lots of things I could do to provide more flexibility, but it shows the key points.

Thursday, 24 September 2009

Numeric keypads; what is the point?

As I hinted the other day, I've recently had some stress/RSI symptoms in my arm. Nothing too serious, just definitely worth fixing ASAP. I'm by no means crippled with RSI or anything, but I am on a mini-crusade for programmer-health, and want to help others avoid unnecessary stress / irritation.

I had a very productive session with a physiotherapist today, who made some really constructive and pragmatic points (for example, demonstrating quite simply and clearly that the curve on my corner-desk points the wrong way, and simply swapping to a mirrored desk could help hugely).

But one startling point she made while observing me; as a programmer, I move frequently between the keyboard (in my case a decent ergonomic split jobbie) and the mouse (now a shiny new "vertical" model) - code a few lines, use the mouse to click a few buttons, test a few things via the UI - rinse, repeat.

Sure, I use the keyboard shortcuts, but I would struggle to remember the hundreds of magic key-presses and "chords" that drive Visual Studio.

Very rarely do I use the numeric keypad. Heck, I also use a laptop, and the only time I find myself looking for Num Lock is when my password inexplicably doesn't work. Yet every time I move between keys and mouse I have to travel this pointless distance.

Lets not do that

Numeric keypads; absolutely useless. Underused, oversized, badly placed, and they don't agree with 'phones on which way the numbers should go.


Goldtouch Keyboard

She gave me a tip to simply try a keyboard without one, letting me have the mouse much closer. You can pick up a "Goldtouch" ergonomic keyboard (sans keypad) for about GBP25 at Amazon. This is so simple and so obvious that I'm just stunned that people insist on still including it, even on "ergonomic" keyboards. If it is so ergonomic, why is it causing unnecessary travel?!?!


USB Numeric Keypad
If you want a keypad (and I do find them handy occasionally), then buy a USB keypad (like they sell for laptops) - roughly GBP10, and that is for the models that double as a handy USB hub. If you're right-handed, stick it on the left, or on the far side of the mouse - whichever you prefer and find most comfortable. Plus you can move it to either side if a "sinister" shows up at your desk.

Join the revolt! I invite you to ditch (or move) your keypad today!

Wednesday, 16 September 2009

protobuf-net vs NetDataContractSerializer

In a user-group meeting yesterday, I was talking briefly (in a “grok” session) about “protocol buffers” (protobuf-net in particular), and I was asked a question about the comparison to NetDataContractSerializer. I didn’t have the answer to hand (I only had numbers vs DataContractSerializer), so promised to find out…

First; note that the key difference between DataContractSerializer and NetDataContractSerializer is (as I understand it) that NetDataContractSerializer includes more type metadata. This makes it possible to do a few things involving “object” and unanticipated subclasses (which can be useful), but renders it .NET-specific (and less version-tolerant).

The test

As per my existing test rig, I test 1000 iterations of a moderately large extract from the Northwind sample database (into some pretty-standard POCO types); I simply extended this rig to incude NetDataContractSerializer. For completeness, I have 3 variants of the tests, tested separately each against a different set of serializers:

  1. LINQ-to-SQL generated classes with serialization enabled at the data-context; suitable for protobuf-net, DataContractSerializer and DataContractJsonSerializer
  2. as the first test, but with the LINQ-to-SQL bits hacked out and [Serializable] added (to make it suitable for the core binary .NET serializers); suitable for protobuf-net, NetDataContractSerializer, DataContractSerializer, DataContractJsonSerializer, BinaryFormatter and XmlSerializer
  3. as second test, but with ISerializable and IXmlSerializable implemented to support BinaryFormatter and XmlSerializer via protobuf-net

The numbers and analysis

Since only the second test supports NetDataContractSerializer, that is the one I’ll focus on (with results from the 3rd test included for interest only, marked *); here’s the numbers:

Serializer Size (bytes) Serialize (ms) Deserialize (ms)
protobuf-net 133,010 10,769 23,511
NetDataContract
Serializer
992,203 29,343 91,453
DataContract
Serializer
772,406 16,272 66,755
DataContractJson
Serializer
490,425 29,604 135,125
BinaryFormatter 276,366 67,151 56,253
XmlSerializer 1,043,137 29,257 38,528
BinaryFormatter* 133,167 12,095 24,653
XmlSerializer* 177,41012,189 31,827

Some interesting points; firstly, for my sample data it is a myth that WCF’s binary type-centric serializer (NetDataContractSerializer) is going to improve things over xml (DataContractSerializer). Both in terms of bandwidth and processing time it is worse on every front. The biggest advantage of NetDataContractSerializer (in this case) is that the type model is more flexible in terms of interfaces, “object” and unknown subclasses; but this also makes it entirely non-interoperable. Of course, some details of the SOAP envelope used by the binary type-centric WCF transport may be more efficient – but I’m looking at payload for these tests.

Secondly (and fortunately for me) it yet again proves that google know how to do data transport; with protobuf-net being the clear winner for both bandwidth and processing time.

But for me, another point that I’d like to highlight is versatility and compatibility; the same classes can be used happily by a wide range of serializers (above), and presumably a few others too. For the record, the entities in the above test don’t even have any protobuf-net-specific attributes (the outer-most wrapper does have them, but only because at some point in the distant past I wanted a way to compare/contrast some protocol-buffers-specific details, which can only be controlled via the custom attributes).

Other thoughts

As with all performance tests, your specific environment and data may be an important factor. But based on the above numbers it may be reasonable to assume that protobuf-net stands a good chance of holding its corner.

Finally; following some unrelated conversations at the same user-group (they kept me pretty busy), I stress that tight serialization is only part of the story:

  • It can help reduce bandwidth costs and CPU costs associated with (de)serialization, but it won’t help if latency is your issue; protobuf-net makes no claim to change the speed of light.
  • While protocol buffers may be supported on a range of platforms, it is by no means ubiquitous; if your biggest demand is portability, perhaps use SOAP/WSDL (or offer both SOAP and protobuf-net on separate endpoints).
  • By itself it won’t solve all your disconnected woes; although it can presumably be used in tandem with any message-queue based solution you might dream of.