First Look: Hubble

Posted Sunday, May 13, 2012 by

I'm finally ready to give you all the first look at the Windows 8 Metro application I've been working on. Hubble is a new way to create and manage issues on your GitHub repositories.

I've used Semantic Zoom to provide management overviews so you can easily visualise how work items are distributed either over your team or milestones. For management I've created a unique drag and drop Kanban style board to assign tasks between collaborators, milestones or labels. Here's the first sneak peek:

If you like what you see help spread the word!

How not to use LINQ

Posted Tuesday, March 06, 2012 by

Since LINQ (Language Integrated Query) was released by Microsoft as part of the .NET Framework 3.5 in 2007 (has it really been that long?) developers have really taken to it, however I often see the same mistakes repeated time and time again and thought I'd cover some of them.

Often developers first encounter LINQ with examples using the query style syntax; this tends to appeal to people initially due to its closeness to SQL in syntax. Only later on do they work with the extension method style syntax (which is actually the one I prefer). This can often lead to syntax like:

var product = products

    .Where(p => p.Id == 42)

    .Select(p => p)

    .FirstOrDefault();

 

product.UpdatePrice(13.50m);

The important point here is that the Select method is a no-op, doesn't do a damn thing and should be removed. Anything that gets in the way of another developer understanding your code should be removed.

The other method used a lot is FirstOrDefault by developers wanting to turn their collection into a single object. There's a couple of problems with this, as the name implies all the *OrDefault methods return the default value for the type if they can't return a result. For reference types this means null, so in the code above we'll get a null reference exception on the second line if there aren’t any objects in the collection. 95% of the times I see FirstOrDefault it should have been replaced with First, or to be even more accurate with Single, but I'll get to that.

The important takeaway here is that you should truly think about whether the method may not product a result and if it might not then handle that case. If nothing else then using First will give a more informative exception rather than the more generic null reference one. The next problem with the code above (assuming Id should be unique) is that we shouldn't be using First but Single, if we're retrieving a product by id then it's certainly an exception if there's more than one! First will happily return the first matching result, Single will throw an exception, so again the takeaway is think about what should be in the collection and whether First or Single is more appropriate.

This may seem like a small change, but choosing the right method also lets you show your intent to the next developer to maintain this code. If they see FirstOrDefault they may make certain assumptions about how other parts of the system may work. On a related note all the above methods as well as Any have an overload that takes a predicate so you can remove the redundant Where method and use the overload of Single. Giving us a final refactoring of:

var product = products

    .Single(p => p.Id == 42);

 

product.UpdatePrice(13.50m);

The results of LINQ queries are almost always collections, if there are no results the collection will be empty, the result will not be null. So code like below will never throw the exception.

var onSaleProducts = products

    .Where(p => p.OnSale);

 

if(onSaleProducts == null)

    throw new ApplicationException("No products on sale");

These are the most common mistakes I see with LINQ code, using the wrong method to get the job done, while it may work for testing, once deployed can be very different.

Tags: c#,

New Tutorials Series

Posted Tuesday, February 21, 2012 by

Today I'm proud to announce the start of a new tutorial series Building a Marketplace Ready App. The goal of this series is rather than covering the basics about putting together an application, but rather the large amount of work that's required to take a sample style piece of code and getting it into a shape ready for publication. The first tutorial covering Setup and Product Search has just been posted.

The other goal with series to help me compile a lot of the disparate utilities I've used to create applications and bring them together into a single package, the 0.1 version you can download from Nuget or browse the code on Bitbucket.

Take a look and read and let me know what you think.

Brand New Website

Posted Wednesday, February 08, 2012 by

So the blog here has been a little quiet over the last month and now I can reveal why. For the last couple of months I’ve been working on a new version of my website. Normally it wouldn’t take this long for me but I gave myself a few challenges on the way. The first being the design itself, wanting to do a bit more of a “Metro” style design left me actually designing the site myself, not something I’m particularly comfortable with but I think it came out ok but I don't think I'll be offering design services any time soon. The second was a chance to look at Entity Framework Code First, I initially spiked a version that used RavenDB but found deployment to my shared host was problematic, it's a shame since it's a great project to work with.

For a lot of my professional career I’ve been a web developer but for last eighteen months or so I’ve been doing more backend or xaml based work so haven’t really done much with HTML5, CSS3. There's been a lot of new things new to this area in the last year including responsive design, mobile, languages such as SASS and CoffeeScript. It’s amazing how fast this industry moves and how quickly it feels like your skillset is out of date. Hopefully I can find the time to revisit this website project a bit more often and evolve the entire project.

Some other fun things including on the way was using the fantastic Web Workbench product from Mindscape here in New Zealand to create the .LESS stylesheets. Having variables for things like accent colour made everything a lot easier.

As with all software projects this will be an ongoing thing as there’s still a lot I want to do, these include:

  • Cleaning up the markup to be more semantic and improve the use of the new HTML 5 tags.
  • Move to a responsive grid to help the website work on mobile devices.
  • Social Sharing via Facebook and Twitter.
  • As always the code can be cleaned up.
  • Metro Tutorials
  • MetaWeblog API so I can use Markpad as an editor.

Hope you like the new website; I’ve got lots of plans for it for the year and would love your feedback.

Storyboards in Caliburn Micro

Posted Monday, November 14, 2011 by

In my previous post I talked about the benefits of using co-routines in Caliburn Micro to ease any interactions with the View from the View Model. In that case it was the use of the Visual State Manager; in this post we’ll cover managing storyboards and animation.

We’ll use code from an older post around how to create one off event handlers. What I’ve done is encapsulate that logic into an extension method ToObservable.

public static IObservable<IEvent<EventArgs>> ToObservable(this Storyboard storyboard)

{

    if(storyboard == null)

        throw new ArgumentNullException("storyboard");

 

    return Observable.FromEvent((EventHandler<EventArgs> e) => new EventHandler(e),

                                e => storyboard.Completed += e,

                                e => storyboard.Completed -= e);

}

In the BeginStoryboardResult we verify the view is a FrameworkElement (and therefore can contain Resources). We then load the Storyboard from the Resources collection. Using the extension method we wire the Completed event of the Storyboard to the completion of the IResult.

public class BeginStoryboardResult : ResultBase

{

    private readonly string storyboardName;

 

    public BeginStoryboardResult(string storyboardName)

    {

        this.storyboardName = storyboardName;

    }

 

    public string StoryboardName

    {

        get { return storyboardName; }

    }

 

    public override void Execute(ActionExecutionContext context)

    {

        if(!(context.View is FrameworkElement))

            throw new InvalidOperationException("View must be a framework element to use BeginStoryboardResult");

 

        var view = (FrameworkElement)context.View;

 

        if(!view.Resources.Contains(StoryboardName) || !(view.Resources[StoryboardName] is Storyboard))

            throw new InvalidOperationException(String.Format("View doesn't the contain a Storyboard with the key {0} as a resource", StoryboardName));

 

        var storyboard = (Storyboard)view.Resources[StoryboardName];

 

        storyboard.ToObservable().Take(1)

            .Subscribe(e => OnCompleted());

 

        storyboard.Begin();

    }

}

After that it’s pretty much just starting the actual storyboard.

Again one of the main benefits of result an IResult like this is we still maintain separation between the view model and the view, we can now create unit tests that test how the view model plays storyboards without requiring the actual storyboard.

Page 1 of 1612345>

Professional Windows App Development