A a white Paper by Don Tebbet 627 Ridgeview Rd. Brightwood, Virginia 22715




Yüklə 33.74 Kb.
tarix27.02.2016
ölçüsü33.74 Kb.


A
A White Paper by Don Tebbet
White Paper by Don Tebbet


627 Ridgeview Rd.

Brightwood, Virginia 22715

(540)543-2109

http://www.tebbet.com

CSWinCodeGen - A C#.NET Windows Application Code Generator


Don Tebbet

Contents


Introduction 3

Problem Statement 3

Previous Options 3

A Solution 3

Implementation 5





Introduction


This paper describes a code generator, CSWinCodeGen, for C#.NET Windows Applications. (The source for CSWinCodeGen is available for download here.)

Problem Statement


Clients often wish to see, as soon as possible in the software development life cycle (SDLC), some evidence that their hard-spent consulting budget is returning some tangible benefit. In order to satisfy their demand to ‘see some screens’ considerable effort may be spent prematurely in developing foundation components of a running system which, as analysis and design progress, must be continuously changed to accommodate new discoveries in the requirements gathering process.

Previous Options


Devoting development resources to construction of incomplete design has been a typical method of keeping the customer happy with some visible evidence that progress is indeed being made. Unfortunately, this immediately front-end loads the development process with much tedious work which may become almost as immediately obsolete.
Alternatively, code generation tools based upon logical design may be employed to create foundation components of systems. In my experience, these tools lack the practical visibility of presenting actual data that is meaningful to the client.

A Solution


CSWinCodeGen is a code generation tool which builds class components for a system from an existing database schema. The benefits of the tool include…


  • Automated generation of control classes

  • Automated generation of the data access layer

  • Automated generation of CRUD forms

  • Automated generated of grid forms



Automated generation of control classes


Based upon the tables present in a given database, CSWinCodeGen produces .CS files for classes for each table to represent the control class for the object represented by the table.

Automated generation of data access layer (DAL)


Based upon the tables present in a given database, CSWinCodeGen produces .CS files for classes for each table to represent the entity or data access class for the object represented by the table.

Automated generation of CRUD forms


Based upon the tables present in a given database, CSWinCodeGen produces forms classes supporting the basic features of change, report, update and delete (CRUD) for each table.

Automated generation of grid forms


Based upon the tables present in a given database, CSWinCodeGen produces forms classes providing data grid views of each table.

Implementation




CSWinCodeGen Main Form



In the main form CSWinCodeGen above I have provided a name of the SQL Server where the database, ERP, resides. I have provided a namespace, ERPWin, to be used in code generation. Note that leaving the optional ‘Table’ textbox empty implies that code will be generated for all tables present in the ERP database. The checkboxes indicate which code components are to be generated. The ‘Target Folder’ textbox provides the path to which generated files will be written.


The following sections examine what happens when the ‘Generate’ button is clicked.

Generating app.config

Note that one of the members of formMain is the following StringBuilder to hold a database connection string.




///

/// Database connection string

///

public StringBuilder sbConnStr;




Here’s the event handler for the click of the ‘Generate’ button.



private void buttonOk_Click(object sender, System.EventArgs e)

{

this.textBoxEvents.Text = "";



GenCode();

textBoxEvents.Text += "\r\nDone\r\n";

}

In the GenCode() method note the call to BuildConnectionString().



public void GenCode()

{

this.Cursor = Cursors.WaitCursor;



alCurrentProperties.Clear();

CopyBaseFiles();


BuildConnectionString();

.

.



.

And BuildConnectionString() lookslike this.

This default build of the connection string assumes integrated authentication is sufficient. Of course, this can be modified here or in the resultant app.config.


The CodeGen() method (above), may subsequently call the following WriteAppConfig().

///

/// Create an app.config for the target code base with a key for the SQL connection string

///

///


private void WriteAppConfig()

{

FileStream fsAppConfig = System.IO.File.Create(textBoxTargetFolder.Text + "\\" + "app.config");



StreamWriter swAppConfig = new StreamWriter(fsAppConfig);
swAppConfig.WriteLine("");

swAppConfig.WriteLine("");

swAppConfig.WriteLine("\t");

swAppConfig.WriteLine("\t\t");

swAppConfig.WriteLine("\t");

swAppConfig.WriteLine("");

swAppConfig.Close();

fsAppConfig.Close();

}


The WriteAppConfig() code above creates an app.config file as follows.




Identifying Classes and Members to Generate in the CodeGen() Method
Continuing our examination of the CodeGen() method we see the creation of a database connection and a query to get table and column names.

We can now read the results of the query and load our own Array List of properties for classes, alProperties…

… and then use alProperties to drive the creation of .CS files using a little bit of classic control break processing on a new array list of properties containing only members of the ‘current’ class.



At each control break in the for each loop above we have an array list, alCurrentProperties, loaded with only members of a single class and call the GenCSFiles() method to process that array list to create the selected source files.


Generating the Control Classes


For each table in the database a control class is generated. The .CS file is named as the table prefixed with ‘ctrl’. Given a table in the database named ‘Location’ CSWinCodeGen will create a control class file named ‘ctrlLocation.cs’. Ideally, the generated control classes will be included in a solution project for an assembly representing the control objects through which other layers of the solution architecture will access the system objects.
The GenCtrlCSFile() method of CSWinCodeGen produces a control class for the Location table as follows.

Note the namespace inclusion of ERPWin.DAL which provides access to the generated data access layer.
Within the Private Properties region we see the following code corresponding to the columns in the Location table.

In the Public Properties region we see the public property methods corresponding to the private members.

Finally, we see the Read(), Write() and Delete() methods which work through the data access layer.






Generating the Data Access Layer


For each table in the database a data access layer (DAL) class is generated. The .CS file is named as the table prefixed with ‘dal’. Given a table in the database named ‘Location’ CSWinCodeGen will create a control class file named ‘dalLocation.cs’. Ideally, the generated control classes will be included in a solution project for an assembly representing the DAL through which other layers of the solution architecture will access the data in the database.
The GenDALCSFile() method of CSWinCodeGen produces a data access class for the Location table as follows.

Generating the CRUD Forms


For each table in the database a rudimentary Windows form class is generated. The .CS file is named as the table prefixed with ‘crud’. Given a table in the database named ‘Location’ CSWinCodeGen will create a control class file named ‘crudLocation.cs’. Ideally, the generated control classes will be included in a solution project for a user interface to the solution.
The GenCRUDForm() method of CSWinCodeGen produces a Windows form class for the Location table. The form design appears as follows.

Generating Grid Forms


For each table in the database a Windows grid form class is generated. The .CS file is named as the table prefixed with ‘gridForm’. Given a table in the database named ‘Location’ CSWinCodeGen will create a form class file named ‘gridFormLocation.cs’. Ideally, the generated control classes will be included in a solution project for a user interface to the solution.
The GenGridForm() method of CSWinCodeGen produces a Windows grid form class for the Location table. The form design appears as follows.

When executed within the ERPWin solution with code to present the form it appears as follows.



Including Generated Code in a Solution


In the solution shown below we see three projects in a solution called ‘ERPWin’ for which we have used CSWinCodeGen to generate classes.


  • ERPControl

  • ERPDAL

  • ERPWin

The ERPControl project is a class library composed of the control classes generated by CSWinCodeGen. The ERPDAL project is a class library composed of the data access layer classes generated by CSWinCodeGen. The ERPWin project is the Windows application project providing user interface to the solution including the CRUD and grid form classes generated by CSWinCodeGen.



The expanded solution view of the ERPControl project shows the inclusion of our generated control classes.

The expanded solution view of the ERPDAL project shows the inclusion of our generated DAL classes.


The expanded solution view below shows the solution view including the generated forms classes.




Summary

Using CSWinCodeGen we can quickly generate the basic code required for an n-tier solution for a given a database.




Date


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azrefs.org 2016
rəhbərliyinə müraciət

    Ana səhifə