Software dev, tech, mind hacks and the occasional personal bit

Month: May 2008

Photos from Sapa, Vietnam

I put holiday photos on my screen saver, and these favourites just came up. I thought I’d share them with you…

Exploring Sapa

Sapa Guides

Bounded Actions Using Lambda – IDisposable is old and ugly!

In .NET 2, it was all the rage to make hand-crafted, clever IDisposables that let you do a bounded action with clean up. Eg,

void SomeMethod()
{
        using (new SetCursorToWaitEggTimer())
        {
            VerySlowOperation();
        }
}

void VerySlowOperation()
{
    ... etc ...
}

This was kind of cute – you could make sure that, even if an exception was thrown, your clean up (eg, changing cursor back to normal) would occur. Implementing the IDisposable was a bit ugly but consuming it wasn’t bad.

Now, with the sexy C# 3 syntax, you can do something similar much more elegantly. Eg,

void SomeMethod()
{
       DoWithWaitEggTimer(VerySlowOperation);
}

void DoWithWaitEggTimer(Action action)
{
    try
    {
        Mouse.OverrideCursor = Cursors.Wait;
        action();
    }
    finally
    {
        Mouse.OverrideCursor = null;
    }
}

If you’re feeling like more adventures, you can also start passing these delegates around and injecting them. For example:

class SomeClass
{
    public Action RunSlowCode 
    {
    	get { return runSlowCode ?? new Action(a => a.Invoke()); }
    	set { runSlowCode = value; }
    }
    Action runSlowCode;

    void DoSomethingSlow()
    {
         RunSlowCode(PullDataFromExternalSystem);
    }
}

This approach allows you to inject the delegate for what happens when slow code is run. So you could inject DoWithWaitEggTimer() or something new like DoWithWaitMessageDisplayedToUser(). Similarly, it could be used for unit testing or injecting between layers in your application.

C# Default Access Modifier for Class Members – and drop that private habit!

The default access modifier for the members of a C# class (eg, fields, methods, and properties) is ‘private’. As such, I recommend never using the redundant ‘private’ keyword for class members. Leaving off the private nicely separates your privates from your public/inheritable interface in syntax highlighting. It also saves people having to read redundant code – you wouldn’t want your code to be full of un-needed casts, or redundant ‘this.’ references, would you?

WPF Control Inheritance With Generics

Working in WPF is quite exciting – there’s a lot of new possibilities, especially with easy control composition, much improved binding and Expression Blend to make sexy interfaces. One of the things you’re likely to want to do though, when writing anything more than a toy application, is to have a base class for your UserControls or Windows, to share common functionality. It is also quite likely you will want to use generics in conjunction with control inheritance. With both the code behind, and the XAML, it’s not immediately obvious how to do generic inheritance. It is a bit fiddly to get going, and sometimes the errors are not helpful. Here’s a simple example that outlines how to bring it together.

The base control

namespace WpfGenericsDemo
{
    public class BaseUserControl<T> : UserControl where T : IPresenter
    {
        public BaseUserControl()
        {
            ... various configurations ...
        }

         ... Awesome functionality to share ...
    }
}

The child control code-behind

namespace WpfGenericsDemo
{
    public partial class ChildUserControl : BaseUserControl<ChildPresenter>
    {
        public ChildUserControl()
        {
            InitializeComponent();
        }

         ... More code ...
    }
}

The child control XAML

<WpfGenericsDemo:BaseUserControl x:Class="WpfGenericsDemo.ChildUserControl"
    x:TypeArguments="WpfGenericsDemo:ChildPresenter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfGenericsDemo="clr-namespace:WpfGenericsDemo">
    
    ... The rest of your awesome XAML ...

</WpfGenericsDemo:BaseUserControl>

Notes

  • Your top level node is the parent class of the control you want to create (eg, BaseUserControl). You specify the control class you want to create with ‘x:class’ (eg, ChildUserControl)
  • ‘x:TypeArguments’ is the way you specify the generic type (eg, ChildPresenter)
  • You need to namespace your classes – eg, with ‘xmlns:WpfGenericsDemo’ which uses a clr-namespace style reference
  • Only your top level node can be genericised in XAML

Powered by WordPress & Theme by Anders Norén