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 AjGenesisExamplesNet3.zip 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

makeajfirstexample.cmd

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
http://www.ajlopez.com/en
http://delicious.com/ajlopez
http://twitter.com/ajlopez

4 Comments »

  1. [...] Another main point: it could be modified to generate the full VS solution, or to generate a solution in other languages (an example generating a full VS solution:Code Generation for LINQ and C# 3.0 with AjGenesis) [...]

    Pingback by State Machines in C# 3.0 Using AjGenesis Templates « Angel “Java” Lopez on Blog — October 20, 2008 @ 8:20 am

  2. [...] Otro punto a destacar: el ejemplo podría ser modificado para generar la solución Visual Studio completa, o para generar una solución en otro lenguaje (un ejemplo generando una solución VS completa:Code Generation for LINQ and C# 3.0 with AjGenesis) [...]

    Pingback by Máquinas de Estado en C# 3.0 usando AjGenesis | Buanzolandia — October 21, 2008 @ 10:42 am

  3. [...] Otro punto a destacar: el ejemplo podr??a ser modificado para generar la soluci??n Visual Studio completa, o para generar una soluci??n en otro lenguaje (un ejemplo generando una soluci??n VS completa:Code Generation for LINQ and C# 3.0 with AjGenesis) [...]

    Pingback by M??quinas de Estado en C# 3.0 usando AjGenesis | Buanzolandia — October 24, 2008 @ 1:02 am

  4. i am pankaj your business halp software code ajfast example 1000000000 cash

    Comment by pankaj — July 18, 2009 @ 12:13 pm


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 66 other followers

%d bloggers like this: