ZenArchitect.NL (Henk van Dijken)

the art of model-driven code generation

Generating Business Entities from a Database Model

without comments

After Generating Database Objects from an UML Model you are desperately in need of a data access layer (DAL) and a business logic layer (BLL). Today we are going to generate the famous Business Entities.

After executing that funny sql-script we generated from UML previously, you are the proud owner of a database. Since I am a great fan of MyGeneration we will use a similar approach and scrape the meta model from the database.

To be able to do so, the code generator needs some input. First, it has to know which tables must be read (filter) and where to find the tables (database) to extract meta data from.

image

At this point the generator has enough information to read the data model and generate code. The list of tables are traversed and classes are created. Within each table the meta data of its columns are extracted.

image

Since we – at this moment – are not able to automagically convert data types, we also need a support routine for conversion of database type to its C#.NET equivalent. This is done by append some script to the template.

image

Finally, using this template, the following code is generated from the selected data model.

/*------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     ZaosGenerator Version: 1.0.0.0
//     Template Author      : Henk van Dijken
//     Generation Date      : 29-7-2010 21:12:15
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;

namespace ZenArchitect2.BusinessEntities
{
	public partial class TestParent
	{
		public TestParent() {}

		private int TestParentIDField;
		public int TestParentID
		{
			get { return this.TestParentIDField; }
			set { this.TestParentIDField = value; }
		}

		private string TestParentName1Field;
		public varchar TestParentName1
		{
			get { return this.TestParentName1Field; }
			set { this.TestParentName1Field = value; }
		}

		private int TestParentNumberField;
		public int TestParentNumber
		{
			get { return this.TestParentNumberField; }
			set { this.TestParentNumberField = value; }
		}

	}

	public partial class TestChild
	{
		public TestChild() {}

		private int TestChildIDField;
		public int TestChildID
		{
			get { return this.TestChildIDField; }
			set { this.TestChildIDField = value; }
		}

		private int TestParentIDField;
		public int TestParentID
		{
			get { return this.TestParentIDField; }
			set { this.TestParentIDField = value; }
		}

		private string TestChildNameField;
		public varchar TestChildName
		{
			get { return this.TestChildNameField; }
			set { this.TestChildNameField = value; }
		}

	}

}

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Written by Henk van Dijken

July 29th, 2010 at 8:01 pm

Posted in Code Generation

Tagged with

Leave a Reply