Friday, August 15, 2008

Generating the Code

Essential Information
The following information is necessary to generate the rest of the code:
  • database identifier
  • class/table name
  • fields - name, type & purpose
Conversion Script
In order to generate the conversion script, the previous version of this information will also be necessary.  If multiple previous version are currently in use, a conversion script can be generated for each of the previous versions in use.

Database Identifier
The database identifier tells which which connection string to pull from the configuration file.

  connectionString = ConfigurationManager
    .ConnectionStrings[
      ConfigurationManager
        .AppSettings[databaseIdentifier]
    ].ConnectionString;

Class/Table Name
The class/table name tells the name to use for the class in the CS file as well as the table in the database.

Fields
The fields identify the fields of the class and the columns of the table.  The order of these fields determines the order of the columns.

Field Name
The field name is the name of the field and the column.

Field Type
The field type is one of the following: 
  • primitive type - The primitive type is a C# type.  In the case of a string, a length for the database is also included.  
  • enumeration - The names of the enumerated values are also included.
  • other class - The other class needs to exist in order to include the key(s) to that class's table.
Field Purpose
The field purpose is one of the following:
  • passed-in key - the value for the key will be passed into the object constructor
  • code-generated key - the value for the key will be generated in the object constructor using the code provided
  • database-generated key - the value for the key will be generated in the database (this is for auto-incrementing keys)
  • required non-key - the value will be passed into the object constructor - a null value is not allowed
  • optional non-key - the value can be set via a property and will remain null if not provided
Generated Code
The tool can use this information to generate the constructor, private data members, properties and the essential methods (Read, Write, Delete, Exists).  This code will go into a file using the class/table name following this pattern: {classname}.generated.cs. Additional Developer code can go in the corresponding {classname}.supplemental.cs file.  This will allow the {classname}.cs file to be used just for the information essential for generating the rest of the class.  (The source file can, of course, be an XML file or any other file as long as it contains the essential information and the tool can process it.)

Generated SQL Scripts
The tool also generates the .SQL file which contains the database creation script including all of the stored procedures.  This file is called {classname}.sql and can then be included in a master script file for generating the database.

Previous Versions
The tool needs to have the ability to create conversion scripts from previous versions.

Where is the version information recorded?  Does it need to be recorded?

There are several ways to handle the version problem.  An existing database could be compared.  Another input file could be compared.  The generated CS and/or SQL file could be compared.  The input file could have multiple sections with one section for each of the various versions.  I will deal with the version problem at another time.

No comments: