Model Driven Code Generation with Wurblets and Templates
The Model Driven Development (MDD) is based on the notion that an abstract platform independent model can be transformed into more specific models and/or directly into platform dependent source code. In this example, collaborating wurblets generate Java code for a data abstraction layer according to a data model.The Template
Although the wurbelizer is not a template engine, templating can easily be achieved by creating a file that contains a set of wurblet anchors which will describe the generated part of the template. Most IDEs provide at least some rudimentary template support which in combination with the wurbelizer can be upgraded to a full-featured template processor. As an example, the Netbeans-template for a Tentackle Application Database Object, an AppDbObject, looks like this:The Netbeans-variable __NAME__ will be replaced by the actual Java classname when the new file is created with Netbeans from a template. The Wurbelizer-variables and the model must be provided by the developer, which is shown in the next step:
From the Model to the Source
The database wurblets expect the model to be specified in two parts: one for the attribute mapping ($mapping) and the other for the relations between the database objects ($relations). It is up to the developer whether to put these specifications either in regular files or to store the model directly within the source file to be wurbelized. Knowing that the code is the truth, we prefer the latter variant.Below is a sample class from a real project (a node of an xml-document stored in a database):
To create a new class, all we have to do is to specify the model ($mapping and $relations), a few properties like $tablename and some application specific anchors for wurblets like AppDbSelectList. All the rest is generated by the wurblets. Isn't that easy?
Notes
- The generated code is specific to the Tentackle framework that provides its own O/R-mapper. It is based on a pure object oriented approach, i.e. there is no bytecode instrumentation or generation of proxy classes at runtime behind the scenes. The mapping is based on polymorphism and is solely achieved by implementing and/or overriding methods.
- Although the example is tentackle specific, it demonstrates a typical pattern for using the wurbelizer: abstract classes like AppDbObject implement the generic part and wurblets override and/or implement certain methods which are specific to the model.


