An analogy: Good UIs and Fluent APIs


A while back I was writing a web app to try the Entity Framework and MVC together.

I knew the pain points would probably be around Foreign Keys, or at least the lack of them in .NET 3.5 SP1 (FKs are now available by default in .NET 4.0).

So I started looking for opportunities to edit entities and their FKs, to investigate the expected pain.

The interesting thing is while I did this it all felt somewhat forced, and it started to dawn on me that in a good UI there are very few times that you want to select something from a drop down.

Usually good UIs are designed so that most things including FKs are established from Context. A good UI only asks you to provide the information it can't infer.

Take StackOverflow as an example. When you "Answer a question" you simply type your answer and submit.

But an Answer is more than just a long string, it has an author, a created time, and a Question.

The Question is probably modeled as an FK in the database, but you aren't asked to pick a question, you've already done that when you clicked on the Question to read the details.

Essentially you establish the parameters to the "CreateAnswer(...)" method as you browse.

Then I started to realize that Fluent APIs are all about the same thing. There are lots of interesting Fluent APIs in the .NET world now. Something like StructureMap is a good example. But in the interests of brevity here is a silly example of a Fluent style API just for context:


As you see rather than forcing you to provide all the parameters in one go, Fluent APIs allow you build them slowly as you chain method calls with intellisense guiding you.


If you squint hard enough Intellisense is similar to a hyperlink, and the parameters to the function are similar to whatever a web form needs you to enter.

To illustrate this analogy we could describe the process of creating an Answer on StackOverflow like this:

.Answer("here is my answer");

Rather than like this:

"here is my answer");

What do you think? Is this an interesting analogy?

All this is a little random I know, but hey, I wanted a change from writing EF tips!