Building an Application using AjGenesis (Part 2)

This is the second post in the serie, explaining how to build an application using AjGenesis, my open source code generation project. The previous post in this serie:

Building An Application Using AjGenesis (Part 1)

In this second part, I will generate two simple files, from the model defined in the last post.

The files for this second steps can be downloaded from

You need to download the AjGenesis binaries (trunk version) from, expand them, AND add the bin folder to your path.

There is the command PrintEntities.cmd:

AjGenesis.Console Project.xml PrintEntities.ajg

AjGenesis.Console is the console program that launch the AjGenesis process. It receives many arguments. If the argument is an .xml file, its content is loaded in memory as an AjGenesis model (explained below). If the argument is an .ajg, the content of the file is a task, a list of commands to execute in AjGenesis. The commands are written in an interpreted language, named AjBasic.

This is the content of Project.xml (read previous post for more details):

  <Description>Building an Application using AjGenesis</Description>
      <Entity Source="Customer.xml"/>
      <Entity Source="Supplier.xml"/>

Once the model is loaded in memory, there is a public variable Project with dynamic properties loaded for each attribute and element. From AjBasic, we can access Project, Project.Model, and Project.Model.Entities.

The content of PrintEntities.ajg:

for each Entity in Project.Model.Entities
  Message "Entity " & Entity.Name
  for each Property in Entity.Properties
    Message "   Property ${Property.Name}"
  end for
end for

Note the use of for each, an AjBasic command. Message is a command to print something in the output console. The above code shows one of the features of AjBasic: string expansion. That is, the string:

“ Property ${Property.Name}”

is expanded in runtime to the content of the expression between ${ and }. If you run


the output is:

Entity Customer

   Property Name

   Property Address

Entity Supplier

   Property Name

   Property Address

Now, let generate one class file in C# for each entity in model. This is the GenerateClasses.cmd:

AjGenesis.Console Project.xml GenerateClasses.ajg

This command loads Project.xml in memory, and execute the AjGenesis task:

for each Entity in Project.Model.Entities
  TransformerManager.Transform("EntityClass.tpl", "${Entity.Name}.cs", Environment)
end for

There are few public helper objects, predefined in AjGenesis environment. One of them is TransformerManager, that takes three arguments:

– The template file name

– The name of the file to generate

– The current Environment (name/values of global variables and functions, usually pointed by Environment global variable)

This is the first template in this tutorial series, EntityClass.tpl:

// Entity Class, generated with AjGenesis (
public class ${Entity.Name}
  for each Property in Entity.Properties
  public string ${Property.Name} { get; set; }
  end for

A template is a text file with the content to generate, and with fragments between <# and #> that are filled with AjBasic commands. In the above template, the for each command processes each of the properties in the current entity. As in the string expansion, expressions between ${ and } are evaluated and its results are inserted in the output of template process.

This is the output generated for Customer.cs using EntityClass.tpl template:

// Entity Class, generated with AjGenesis (
public class Customer
  public string Name { get; set; }
  public string Address { get; set; }

There are still some simplifications: no namespace, all properties are strings, no id for entities, etc… I will address these issues, step by step, in future posts. Next steps: generate, from the same model, C#, VB.NET or Java class files.

Keep tuned!

Angel “Java” Lopez

3 thoughts on “Building an Application using AjGenesis (Part 2)

  1. Pingback: Building an Application Using AjGenesis (Part 3) « Angel “Java” Lopez on Blog

  2. Pingback: Armando una aplicación usando AjGenesis (Parte 3) - Angel "Java" Lopez

  3. Pingback: Building an Application Using AjGenesis (Part 4) « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s