flow.
"Flow is a condition of deep, nearly meditative involvement." - Tom DeMarco

30 Seconds to a Fluent Interface

Fluent interfaces are a great way to make your code more readable, and the express your intent in a clearer, often more concise, way.

A hallmark of a good fluent interface is the ability to seamlessly string together an arbitrary number of objects and still have your code resemble spoken language.  For example, consider the following snippet of code from a builder object built with a fluent interface…

var larry = new CustomerBuilder().WithName("Larry").WithAge(30).Build();

The fluent interface applied to the code makes it incredibly simple for anyone to ascertain the intent of this code at a glance.  The key to this ability to easily chain members together in nearly any order is that each member of the class returns a reference to itself.  This reference can then be used to to add additional commands onto the chain.  Let’s take a look at the CustomerBuilder class…

internal class CustomerBuilder
{
    private string _name;
    private int _age;

    public Customer Build()
    {
        return new Customer(_name, _age);
    }

    public CustomerBuilder WithName(string name)
    {
        _name = name;
        return this;
    }

    public CustomerBuilder WithAge(int age)
    {
        _age = age;
        return this;
    }
}

Notice that with the exception of the Build() method, each of these method signatures have a return type of their parent class, CustomerBuilder.  Beyond that, notice that all of those methods satisfy that signature by returning a reference to the current object.  This allows us to continue to alter the method under construction, either by setting the name or age, without losing context.  The afore mentioned Build() method returns the newly constructed object once we’ve finished setting the properties.  This trick of almost always returning the current object from a method is what allows us to easily chain objects together to create more naturally spoken code.  If you think this looks great in C#, you should try it in a more terse language such as Boo.

Next time we’ll dive deeper and talk about some of the issues which come up when you try to mock a fluent interface.

MSTest Projects Won’t Upgrade to VS2008

I recently just upgraded a branch of our solution from VS2005 to VS2008 using the upgrade wizard.  All projects upgraded easily except for a few.  After a second look, I noticed all of those projects were unit test projects.  In fact, they were all MSTest unit test projects. 

We use MbUnit and NUnit for our unit tests so we had abandoned the MSTest projects some time ago.  This was mainly because I got tired of watching Visual Studio hang for 2 – 3 seconds to load that stupid Testing Toolbar just because I accidentally clicked on one of the MSTest projects in the solution explorer.  However, there were a few remnants still hanging around that we just hadn’t gotten to.  If you have a similar situation, here’s how to fix the problem…

    1) Click on the “unavailable” project in the Solution Explorer and choose “Edit MyProject.csproj”

    EditProj
    2) The project file will open in the Visual Studio editor.  Locate the <ProjectTypeGuid> element and remove the value {3AC096D0-A1C2-E12C-1390-A8335801FDAB}.  This it the value that identifies the project as an MSTest project.  You can find a list of more project type guids here.

    projectType

    3) Click the “unavailable” project in the Solution Explorer and choose “Reload”

    reload

    4) Voila.

Call for Help for DotNet Online Help Authoring Application

Hello all,

My team is currently in the process of choosing a help authoring system that will allow us to produce and integrate context sensitive help into a fairly large, pure .NET SmartClient application.

We have previously used Quadralay WebWorks for our legacy C++ application, but the .NET support for WebWorks seems flaky at best.  Does anyone have…

  1. 1) Any experience with WebWorks from a .NET application and would like to share, good or bad?
  2. 2) A recommendation for a good online help system with good support for .NET WinForms (we’re also using DevExpress for our custom UIs).  We are starting to consider RoboHelp, but are only in the exploratory stages so far.

If you have any recommendations you would like to share, please leave a comment below.  If you don’t feel comfortable leaving a comment regarding a commercial product, feel free to drop me a private email at jeremy AT jeremyjarrell DOT com.  You’re recommendations will be kept in the strictest confidence.

Thanks in advance,

Jeremy