Create a Builder with Fluent API and C#

Having already read about this elsewhere, I still thought this stuff too much fun to not blog about: creating a builder with fluent API. You can read more about the subject on internet, this blog is just code 🙂

The code is an example implementation, it’s about the concepts. It is a stripped down but pimped version of an implementation I did at a client I worked. The application needed to provide the users with a way to dynamically construct queries for their database. Some queries came preconfigured and I used the fluent API to create these. It also enabled me to create tests more easily.

First let’s take a look at the object we’re building, the Query.

If we build the structure without a builder this would look something like this:

This is a lot of code, not very readable, not very expressive. So now we will start with the first simple implementation for setting the table name and adding columns, we create the QueryBuilder with the method OnTable and AddColumn.

The interesting part is the return value of this method, it returns the builder itself so we can call AddColumn again and again. This practice called method chaining. We can now use this code like this:

The Done method is to signal construction is done and to return the actual Query object. Fairly easy and the results are already more readable code. But we’re not done yet, we’re now going to use a second builder to add the OrderBy and take these concepts even further.

We use the constructor the pass a reference to the Query object we’re building and also the calling QueryBuilder. This is so we can return to this builder when we’re done with the order by. The previous builder used the Done method to signal the end, for this builder I decided to use the AndDirectionTo method to do this. To use this first we’ll add the following to the QueryBuilder:

Now we can use it like this:

We’ve created a little domain language here by using the SetOrderOn().AndDirectionTo() methods. But this code doesn’t prohibit saying AndDirectionTo().SetOrderOn() or even AndDirectionTo().AndDirectionTo(). For this we’re going to use interfaces. We add these:

And modify the OrderByBuilder like this:

Now we when the SetOrderOn method is called it returns the method of the IAndDirectionTo interface, not the complete builder’s interface. Nifty! We’ll make sure the QueryBuilder forces the first methods to be SetOrderOn using the same ‘trick’:

Now calling AddOrderBy can only be followed by SetOrderOn. But when you look at the usage code, using the AddOrderBy method seems a little redundant. So with I decided to go a little further with adding the Table objects. I’m not completely happy with this solution because the QueryBuilder needs to know too much about the TableBuilder, but it does result in a nicer syntax. Change the QueryBuilder method like this:

Like this we’re able to call the first method in the chain and use the code like this:

That’s it. I created a sample project and put it on GitHub.