Home > Uncategorized > Using Fluent Interfaces

Using Fluent Interfaces

February 6th, 2009

A couple weeks ago I was mentioning a training I did at Concentra. Well here’s the codebase that came out of the three days of training.

You’ll need a subversion client to checkout the code (like tortoiseSVN)

http://sleepoverrated.googlecode.com/svn/2009-01-14-Training_Codebase

Fluent Interfaces

Fluent Interfaces are are a way of writing code in which its more readable. Wikipedia uses the example of having a method return its class so you can add up a series of method calls like this.

new ConfigurationFluent().SetColor("blue")
                                           .SetHeight(1)
                                           .SetLength(2)
                                           .SetDepth(3);

The preparation exercise was to create a Movie Library that allowed us to search and sort in an number of different ways. We used the specification pattern to search thru an in memory collection. For searching moved towards a way of having a Linq like query syntax that let us select all the movies of different criteria. You can see some examples of this used in MovieLibrary.cs

Here is an example of how we used Movie.is_of_genre to search for all the action movies in our library. The Search Criteria is built up as a Specification and then checked for each movie in all_movies() by satisfied_by()

        public IEnumerable<movie> all_action_movies()
        {
            return all_movies().satisfied_by(Movie.is_of_genre(Genre.action));
        }

Looking at Move.is_of_genre() in Movie.cs we can see the Fluent Interface we created for created a Specification to check if a Movie has a genre that we specify.

        public static ISpecification<movie> is_of_genre(Genre genre)
        {
            return Where<movie>.has(x => x.genre).equal_to(genre);
        }

this is our fluent interface that we developed for querying. I recommend you look thru the code for this.

How would you write this?

When writing Fluent Interfaces you want to just write some code to do the thing you want to do in the way you want it to look. Then you have to figure out some way to make it compile. You may have to change your code around to get it to work.

Implement classes in reverse

You may naturally feel like implementing the Where class first but the one thing you know is what has to be returned and that is the responsibility of the last method.

  1. equal_to needs to return an ISpecification<movie>
  2. Where.has() needs to return a class with an equal_to method

We named the class returned by Where.has() as SpecificationBuilder<T,TOutput>. The x=> x.genre is passed into the new SpecificationBuilder in the constructor as a Func<T,TOutput> property_accessor. This Func is a function we call that takes in a Movie and returns a Genre if it was a delegate it would look like

        public Genre property_accessor(Movie movie)
        {
            return movie.genre;
        }

The great thing with C# generics is that it actually infers the TOutput type of genre in from the lambda x => x.genre. I barely noticed this ability until I started using Generics in Java. It is easier to just do this in Linq and there are some examples of that in MovieLibrary.cs but it’s a challenging exercise to try.

Doing things differently teaches you different things.

Uncategorized

  1. James Green
    August 24th, 2009 at 19:19 | #1

    Hi,

    I’m having trouble opening this solutions. The moviestor.web project won’t load. I noticed inside the .csproj file that it uses the .NET MVC stuff so I’ll try installing that and see if that helps.

    Thanks for a great article.

    James.

  2. September 5th, 2009 at 23:34 | #2

    Ya sorry I didn’t mention that, You’ll need MVC, but that code was using an old version of that so you’ll find it will have some errors compared to the last one.

    I’m teaching another course this week, so I’ll be posting the code from that too

  1. No trackbacks yet.