Code Author
Agile Methodology through Code Generation
Getting Started Guides.
These scenario based lessons will teach you how to build a real life application through the use of Code Author. Emphasis is placed not only in using the Code Author tool but more so in the process to create a best-practice, three-tiered solution.
Lesson 1 – The basics.
Length: 10 mins
Background:
You’re a freelance .NET software consultant. Your customer, Xin’s Pizza operates a growing pizza business. The business consists of three local stores. Until now, all records are kept on paper. Xin has approached you for help in implementing a customized software system to better manage his business. As a first step, he has asked you to develop a very basic CRM (Customer Relations Management) system to keep track of his regular customers.
Specification:
You meet up with Xin to discuss his business requirements. The business requirements are very simple. A list of customers and their contact details must be kept electronically. The only information collected about the customer is his/her name, address (in plain text) and a phone number. Xin would like to be able to access this information via a web browser from his home and in the three stores.
Development:
The development is carried out in three main stages.
1. Design the data model.
2. Architect the solution and generate the business objects.
3. Develop the web site.
Step 1 –Design the data model.
1. Start SQL Server 2005 Management Studio or Visual Studio Server Browser.
2. Create a new database called XinPizza.

3. Create a new table called tblCustomer.

4. Add the ID, name, address, and phone number columns. Set the ID column as an identity field and as a primary key.

Step 2 - Architect the solution and Generate the objects.
1. Start Visual Studio.
2. Because this is a small project, we shall architect all the code into a single web site project. Create a new Web Site called XinPizza. (For larger projects, we may create a blank solution first, and then add separate projects for the business layer & the web site.)

3. Once the solution is created, we shall start Code Author.

4. Select the Create new template option and click next. Type in a project name, select the database, and select the default output path as per screen below. The default output path should point to the root folder of the solution. Click next.

5. In the next screen we are able to select the tables we use for generation. We only select the tblCustomer table we created earlier. Click next.

6. In this step we can configure some more advanced settings. One of the things we will do here is to set a more appropriate output folder for the business objects. We change the default \BO to \App_Code under the BusinessObjectFolder property. Click Next.

7. Code Author will now generate our code.

8. Click the save button to save the template for later re-use.
9. Go back to visual studio and click refresh in the solution explorer. You will notice several new files and folders. These were generated by Code Author.

10. The includes folder contains several DLL references such as the Microsoft application blocks. These are used by our business objects to enhance database access. We must add references to these files by right clicking on our project and selecting Add References. Add all dll files as references in the includes folder.

11. Another file in the includes folder is the app.config file. This file contains a few lines used by our business objects. We should open this file, make any necessary configuration changes such as the connection string for our database, then paste the settings into our web.config file. (if there is no web.config file, create one first)

12. Don’t worry, we’re almost done. The next step is to look in the App_Code folder to ensure the generated objects are included in our project. By default, they should be for a web project, however if the files don’t appear, you should click the show all files button in the tool bar and then include the files.

13. Press ctrl+shift+b to ensure the solution builds.
14. Now the very last step. Did you notice that Deployment folder? Open it and open the StoredProcedures.sql file. Copy everything in this file and run it in SQL Management Studio under the XinPizza database. This will deploy all of our CRUD stored procedures.
That’s it! Now for the final Stage.
Step 3 – Develop the full solution.
Our full solution will consist of two pages; a search page allowing the user to list customers; and a details page allowing the user to add, and edit customer details.
Code Author objects also support binding to ObjectDataSource which allows the creation of “codeless” applications with lightning pace. However, in practice very few applications can live with the limited functionality inherit in this approach. Therefore in this solution we will be coding our solution the old fashion way.
1. Remove the Default.aspx and create two new pages: Search.aspx, and Detail.aspx. Set the Search.aspx page as the start page.
2. Add a textbox, two buttons, and a datagrid to the search page. Change the text of the controls appropriately. Also add a select command column to the datagrid.

3. In the Search page code behind, add the following code. Also don’t forget to add the event handlers OnSelectedIndexChanged="GridView1_SelectedIndexChanged" to the GridView control in the design page.
public partial class Search : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Customer customer = new Customer();
customer.Name = TextBox1.Text;
Customer[] customers = Customer.Search(customer);
GridView1.DataSource = customers;
GridView1.DataKeyNames = new string[] { "Id" };
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
Server.Transfer("Detail.aspx?ID=0");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Server.Transfer("Detail.aspx?ID=" + GridView1.SelectedValue);
}
}
4. Add three textboxes, and two buttons to the details page. Change the text of the controls appropriately.

5. Type the following code into the detail code behind page. Again don’t forget to add any event handling links to the design page such as OnClick="Button1_Click".
public partial class Detail : System.Web.UI.Page
{
int _id = 0;
protected void Page_Load(object sender, EventArgs e)
{
// Get ID
if (Request.QueryString["ID"] != null)
_id = Int32.Parse(Request.QueryString["ID"]);
if (!IsPostBack)
{
LoadUI();
}
}
private void LoadUI()
{
if (_id != 0)
{
Customer customer = Customer.Get(_id);
TextBox1.Text = customer.Name;
TextBox2.Text = customer.Address;
TextBox3.Text = customer.Phone;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Customer customer = new Customer();
customer.Id = _id;
customer.Name = TextBox1.Text;
customer.Address = TextBox2.Text;
customer.Phone = TextBox3.Text;
if (_id == 0)
customer.Insert();
else
customer.Update();
Response.Write("Saved.");
Server.Transfer("Search.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
Server.Transfer("Search.aspx");
}
}
6. That’s all!

