Click here to Skip to main content
Click here to Skip to main content
Go to top

Creating Code for Coding

, 18 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Creating code to create code

Introduction

Have you ever felt like doing the same tasks or writing similar code repetitive times? For example, creating a new web application, creating a database, running aspnet_regsql.exe, creating roles, writing web.config, creating a new form for login, coding the login...
What about having it at once with one click at all?

Background

Some years ago, I was faced with an "easy request"; convert an Excel file into an application.
The deadline was one week, easy huh?
Opening the Excel file, it was nothing less than hundreds of sheets, that day I decided to create a virtual programmer, i.e., code to create code. In one week, the application was done.

How It Works?

Basically, the idea of the virtual programmer has the following features:

  • Read a template file, substitute tags, write an output file
  • Perform configuration operations

The current article shows up how to get in one click the following tasks/code:

  • Create a database
  • Run aspnet regsql to create membership tables
  • Create web.config
  • Create some roles and users
  • Create the Login form and code behind
  • Have a startup project with SQL Authentication

Note: Yes! There are many tools to perform the same task! But the idea here is the approach of creating code for coding helping to get rid of doing repetitive work and saving time. It means more time for coffee. Smile | :)

Creating a Database

Our first task is to create a database, we can do this by the following code:

...
SqlCommand command = connection.CreateCommand();
command.CommandText = "CREATE DATABASE " + dbname;
connection.Open();
command.ExecuteNonQuery();
connection.Close();		
...		 
// ! removed validations in the example above 

Executing aspnet.regsql

Now we should run aspnet_regsql manually once again. Instead, we will call SqlServices which will perform the same operation to create the aspnet tables in the DB.

...
SqlServices.Install(dbname, SqlFeatures.All, connectionstring);
... 

Creating the web.config

The next task should be edit manually the web.config... But why?
We will read a template file, i.e. web_config.txt, perform a tag replacement and then save the output web.config file.

sContents = TextFileHelper.ReadTextFile("web_config.txt"); 
sContents = sContents.Replace("@CONNECTIONSTRING@", connectionstring);
sContents = sContents.Replace("@APPLICATIONNAME@", applicationname); 
...
TextFileHelper.WriteTextFile("web.config", sContents);  
... 

Adding Roles and Users

To add roles and users to the application, we do have again template files. They are configure_aspx.txt and configure_aspx_cs.txt.
The concept is the same: Read the template file, substitute the tags and create the output files configure.aspx and configure.aspx.cs.

sContents = TextFileHelper.ReadTextFile("configure_aspx_cs.txt"); 
sContents = sContents.Replace("@ROLE1@", role1);
sContents = sContents.Replace("@ROLE2@", role2); 
...
sContents = sContents.Replace("@USER1@", user1);
sContents = sContents.Replace("@USER2@", user2); 
... 
sContents = sContents.Replace("@USER1ROLE@", user1role);
sContents = sContents.Replace("@USER2ROLE@", user2role); 
... 
TextFileHelper.WriteTextFile("configure.aspx.cs", sContents);  
...

The code on configure.aspx.cs:

UserHelper.AddRole("@ROLE1@");
UserHelper.AddRole("@ROLE2@");
...
UserHelper.CreateUser("@USER1@", "@PWD1@", "@ROLE1@");
UserHelper.CreateUser("@USER2@", "@PWD2@", "@ROLE2@"); 
... 

After running the code above, it just calls the configure page configure.aspx through the browser and voila, you do have the skeleton of an ASP.NET application with SQL Authentication.

Running the Application

step1.jpg

Fill up the application name and your connection string.

Click Proceed and voila, you do have a database and a functional website.

step2.jpg

The website files are generated one folder under your application EXE / Output / chosen application name.

Now in VS, go to File / Open Website and open your new application.

step3.jpg

Set your virtual path to /.

step4.jpg

And run the configure.aspx. It will create the roles and an admin user.

step5.jpg

Log in using the credentials admin adminpwd.

And here you are at default.aspx.

step6.jpg

Points of Interest

This is only a sample where you can code to create your code. Think about the tasks you repetitively perform and create your own virtual programmer.

Cheers!

History

A subset of the code is being prepared to be placed online.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

andalmeida
Engineer IBM
Brazil Brazil
Senior Analyst
 
Founder of TIHunter Vagas de TI
 
Linkedin Profile
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberWestieBoy24-Feb-13 17:11 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 21:47 
nice
Questionyour idea is good, but do you know "CodeSmith"? PinmemberZeChic19-Sep-11 17:56 
GeneralRe: your idea is good, but do you know "CodeSmith"? Pinmemberandalmeida19-Sep-11 21:31 
AnswerRe: your idea is good, but do you know "CodeSmith"? PinmemberP0110X27-Sep-11 4:36 
GeneralMy vote of 4 PinmemberAbe Ek14-Sep-11 7:45 
GeneralMy vote of 5 Pinmemberfredatcodeproject13-Sep-11 22:32 
GeneralMy vote of 5 PinmemberMilena Almeida12-Sep-11 23:18 
GeneralRe: My vote of 5 PinmemberMarcus Kramer13-Sep-11 8:42 
GeneralRe: My vote of 5 Pinmemberandalmeida13-Sep-11 9:36 
GeneralRe: My vote of 5 PinmemberMarcus Kramer15-Sep-11 5:15 
GeneralRe: My vote of 5 Pinmemberandalmeida15-Sep-11 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Sep 2011
Article Copyright 2011 by andalmeida
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid