Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

Creating Code for Coding

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
18 Sep 2011GPL33 min read 32.7K   559   33   12
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. :-)

Creating a Database

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

C#
...
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.

C#
...
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.

C#
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.

C#
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:

C#
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

code4coding1/step1.jpg

Fill up the application name and your connection string.

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

code4coding1/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.

code4coding1/step3.jpg

Set your virtual path to /.

code4coding1/step4.jpg

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

code4coding1/step5.jpg

Log in using the credentials admin adminpwd.

And here you are at default.aspx.

code4coding1/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)


Written By
CEO
Brazil Brazil
"A well written code is self explanatory" - Anonymous Programmer
Founder @TIHUNTER.COM.BR
Linkedin Profile

Comments and Discussions

 
GeneralMy vote of 5 Pin
WestieBoy24-Feb-13 17:11
WestieBoy24-Feb-13 17:11 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:47
professionalManoj Kumar Choubey26-Feb-12 21:47 
nice
Questionyour idea is good, but do you know "CodeSmith"? Pin
ZeChic19-Sep-11 17:56
ZeChic19-Sep-11 17:56 
GeneralRe: your idea is good, but do you know "CodeSmith"? Pin
andalmeida19-Sep-11 21:31
andalmeida19-Sep-11 21:31 
AnswerRe: your idea is good, but do you know "CodeSmith"? Pin
P0110X27-Sep-11 4:36
professionalP0110X27-Sep-11 4:36 
GeneralMy vote of 4 Pin
Abe Ek14-Sep-11 7:45
Abe Ek14-Sep-11 7:45 
GeneralMy vote of 5 Pin
fredatcodeproject13-Sep-11 22:32
professionalfredatcodeproject13-Sep-11 22:32 
GeneralMy vote of 5 Pin
Milena Almeida12-Sep-11 23:18
Milena Almeida12-Sep-11 23:18 
GeneralRe: My vote of 5 Pin
fjdiewornncalwe13-Sep-11 8:42
professionalfjdiewornncalwe13-Sep-11 8:42 
GeneralRe: My vote of 5 Pin
andalmeida13-Sep-11 9:36
andalmeida13-Sep-11 9:36 
GeneralRe: My vote of 5 Pin
fjdiewornncalwe15-Sep-11 5:15
professionalfjdiewornncalwe15-Sep-11 5:15 
GeneralRe: My vote of 5 Pin
andalmeida15-Sep-11 23:33
andalmeida15-Sep-11 23:33 

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

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