Tuesday, November 30, 2010

Some handy extension methods for argument checking

After a quick Twitter interaction with Marc Jacobi I thought I'd post a small set of extension methods I like to use for argument checking:
using System;
using System.Collections.Generic;
using System.Text;

namespace peSHIr.Utilities
{
    public static class Guards
    {
        public static void GuardNull
            (this object argument, string argumentName)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(
                          argumentName,
                          "No object supplied");
            }
        }

        public static void GuardNull
            (this string argument, string argumentName)
        {
            if (String.IsNullOrEmpty(argument))
            {
                throw new ArgumentNullException(
                          argumentName,
                          "No text supplied (or text is empty)");
            }
        }

        public static void GuardZero
            (this int argument, string argumentName)
        {
            if (argument == 0)
            {
                throw new ArgumentNullException(
                          argumentName,
                          "The number zero is not allowed here");
            }
        }

        public static void GuardNull<T>
            (this Nullable<T> argument, string argumentName)
            where T:struct
        {
            if (argument.HasValue == false)
            {
                throw new ArgumentNullException(
                          argumentName,
                          "Empty nullable type not allowed here");
            }
        }

        public static void GuardMinimum<T>
            (this T argument, T lower, string argumentName)
            where T : IComparable<T>
        {
            if (argument.CompareTo(lower) < 0)
            {
                throw new ArgumentOutOfRangeException(
                          argumentName, argument,
                          string.Format("Minimum allowed: {0}", lower));
            }
        }

        public static void GuardMaximum<T>
            (this T argument, T upper, string argumentName)
            where T : IComparable<T>
        {
            if (argument.CompareTo(upper) > 0)
            {
                throw new ArgumentOutOfRangeException(
                          argumentName, argument,
                          string.Format("Maximum allowed: {0}", upper));
            }
        }

        public static void GuardRange<T>
            (this T argument, T lower, T upper, string argumentName)
            where T : IComparable<T>
        {
            if (argument.CompareTo(lower) < 0 || argument.CompareTo(upper) > 0)
            {
                throw new ArgumentOutOfRangeException(
                          argumentName, argument,
                          string.Format("Allowed: [{0},{1}]", lower, upper));
            }
        }
    }
}
When you can use code contracts this is probably preferable. Otherwise, these little extension methods can come in really handy for some general cases.

Saturday, November 6, 2010

First WP7 app should be published!

This morning I found out that my first Windows Phone 7 application had passed testing. It's status is now "Published to Marketplace", but I have not been able to see it appear on my phone yet. Hope it does so soon.

In the meantime, here are a couple of screenshots of the application in English; it supports English, German, Spanish and, even though not supported yet, Dutch. All the content that is shown (RSS feed items from my companies website and its tweets) is in Dutch only, however.

Wednesday, November 3, 2010

Is WP7 getting another theme in its next version?

When playing with the example code solution of the November 2010 version of the Silverlight for Windows Phone Toolkit, I noticed the following:

Since this screen seems to do its best to mimick the Theme page from WP7 phone settings to show off one of the places where WP7 itself uses the ListPicker control (with appointment status being another one), I would not be surprised if we would see a new "dazzle" theme in a next version of the Windows Phone OS.

This piece of example code could mean nothing of course, but I'm still curious what a "dazzle" theme might look like...

Annoyed by swamped App Hub

Yes, I really am.

I admit that a launch of the magnitude of Windows Phone 7 is no easy thing. And that I didn't really get active in WP7 application writing until the LG developer prototype device from Microsoft arrived two weeks ago. But still.

I have been wanting to publish at least one WP7 app to the marketplace for almost those two weeks now. Thanks to Microsoft NL (way to go, Matthijs!) I should have been white-listed for WP7 app publishing already, but so far this has not materialized. The App Hub forum seems awash with developers in a similar predicament. Without a real solution in sight.

The relevant forum threads all started mentioning the magical date of november the 3rd as a solution: by then everyone registered as a marketplace developer (no matter the platform) would be able to publish WP7 apps. Too bad no time (and timezone) was mentioned for this...

Because right now here in the Netherlands 10am on november the 3rd has come and gone, but I still cannot publish. Combining the 'timezone' and the 'no specific time mentioned' issues I'm waiting for tomorrow morning, roughly assuming after 11pm on the 3rd on LA time. However, I do not assume publishing will have commenced normally (=in force) by then...

I would definately not want to be on the WP7 App Hub team right now. Good luck the coming weeks, guys!

Wednesday, October 20, 2010

A small tip to .NET bloggers...

I have a small tip for .NET bloggers out there:

Will you please include full namespaces in your example code?

For people not yet well versed in the .NET API that you describe or use in your code, nothing is more frustrating than copying a couple of lines of code from a blog and then finding out that it doesn't compile because they have not referenced the right assembly and/or included the needed using statement for the namespace.

What would you prefer to find as an example in a post that describes how you can let Windows open a website in the default browser by simply starting a new process with the URL as the commandline?

  1. System.Diagnostics.Process.Start("http://blog.peshir.nl/");
  2. using System.Diagnostics
    ...
    Process.Start("http://blog.peshir.nl/");
  3. Process.Start("http://blog.peshir.nl/");
    You find the Process class in the System.Diagnostics namespace.
  4. Process.Start("http://blog.peshir.nl/");

To me any option is fine, except the last one.

Google Maps should (no: must) come to WP7

With my personal interest in maps and mapping I read the blog post about Google Maps on WP7 (using the Bing Maps Silverlight control) by LocalJoost with great interest.

He mentioned a lot of pageviews to this post shortly after posting it, and I just saw him tweet about his post being picked up on by wpcentral.com.

I think it's great that wpcentral.com did pick up on this. However, I do not agree at all with the first part of the possibly meant to be funny footnote by Daniel Rubino:

Yeah, I know. Who would want to use Google Maps when Bing kicks so much butt? [...]

I suspect that Daniel is both Microsoft biased (not really a problem for me) and not based in the Netherlands, because even though I hear a lot of positive things about Bing Search and Bing Maps, I keep comparing it to Google Search and Google Maps and have not switched yet.

To be frank and rather blunt about it: Bing can't stand in the shadow of Google. Or at least it cannot in the Netherlands, which happens to be where I live and use these services almost exclusively.

Before anyone starts comparing actual mobile client features I want to point out that this is mainly related to (probably) legal and data issues and not to the technology. I just have the feeling Google in general "finds more" for me, gives me more useful results. Also, I like the visual style of Google Maps more than that of Bing Maps at the moment, but that could just take some getting used too.

So for now I'll take Google over Bing any way I can get it. And that includes a Google Maps client for WP7, preferably written by Google itself and including all the official Google Maps features.

Google, are you listening...?