Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

eXtremecode ASP.NET Generator Introduction

4.33/5 (3 votes)
8 Jul 2010CPOL5 min read 20.7K   408  
Xtremecode or eX ASP.Net Generator is simply a tool which takes a collection of database connections as an input and produces a well-formed ASP.NET application.

Image 1

Overview

eXtremecode or eX ASP.NET Generator is simply a tool which takes a collection of database connections as an input and produces a well-formed ASP.NET application.

Image 2

eX Generator consists of two applications. One application is eXtremecode Definition Generator (eXDG), for generating database schema which takes a collection of database connections as an input and gives databases schema (definition) in the form of an XML file. And the second application is eXtremecode Generator (eXG), for generating final code e.g., ASP.NET application which takes the generated database definitions and predefined templates as an input and gives the final result (currently an ASP.NET application).

eXtremecode Definition Generator (eXDG)

eXDG takes a collection of connections which are defined in an XML file. Path of that XML file can be configured from eXDG's application configuration file.

XML
<configuration>
  <appSettings>
    <add key="ConnectionsPath" value="Connections.config" />

Currently, eXDG supports SQLServer, Oracle and MySQL databases. Example of connections defining XML is shown below:

XML
<connections>
  <connection name="SQLServerConn" 
     connectionString="Persist Security Info=False;User ID=sa;
     Initial Catalog=DBName;Data Source=ServerName;Password=password" 
     type="SQLServer" isDefault="false" encrypted="false"/>
  <connection name="SQLServer2005Conn" 
      connectionString="Persist Security Info=False;User ID=sa;
      Initial Catalog=DBName;Data Source=ServerName;Password=password" 
      type="SQLServer05" isDefault="false" encrypted="false"/>
  <connection name="OracleConn" 
      connectionString="Password=password;User ID=userName;
      Data Source=ServerName;Persist Security Info=True" 
      type="Oracle" isDefault="false" encrypted="false"/>
  <connection name="MySQLConn" 
      connectionString="server=ServerName;User Id=userId;
      database=dbName;Persist Security Info=True;Password=password;Port=3306" 
      type="MySQL" isDefault="true" encrypted="false"/>
</connections>

Connection Attributes

  • Name: For unique identification of a connection..
  • Connection String: Simply defining connectivity to database. The format of this string depends on type of the connection.
    For using MySql Connection, it is required to install MySQL .Net connector first. 
    For complete details about MySQL connectivity with .Net application,
    please visit MySQL official site.
  • Type: For telling system about type of the database (SQL server, MySQL, Oracle).
  • IsDefault: If we have more than a connection in our application, we can set one connection as a default. If we don't provide the name of the connection while querying to database, the System will pass that query to default connection for execution.
  • Encrypted: For telling the system about the provided connection string, whether it is encrypted or not .

Running eXDG

Once we define all required connections, now we can run eXDG application. At the time of form loading, the system will read all defined connections and populate a grid with a list of tables and views of respective databases.

Image 3

Option, Select Related Entities

Normally, all database tables are not required in an application. If we have a large number of tables in our database, it gets difficult to select required entities. So if we check "related entities" check box and then select a table from grid, the system will select all related tables of that table for us.

Option, Refresh Entities

If "Refresh Entities" check box is unchecked, when we regenerate definition/schema of entities, the system will generate definition for only new selected entities (tables/views) and already generated entities selection will be ignored.

Sometimes, we create new tables in database which will have relations with old tables/entities. In that case, we want to regenerate old entities so relations of new created tables can also be incorporated with old entity. In order to regenerate old entities, we will keep "Refresh Entities" check box checked.

Generated Definition/Schema Files

Once we click generate button, the system will generate two XML files at defined paths. File paths can be configured from application configuration file.

XML
<add key="EntityDefinitionPath" value="eXDefinition.xml" />
<add key="EntityCustomizedPath" value="eXCustomDefinition.xml" />
"EnitiyDefinitionPath" is for Xml file which contains whole schema of selected entities.

Here is the sample:

XML
<entity name="Promotion" accessorName="SQLServerConn" 
type="db" table="Promotion" view="Promotion">
    <fields>
      <field name="IsActive" datatype="bit" 
      type="native" key="false" computed="false" 
      unique="false" nullable="false" autoGenerated="false" />
      <field name="Stock" datatype="int" 
      type="native" key="false" computed="false" 
      unique="false" nullable="false" autoGenerated="false" />
      <field name="Title" datatype="text" 
      type="native" key="false" 
      computed="false" unique="false" 
      nullable="true" autoGenerated="false" />
      <field name="LaunchDate" datatype="datetime" 
      type="native" key="false" computed="false" 
      unique="false" nullable="false" autoGenerated="false" />
      <field name="PromotionId" datatype="int" 
      type="native" key="true" computed="true" 
      unique="true" nullable="false" autoGenerated="false" />
      <field name="PromotionType" datatype="int" 
      type="native" key="false" computed="false" 
      unique="false" nullable="false" autoGenerated="false" />
      ........
      ........

    </fields>
    <children>
      <entityReference name="CustomerCategoryPromotion">
        <relationalFields referenceField="PromotionId" 
        nativeField="PromotionId" />
      </entityReference>
      <entityReference name="PromotionItem">
        <relationalFields referenceField="PromotionId" 
        nativeField="PromotionId" />
      </entityReference>
      <entityReference name="OrderItem">
        <relationalFields referenceField="PromotionId" 
        nativeField="PromotionId" />
      </entityReference>
    </children>
    <parents>
      <entityReference name="PromotionType">
        <relationalFields referenceField="PromotionTypeId" 
        nativeField="PromotionType" />
      </entityReference>
    </parents>
</entity>
"EntityCustomizedPath"

is for an XML file which contains only the name of selected entities with their fields. It is used for entity level customization of generated code. Here is the sample.

XML
<entity name="Promotion">
    <fields>
      <field name="IsActive" />
      <field name="Stock"/>
      <field name="Title" />
      <field name="LaunchDate" />
      <field name="PromotionId"  />
      <field name="PromotionType" />
      ........
      ........

    </fields>
</entity>

I will explain schema attributes and entity level customization in detail, in advanced articles.

eXtremecode Generator (eXG)

Once we generate definition and do required customization, now we can generate final code with the help of predefined templates.

Generation of ASP.NET application is actually based on defined templates.
Any kind of  application can be generated by defining required templates.
Currently, I have written templates for ASP.Net C# application only. 
Generating language (the language in which templates are written) is C#.

Repository.xml

All defined templates will be controlled or managed by "Repository.xml" file. From Repository.xml file, we can configure the following:

  • Name of the project
  • Name of schema and custom entity files
  • Security settings
  • Names of template files (Session Files)
  • Defining generated files
  • Managing custom code of developer

Repository management is an advanced topic so I will explain it later in more details. Here is an example of Repository.xml file.

XML
<?xml version="1.0" encoding="utf-8" ?>
<Repository projectName="MYSQLTest" 
entityDefinitionFile= "EntityDefinitions.xml" 
entityCustomFile= "EntityCustomized.xml">
 <Security>
  <UserLogin entity="Duplicate-Users" 
      userIdField="UserId" 
      userNameField="UserName" passwordField="Password" 
      defaultRoleIdField="DefaultGroupId" 
      relatedRolesEntity="Duplicate-user_groups" 
      roleIdField="GroupId" />
  <UserRole entity="Duplicate-Groups" 
  roleIdField="GroupId" roleNameField="GroupName" 
      homeModuleIdField="defaultModuleId" 
      relatedModulesEntity="Duplicate-group_modules" 
      moduleIdField="ModuleId"  />
 </Security>
 
 <SessionFiles>
  <!--<File name="report.cs" />
  <File name="share-script.cs" />-->
  <File name="BusinessObject.cs" />
  <File name="IncludedCode.cs" />
  <File name="BODataSet.cs" />
  <File name="BOCommon.cs" />
  <File name="BOList.aspx.cs" />
  <File name="BOList.aspx" />
  <File name="BO.resx" />
  <File name="BOEdit.aspx" />
  <File name="BOEdit.aspx.cs" />
  <File name="Login.aspx.cs" />
  <File name="Login.aspx" />
  <File name="UserProfile.cs" />
  <File name="Default.aspx" />
  <File name="Default.aspx.cs" />
  <File name="sitemap.xml" />
  <File name="Global.asax" />
  <File name="ChangePassword.aspx.cs" />
  <File name="ChangePassword.aspx" />
 </SessionFiles>
 
 <GeneratedFiles>
  <!--Edit Page-->
  <File name="\aspx\{0}BOEdit.aspx.cs" 
  type="table" copyFile="" cond="" 
   mappCustomCode="true" enabled="true" 
   sessionList="BOEditWholeClass" />
   
   <File name="\aspx\{0}BOEdit.aspx" 
   type="table" copyFile="" cond="" 
   mappCustomCode="false"  enabled="true"
   sessionList="ASPXBOEditWholeClass" />
  
  <!--Business Objects-->
  <File name="\App_Code\BusinessObjects\{0}BO.cs" 
  type="table" copyFile="" cond="" 
   mappCustomCode="false"  enabled="true"
   sessionList="BUZWholeClass" />

  <File name="\App_Code\BusinessObjects\{0}BOCol.cs" 
  type="table" copyFile="" cond=""
   mappCustomCode="true"  enabled="true"
   sessionList="BUZColWholeClass" />  
  
  <!--List Page-->
  <File name="\aspx\{0}BOList.aspx.cs" 
  type="table" copyFile="" cond="" 
   mappCustomCode="true"  enabled="true"
   sessionList="BOListWholeClass" />
   
   <File name="\aspx\{0}BOList.aspx" 
   type="table" copyFile="" cond="" 
   mappCustomCode="true"  enabled="true"
   sessionList="ASPXBOListWholeClass" />
   
   <!--Resource-->
   <File name="\App_GlobalResources\BusinessObjects\{0}BO.resx" type="table" 
   copyFile="" cond="" 
   mappCustomCode="true"  enabled="true"
   sessionList="ResourceBOListWholeClass" />  
   
   <!-- Business Base -->
   <File name="\App_Code\DataTables\{0}DataTable.cs" 
   type="table" copyFile="" cond=""
   mappCustomCode="true" enabled="true"
   sessionList="BUZDataTableWholeClass" />
   
   <File name="\App_Code\BODataSet.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="BUZDataSetWholeClass" />
   
   <File name="App_Code\BOCommon.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="BOCommonWholeClass" /> 
   
   <File name="Login.aspx.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="LoginWholeClass" /> 
   
   <File name="Login.aspx" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="ASPXLoginWholeClass" /> 
   
   <File name="ChangePassword.aspx.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="ChangePasswordWholeClass" /> 
   
   <File name="ChangePassword.aspx" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="ASPXChangePasswordWholeClass" />
   
   
   <File name="Default.aspx" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="ASPXDefaultWholeClass" /> 
   
   <File name="Default.aspx.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="DefaultWholeClass" /> 
   
   <File name="Web.sitemap" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="SitemapWholeClass" /> 
   
   <File name="Global.asax" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="GlobalASAXWholeClass" />
   
   
   <File name="App_Code/UserProfile.cs" type="once" 
   copyFile="" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="UserProfileWholeClass" /> 
   
   <File name="unchangedFiles" type="CopyFolder" 
   copyFile="WebSite" cond="" enabled="true"
   mappCustomCode="true" 
   sessionList="UserProfileWholeClass" /> 
      
  <!--MappCustomCode= [false,true]  this option can b used when avoiding custom code conflict 
   but user code will be lost. By default, its value is true -->
  <!--type = table,once,copy-->
  <!--cond = not using yet-->
 </GeneratedFiles>
</Repository>

Running eXG

First, we need to copy the generated definition files into the folder of defined templates so we can configure them in "Repository.xml". Once the application gets started, select the location of templates and output dirctory where ASP.NET application wants to be created.

Image 4

Now click load button for loading templates, last generated entities (definitions generated by eXDG) will be populated in a grid. Select required entities and click generate button for generating the final code.

Option, Overwrite

While generating code, by default, the system checks whether the generated file is already created or not. If file is already created, system prompts and asks the user to overwrite that file or leave it unchanged. If we check "overwrite" check box, the system overwrites all selected files without prompting the user.

Software Setup

I have uploaded setup of eXtremecode ASP.NET generator here. It contains two sub folders, one contains eXtremecode Definition Generator application and the other one contains eXtremecode Generator application with code of predefined templates. I have written steps to generate ASP.NET application in ReadMe.txt file, it must help you.

Image 5

Northwind Example

I have generated an ASP.NET application for Northwind database. Please download it from here. I have also enclosed the sample configuration of templates for Northwind database with the setup of software in eXG folder so if you will be interested to generate Northwind ASP.NET application yourself, you can use that configuration.

89261/untitled7.JPG

Upcoming Articles

That was all the introduction of eXtremecode ASP.NET application generator with basic details. I hope this introduction will be enough to generate a simple ASP.NET application. Still, there are a lot of things which need to be explained. Here is the summary of topics which will be explained in upcoming articles.

  • Generated Definitions/Schema - Explanation of all elements and their attributes.
  • Entity Level Customization - How to order fields of specific entity in ASPX pages. How to control visibility and nature of web controls.
  • Templates - How to modify templates. What conventions have to be followed while writing a template.
  • Templates Management and Controlling - How templates will be executed to generate code. Explanation of "Repository.xml" contents,
  • Security Implementation and Membership Provider - How to manage rights of user in ASP.NET application. Explanation of custom Membership provider. Explanation of custom attributes of "Web.sitemap" XML file. Explanation of controlling visibility of items in navigation controls (Menu, TreeView and SiteMapPath)
  • Embedding of developer's custom code into generated code - How to embed custom code in the way that code will be safe after regenerating of code.
  • Decorators or Application Appearance - How to use decorators. How to define new decorator for designing new layout and appearance of ASP.NET pages.

If you are interested to follow up these topics, please subscribe right now.Image 7Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)