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

Category: C# Page 3 of 4

Tech Ed Talk: REST Patterns and .NET

I’ll be giving a talk at Tech Ed this year on REST and how it can be implemented in .NET, much inspired by the thoughts of Jim Webber on good RESTful web services, and Garr Reynolds on the “Zen” presentation style. Here’s some more info:

REST Patterns and .NET

Sydney Convention Centre, Darling Harbour
5 September 2008
10:15am – 11:30am
(ARC306)

REST has sparked furious debate, and reactions from fan-boy adoration to hate. As the arguments quiet and the dust settles, it is becoming clear that the RESTful style is a viable choice for the Enterprise. Framework support is growing rapidly. WCF now provides basic REST support. Meanwhile, the budding MVC framework opens the door to building services which leverage hypermedia. This talk will leave you with an understanding of the RESTful architectural style and provide you with recommendations on designing and building both simple and hypermedia driven web services in .NET.

Hope to see you there!

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

NUnit Test Runners Were Not All Made Equal

NUnit tests can be run using a variety of different runners. Some common ones are:

The NUnit GUI and Test Driven create a new instance of the test class for each test run. This leads to more isolation but potentially slower performance.

Resharper and NUnit MSBuild Task re-use the same instance of the test class when running each test in the class. This can lead to unintended interaction between tests. Using these runners, it is vital to to assign initial values to instance variables in SetUp, rather than when they are defined or in the constructor.

If you use a mix of different test runners, you can end up with tests that pass on some machines and fail on others (eg, Test Driven locally works fine, but you use NUnit MSBuild Task on your build box and get intermittent failures).

NUnit SetUp Attribute and Subclassed Test Cases

If you have a ChildTestCase class that inherits from a ParentTestCase class, and both of these have a SetUp method, marked with the [SetUp] attribute, would you expect both to be called? If so, you would be sadly disappointed. Only the SetUp method of the ChildTestCase will be called, and the SetUp in the ParentTestCase will be ignored.

According to the NUnit documentation on the Set Up attribute, this is intended behaviour:

If you wish to add more SetUp functionality in a derived class you need to mark the method with the appropriate attribute and then call the base class method.

An alternative approach to get all your SetUps called is to have a base TestCase class define a protected virtual SetUp() (with the SetUp attribute), which all child classes override (and call base on their first line).

Delicious .NET – Slides & Code

Here’s the slides and code from yesterday’s “Delicious Dot Net” talk at ACS.

ACS Talk – “Delicious .NET” – 3rd Oct

I’ll be giving a talk at an ACS event after work on Wed next week (3rd October). Here’s the blurb:

A tasty take on WPF, WCF, LINQ and O-R Mapping

An exploration of some of the freshest, tastiest and most powerful features in .NET 3.5 through implementing a useful application.
You’ll take away an understanding of what’s in .NET 3.5 and how to build online and offline applications with the new technology stack supported by Visual Studio 2008 (Orcas Beta 2).

Location
Norman Selfe Room,
Level 3,
280 Pitt St Sydney (Sydney Mechanics School of Arts)

Time
6:15pm, 3rd October 2007

For more info or to register, please visit the ACS site.

Hope you can come 🙂

Reflexil, cute code injection for C#

While catching up on my favourite blogs, I came across Reflexil on Fabrice’s blog. Reflexil is able to do C# code injection into existing assemblies and save the resulting assembly. I haven’t given it a go yet, but it looks like a really neat tool. The legal implications may mean it is only useful for emergency patching or debugging however…

BarCamp Sydney this Saturday

It is BarCamp in Sydney this Saturday. I haven’t been to one of these unconference style events before, but I’ve heard good things about it from my colleagues. I’m quite excited to go along and see what it is like.

If you’re in Sydney and interested in stopping by, details are as follows:

When: Sat 25th August 2007 from about 9am
Where: University of Technology, Sydney (Jones St entrance)
More details…

One of the novel aspects of BarCamp is that all participants are encouraged to present or start a discussion around something that interests them. For my part, depending on what people are interested in, I was thinking of one of the following:

  • new stuff in the .net space (C# 3.0, .NET 3.5, LINQ, WPF, WCF, etc) and showing some demos
  • giving a bit of a Ruby/JRuby introduction with help from Ola’s JavaBin slides
  • or, if people are keen, a discussion around JRuby vs C# 3 vs Java vs ?? and their stacks for different situations and problems
    • By the way, the conference is free, and it is not too late to sign up 🙂
      Hope to see you there!

Page 3 of 4

Powered by WordPress & Theme by Anders Norén