Tutorial 1: Generating Code From a Database Schema With Monstarillo

Introduction:

The purpose of this tutorial is to demonstrate the use of Monstarillo in a real world scenario. We will be generating the data and business tiers against the database that is used in the Yellow Bridge Software Inc. Bug Killer sample application from a database schema. As you will see, most of the code will be generated for you by Monstarillo and will take only a fraction of the time it would take to write yourself.

This tutorial should take about 30 minutes to complete.

There is an online  support forum dedicated to this case study.

 Database setup:

Create a SQL server database called YBBugs. Run the YBBugsDatabase.sql script in query analyzer to set up the tables, relationship and some test data. A database schema can be found below.

Create a C# windows project and take note of its location.

Run Monstarillo and enter the database connection information.

Click the Generate Code Using DB Schema.  Monstarillo will read the database schema and the following screen will appear.  This screen will allow you to select which tables you would like to generate code for.

Click the Select All button to select all of the tables.  This will bring up the Edit Column Properties window.  On this window you can uncheck the Is Updatable checkbox for a given column to have the column excluded from insert and update stored procedures.  You also have the ability to change the label text for a given column when using Monstarillo to generate GUI code. 

 

Click the next button to bring up the Monstarillo code options window. 

Modify the Output Path so that it points to the folder that contains your new C# project.  Change the Stored Procedure Prefix to yb_.  The stored procedure prefix will be used to name the stored procedures that Monstarillo creates.  The Base Namespace is the namespace that all of the Monstarillo code will be created in.  Press OK  This will bring up the template selection window.  From here you will be able to select a set of templates to run.

The Modify Table Columns will allow you to select columns that you do not want to be included in insert and update stored procedures.  This could be useful in tables that have columns that are updated by triggers like a last updated date column.  Select the first set of templates which will include a stored procedures, PersistBase, Persist, BusinessBase, Business and Info class for every table for which you have selected to generate code.  Press the Generate Code button to generate your code.  Selecting the Show Generated Files checkbox will load Windows Explorer and show you the files that have been created for you.

Load your new C# application in Visual Studio.  Open the solution explorer and press the show all files button.  Select the Business, BusinessBase, Info, Persist, and PersistBase folders right click and select include in project.

Add an Application Configuration file to the project.

Next we will add an appSettings section to the app.config and add a ConnectionString key to it.  Modify the app.config file so that is looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="data source=(local);initial catalog=ybbugs ;integrated security=SSPI;persist security info=False;packet size=4096" />
</appSettings></configuration>

Add a datagrid to the Form1 form. 

Add a button to the Form1 form.  Double click on the button to add code to the button's click event.  Add the following code to the button's click event handler.

YellowbridgeSoftwareInc.Business.BugUser bus = new YellowbridgeSoftwareInc.Business.BugUser( System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString() );

dataGrid1.DataSource = bus.SelectAll();

In the code you are creating an instance of the business object that was created from the BugUser table in the database.  You are passing a connection string to the business object's constructor.  Finally you set the data grid's data source to the return value of the business object's SelectAll() method.  The Select all method returns all of the rows for a given table.  The SelectAll method returns a BugUserInfoCollection.  A BugUserInfoCollection is a collection of  BugUserInfo objects.  The BugUserInfo class is a lightweight custom class the Monstarillo generated for you and can be found in the Info folder.  Also notice that the business object also has SelectAll methods that return a DataTable or a DataReader.

Notice that the Perist, and Business classes for each generated table are not overwritten every time that Monstarillo is run.  The Persist class is inherited from the PersistBase class.  The Business class is inherited from the BusinessBase class.  The Persist and Business classes are excellent places for you to add your custom code.  Here you can override the methods of the PersistBase and BusinessBase classes.

Run the application, press the button and notice that the grid is filled with data from the BugUser table.

Conclusion:

We have seen how easy it is to create a data layer and business layer using Monstarillo.  We have seen how simple it is to use the code generated by Monstarillo. 

For more information on Monstarillo visit http://www.yellow-bridge.com .

Discuss Monstarillo with others at http://forums.yellow-bridge.com .