Models for Code Generation in AjGenesis

Today, I want to enumerate and short review the options you have to write a model in AjGenesis, my open source code generation project.

One of the basic ideas of AjGenesis is to have a free model as an starting point. It’s not a fixed one. What it means? It means that you can create and use your own model. A concrete example, if you want to model a "Hello, World”, you can start writing a simple model like this:

<Project Company="ajlopez.com">
   <Message>Hello, World</Message>
</Project>

If you want more info, extends it as in:

<Project>
	<Messages>
		<Message>Hello, World One</Message>
		<Message>Hello, World Two</Message>
		<Message>Hello, World Three</Message>
	</Messages>
</Project>

More info about this particular example in my post A Hello World Application in AjGenesis.

But XML is only the serialized version of the model. You can write it as text:

Project
	Messages
		Message = "Hello, World One"
		Message = "Hello, World Two"
		Message = "Hello, World Three"
	End Messages
End Project

More info about textual models at

Textual Model for Code Generation in AjGenesis

If you want to describe an Entity in your domain model, you can write something like this in free XML:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Entity>
	<Name>Employee</Name>
	<Description>Employee</Description>
	<SetName>Employees</SetName>
	<Descriptor>Employee</Descriptor>
	<SetDescriptor>Employees</SetDescriptor>
	<SqlTable>employees</SqlTable>
	<Properties>
		<Property>
			<Name>Id</Name>
			<Description>Id</Description>
			<Type>Id</Type>
			<SqlType>int</SqlType>
		</Property>
		<Property>
			<Name>EmployeeCode</Name>
			<Description>Code</Description>
			<Type>Text</Type>
			<SqlType>varchar(20)</SqlType>
		</Property>
		<Property>
			<Name>LastName</Name>
			<Description>Last Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>FirstName</Name>
			<Description>First Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>IdDepartment</Name>
			<Description>Department</Description>
			<Type>IdRef</Type>
			<SqlType>int</SqlType>
			<Reference>Department</Reference>
		</Property>
	</Properties>
</Entity>

A more detailed description of use of such entity model at Application Generation using AjGenesis.

But if read XML hurts your eyes, you can use a in free text form (same as above example, but abbreviated)

Entity Employee
    Description = "Employee"
    SetName = "Employees"
    ...
    Properties
        Property Id
            ...
        End Property
            ...
        Property Name
        End Property
    End Properties
End Entity

 

Not yet implemented, I’m going to add spreedsheets as models:

I wrote a short post about this idea: Another Model for AjGenesis

But sometimes, you want to use an existing source for model metadata. And with AjGenesis, you can! One of the most “popular” starting pseudo-models is the database. In my opinion, it’s not the ideal model, but, depending on the project and your needs, it could be a pragmatic solution (remember, AjGenesis and code generation is a pragmactic solution to some problems in software development, so, I don’t hesitate to adopt pragmatic sources, but be aware of their limitations).

I wrote about some examples at: Generating a Model From the Database Using AjGenesis and AjGenesis: Generating the Model from the Database

AjGenesis was born and designed to survive “fashion development”: it’s not oriented to a particular technology, platform, framework or language. It can generate everything that is a text artifact: Java, C#, VB.NET, PHP, config files, xml, database scripts, even COBOL. You can write tasks and templates to do what you want to generate, at any moment. You can start small, and grow up, adding more abstractions and tricks. But tasks and templates are topics for another blog. Said that, you can use as a model the compiled domain. There is a description of an example written by Dario Quintana using .NET at:

AjGenesis: Generating the model from assemblies

In Java world, it was used to generate code from annotations in code (remember XDocLet). Or from configuration files: hbm2java tools in Hibernate. NHibernate, the most popular .NET open source Object Relational Mapping library, lacks such tools. As in Java, AjGenesis can use meta tags embedded in .hbm mapping files, to generate code. See:

Generating Code with AjGenesis using NHibernate Hbm Mapping Files

(I have a more complete example in trunk, that generates a complete solution using ASP.NET and NHibernate).

You can download the project from the source code trunk at:

http://www.codeplex.com/ajlopez

Next topics: tasks, templates, abstractions vs technicalities, in-memory models, model to model transformations, completing the model, decisions, expert systems and artificial intelligence applied to code generation and AjGenesis.

Stay tuned!

Angel "Java" Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez