Angel \”Java\” Lopez on Blog

July 11, 2010

Building an Application Using AjGenesis (Part 3)

Filed under: AjGenesis, Code Generation, Open Source Projects — ajlopez @ 9:08 pm

This is the third part in this tutorial post series, about building an application using AjGenesis, my open source code generation project. Previous posts:

Building An Application Using AjGenesis (Part 1)
Building An Application Using AjGenesis (Part 2)

The code of this post can be downloaded from AppExampleStep03.zip.

You need the latest AjGenesis binaries. You can get them from AjGenesisTrunkBinaries.zip. (the full source code is at AjGenesis Codeplex repository). You should add the bin directory to your path, to run the examples of this post.

This time, I improve the code generation process. The model is the same, but now, the GenerateClasses.cmd:

AjGenesis.Console Project.xml CompleteModelCSharp.ajg GenerateClasses.ajg

has a new parameter: an intermediate task to execute, CompleteModelCSharp.ajg:

' Some functions
' Name to use for variables
function CSharpVariableName(name)
  firstletter = name.Substring(0,1)
  
  return firstletter.ToLower() & name.Substring(1)
end function
' Name to use for Classes, Properties..
function CSharpName(name)
  firstletter = name.Substring(0,1)
  
  return firstletter.ToUpper() & name.Substring(1)
end function
function CSharpType(type)
  type = type.ToLower()
  
  if type="text" then
    return "string"
  end if
  
  if type="integer" then
    return "int"
  end if
  
  return type
end function
' Set namespace to use in CSharp code
if not Project.CSharp.Namespace then
  Project.CSharp.Namespace = CSharpName(Project.Name)
end if
' Complete Entities
for each Entity in Project.Model.Entities
  ' Set the variable name to use for an entity
  if not Entity.CSharp.VariableName then
    Entity.CSharp.VariableName = CSharpVariableName(Entity.Name)
  end if
  
  for each Property in Entity.Properties
    ' Set the CSharp to use in each property
    if not Property.CSharp.Type then
      Property.CSharp.Type = CSharpType(Property.Type)
    end if
  end for
end for

New things: the definition of functions, the use of .NET method as .ToUpper(), and the completion of the model, like in:

if not Project.CSharp.Namespace then
  Project.CSharp.Namespace = CSharpName(Project.Name)
end if

I added some properties in the model-in-memory. If you don't define a namespace to use, one is created. Note the if Project.CSharp.Namespace is not defined and Project.CSharp is nothing, no exception is raised: undefined O.A.B is evaluated as nothing, and nothing is false in AjBasic (strategy borrowed from PHP).

Then, the above code enriches the model to use, taking decisions related to C#. In next posts, I will write similar code for other technologies. But it's worth to mention that this approach able to write a model, and then, when the technology is chosen, enrich them with tech info.

There is a fragment that add C# type to properties:

  for each Property in Entity.Properties
    ' Set the CSharp to use in each property
    if not Property.CSharp.Type then
      Property.CSharp.Type = CSharpType(Property.Type)
    end if
  end for

The second task is the same of the previous post, GenerateClasses.ajg:

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

The template EntityClass.tpl:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
namespace ${Project.CSharp.Namespace} {
  public class ${Entity.Name}
  {
<#
  for each Property in Entity.Properties
#>
    public ${Property.CSharp.Type} ${Property.Name} { get; set; }
<#
  end for
#>
  }
}

now uses the new properties in model.

This is the Customer.cs generate file:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
namespace AjApp {
public class Customer
  {
    public string Name { get; set; }
    public string Address { get; set; }
  }
}

Next steps: write more tasks and templates to generate Java, VB.NET files.

As usual, I should mention this is a tutorial series. In the real life, I prefer to adopt another approach, described in my previous post AjGenetizing An Application.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] Building An Application Using AjGenesis (Part 1) Building An Application Using AjGenesis (Part 2) Building an Application Using AjGenesis (Part 3) [...]

    Pingback by Building an Application Using AjGenesis (Part 4) « Angel “Java” Lopez on Blog — July 14, 2010 @ 7:28 pm

  2. [...] Building An Application Using AjGenesis (Part 2) Armando una Aplicación usando AjGenesis (Parte 2) Building an Application Using AjGenesis (Part 3) Armando una Aplicación usando AjGenesis (Parte 3) Building an Application Using AjGenesis (Part 4) [...]

    Pingback by Armando una aplicación usando AjGenesis (Parte 5) - Angel "Java" Lopez — September 14, 2010 @ 8:57 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: