Introduction

Undoubtedly, Web 2.0 and AJAX have simplified the development pipeline for taking an application concept from the drawing board to the live environment. Even so, there is an argument for the installed desktop application - start-up speed, rich response, independence from connectivity. And then, there is the third scenario: provide an application that can launch either way.

Background

Habanero was launched in June 2007 as free open source, primarily as an object relational mapping (ORM) framework for .NET that went beyond the usual ORM data focus to provide a runtime-generated presentation layer. Peer programmed using test-driven development, Habanero simply put into practice the full development pipeline. Habanero's developers, Chillisoft, have been producing custom software for industrial and business clients for many years, and Habanero encapsulated most of the common routines.

Chillisoft had always had a preference for developing desktop applications, owing to the cumbersome nature of working with ASP.NET. That perspective changed when Gizmox released Visual WebGui. Simulating both the API and the look-and-feel of WinForms in a web environment, Visual WebGui (VWG) provided the platform for developers to design a web application using a desktop methodology. Even better, with its similarities to WinForms behaviour, the Habanero presentation layer could be adapted to accommodate VWG. Habanero version 2 was released in July 2008 as a full Enterprise Application Framework with support for both WinForms and VWG generation.

Setting up the Tiers

The attached project provides a simple application for a computer parts supplier called Replace-IT. We'll step through the creation of this application, and you can use the sample code as a reference point.

A key feature of ORM is the extraction of the business object layer away from the database. In the sample code, we provide a Firebird database, but you could just as easily store the data in MySQL or SQL Server. We've provided a BusinessObject (BO) project under the solution, where all the domain objects are stored, such as ComputerPart, OrderItem, and ComputerPartType.

In our example, we have created a generic UI layer that builds the appropriate user interface for the release environment. Finally, two execution projects are provided, one for a desktop application and one for a VWG web application.

Running the Applications

When you do open the sample projects, you'll need to set the absolute directory to the Firebird database file. Under the Replace_it project, open app.config and set the database value to include the full path. Do likewise for web.config under the Replace_it.VWG project. To choose which environment to run under, simply right-click on either Replace_it or Replace_it.VWG and choose "Set as Startup Project". Run both applications, and you'll see that you get almost the same result with both WinForms and your internet browser.

Introducing IControlFactory

The obvious question is: how can a generic UI layer produce specifically typed controls? And also, how can custom behaviour be assigned to a control so that it behaves differently in different environments? For instance, the web environment places a premium on data acquisition, so VWG provides a paging mechanism that prevents the user from loading a thousand items on one grid, while a desktop application typically has access to a database server through a faster connection.

To solve these challenges, Habanero uses a control factory that produces a control of a specific type with specific behaviour. A generic UI control manager (such as a form) can now create specific controls by calling a "create" method on a control factory that has been passed to it, rather than using a specifically typed constructor.

Let's look at an example from the code. The following class produces a form to display a grid of computer parts:

The appropriate ControlFactory is passed through the constructor, and is used to create a ReadOnlyGridControl, which is a Habanero control that lists objects in a grid and provides tools to filter the data and to edit individual items in a popup form. Note that the control is instantiated using the interface IReadOnlyGridControl.

Creating Generic Main Menus

There is an intermediate class called UIManager that provides a main menu for the application, instantiating the corresponding control for each menu item, passing through the control factory. In the executable project, UIManagerWin inherits from UIManager, and instantiates ControlFactoryWin for use by the UI layer. The final step involves adding code to the startup form/class for the project. In the WinForms executable, you would add two lines of code to Program.cs which instantiates a typed form and passes it through to the UIManager.

The Process Flow in Code

Let's follow this process from the top with code examples. First, in Program.cs:

The final step is to instantiate the right control, as illustrated in the example grid code presented earlier.

What is the Development Cost?

There are three big benefits to this approach for designing user interfaces. Firstly, you can release in multiple environments. Secondly, this design lends itself to test-driven development of the presentation layer, a challenge that developers frequently avoid. Thirdly, the ControlFactory introduces a considerable degree in flexibility. While we've discussed the differences between web and desktop, even a single desktop application can use different ControlFactorys to produce different behaviour on the same control at different times.

As for costs, the Visual Studio designer does not support this kind of typing (and some will, of course, say: good riddance to the designer!). Secondly, there's a slight change in development patterns. Instead of instantiating controls, you're calling through to the ControlFactory and assigning to an interface instead of a specific type.

On the whole, the amount of code presented above provides a fairly simple application foundation. From here, the majority of your work goes into extending the generic UI layer with all the controls you need to provide a working application.

Habanero is free open source, and you may want to view some of the interesting code structures used to achieve this outcome. You can download the full package and access tutorials and support at the official website.

Comments and Discussions

sir,an error has to occurred to apply for run execute command,a project with an output type of class library cannot be started directly
in order to debug this project to this solution which references the library project,set the executable project as the startup project.

I saw the FireStarter tool come with Habanero v2.0.
Also FireStarter doesn't support Firebird to generate 'ClassDef from database'.
How can i update FireStarter to support Firebird? must upgrade FireStarter to Habanero v2.0.1?

Firestarter doesn't yet support all the databases that Habanero supports. This only has implications for reverse engineering from the database. What you can do is to temporarily port your database to another vendor like MySQL, Access or Sql Server and generate from there.

We are currently rewriting Firestarter to support new features like form generation and database generation, but it will be a while till that version is released.

I will speak to the team about adding support for Firestarter, as I'm not sure it's a 5-minute job.

We've put our hands to the plow and added Firebird support to Firestarter. The modified version is now included with the full Habanero package download at the official website (www.chillisoft.co.za/habanero).

We're certainly heading that way. Our development focus with Habanero has been on getting the functionality rock solid (ORM, data layer, robust testing, ease of use) and now we will be paying additional attention to toolsets like WPF that provide enhanced looks.

Basically, the challenge was to extract the logic to support multiple controls, and now that we've laid that platform, support for WPF will come a lot more easily.

Just an additional point of interest. The key feature of Habanero's presentation layer is that it automates much of the work involved in setting up your controls and binding them, pre-constructing some of the typical UI screens. As such, WPF with Habanero will be a lot better than WPF on its own.

I have a feeling that when someone tries to accomodate two different UI approaches into one, they might end up with the bad UI in both cases. I would better spend more time on tiering the application properly, so having different types of client wouldn't be so painful.

You might be right Alex - the truth is that this kind of development is fairly new and we still have to settle into best practices, which will probably only really get formulated with hands-on coding and deployment. With a bit more hindsight we might find some new surprises cropping up, both good and bad.

Very good article Eric. I'm a big fan of Habanero and this feature is definitely a big thumbs up.
Can't wait to start my next project and take advantage of this and all the other new features of the latest Habanero release.
Viva Habanero, Viva!!!

I was very skeptical in the beginning. But after 14 days of development in this framework
I am really suprised with speed and comfort which is comparable to classical desktop development.
This solution is perhaps mostly for business database applications ( this is what I do ) and I say it is rally, really good solution. No shity CSS, Javascript, HTML.
Great WORK !