Angel \”Java\” Lopez on Blog

September 30, 2008

Code Generation for LINQ and C# 3.0 with AjGenesis

Filed under: .NET, AjGenesis, ASP.NET, C Sharp, Code Generation — ajlopez @ 8:18 am

AjGenesis, my open source code generation project, uses a user-defined model. You can define tasks and templates, to transform the model to code and text files. You can write your own model, new tasks and transformation. You can invoke custom code or .NET objects, in the middle of the generation process.

As the model is user-defined, the system is not limited to one kind of projects. It’s based in templates and tasks, and then, the generated text artifacts are not bound to only one technology. You can generate files for any technology, framework, line of business and platform. You can generate DDL scripts, or a full base application, or anything between. It’s your decision.

When I began to write and use the system, .NET 2.0 didn’t exist. When the new technology became public, I wrote additional tasks and templates to take advantage of new features, as master pages, themes and new ASP.NET controls, without touching the core code of the AjGenesis project. Once the new templates and tasks were written, the project began to generate web applications for ASP.NET 2.x.

Now, as a proof of concept, I wrote a new example, that generates code using LINQ, the new kid on the microsoft block for .NET 3.x. The result was published as in the page AjGenesis Examples in CodePlex. To run the example, you need the current release 0.5 (includes source code, initial examples, an binaries). (There is a new version under development in the code repository).

The example

There is only one model in the example folders: AjFirstExample, with a simple XML model describing two plain entities, Customers and Suppliers. It’s the same model I used in previous examples.

<Project> <Name>AjFirstExample</Name> <Description>First Example using AjGenesis</Description> <Prefix>AjFE</Prefix> <Domain>com.ajlopez</Domain> <CompanyName>ajlopez</CompanyName> <Model> <Entities> <Entity Source="Entities/Customer.xml"/> <Entity Source="Entities/Supplier.xml"/> </Entities> <Lists> <List Entity="Customer"/> <List Entity="Supplier"/> </Lists> <Forms> <Form Entity="Customer"/> <Form Entity="Supplier"/> </Forms> <Views> <View Entity="Customer"/> <View Entity="Supplier"/> </Views> </Model> </Project>

Templates folder contains two sets of .tpl files (AjGenesis templates). MsSql templates are used in DDL script generation. Inside CSharp3 folder, there are new templates that generates LINQ DataContext, and simple classes.

Running the example

In the main directory, there is a command file


that loads the model in memory, technology model, and execute two tasks, creating code and DDL scripts.

The solution resides in a new directory, Build, with source code and MS SQL scripts:

Build\AjFirstExample\CSharp3\Sql\CreateDatabase.sql creates database and table, fragment:

create database AjFirstExampleNet3 go ... use AjFirstExampleNet3 go ... -- -- Entity: Customer -- Description: Customer Entity -- if exists (select name from sysobjects where name = 'Customer' and type = 'U') drop table Customer go create table Customer ( [Id] [uniqueidentifier] not null CONSTRAINT [DF_Customer_id] DEFAULT (newid()), [Name] nvarchar(200), [Address] nvarchar(2048), [Notes] nvarchar(2048) CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) go

The solution can be loaded in VS2008:

AjFirstExampleDataContext.cs is a simple LINQ DataContext:

public partial class AjFirstExampleDataContext : System.Data.Linq.DataContext { public AjFirstExampleDataContext() .... public Table<Customer> Customers; public Table<Supplier> Suppliers; }


You can replace this DataContext by one generated via Linq To SQL wizard, in Visual Studio. There is an option in the technology model to specify the generation of this custom DataContext. But you can drop it, and replace by a more complete DataContext: the project will compile with both approach.

The solution has three projects: data, services and ASP.NET presentation. The initial page is spartan (you know, I’m a developer, not a graphic designer… :-)):

From Administration option, you can browse, add, edit and delete customers and suppliers:


Next steps

I want to convert other previous examples to this new templates (AjTest model is the more challenging, with one to many relations and many entities). It could be interesting to generate ASP.NET MVC pages, instead of plain ASP.NET. And some testing infrastructure. I’m taking the code of ScrumLite solution as a base example to follow.

Angel “Java” Lopez

Create a free website or blog at