|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionLayergen is a program I wrote that will automatically create data access layers and business layers based off a SQL Server database or a Microsoft Access Database. Layergen will generate code in either C# or VB.NET, and is compatible with ASP.NET 1.1, ASP.NET 2.0, and ASP.NET 3.0 and up, in addition to Windows Forms applications and WFC apps. It also supports advanced stuff like data encryption and sorting. The Goals of LayerGenWhen I set out to design LayerGen, I did it with the following goals in mind:
Using the CodeUsing Layergen is pretty straightforward. When you run the program, you specify the SQL Server, the username, the password, the database, the target language, and a destination directory where the output files will be generated. If you click the Advanced Options button, then the following dialog appears:
From this dialog, you can enable sorting (strongly recommended) which will allow you to sort the record collections on any field in the table. You can also tell Layergen to automatically insert stored procedures into the table (experimental). If you do not enable this, then a Procedures.SQL file will be generated that will contain the needed Stored Procedures. Note that if you are using a Microsoft Access database, then the Stored Procedures will automatically be inserted whether you check this box or not. You can also enable data encryption. This will seamlessly encrypt all text data in the table. After you pick your options, hit the Create Layers button and the table/view selector will pop up:
Now, you can put a checkmark next to each table or view that you want Layergen to create layers for. Note that if you select a table that has a foreign key to another table, then you must also generate layers for that table. Pushing the Select Dependents button will automatically select those tables for you. Once you hit OK, the layers will be generated and will reside in the directory you specified earlier. It's that simple! Using the Generated CodeTo use the code, the first thing you need to do is insert the Stored Procedures into the database (unless you had Layergen automatically do this). You can copy the procedures out of the Procedures.SQL file and paste them into SQL Server. If your database is a Microsoft Access database, then you can skip this step because Layergen will automatically insert the procedures. Once your procedures are in place, the next step is to include the generated files into your project. Each table has two files that are generated (one for the business layer and one for the data layer). In addition to these files, there is also a Universal file and an Interface file. The Universal file is basically your connection string to the SQL Server or Microsoft Access database. If you are using Microsoft Access, then you may want this connection string to be dynamic, based on where the user installed your application. There are comments in this file that show you how to put the connection string inside your Web.Config file, if you are working on an ASP.NET application. Once these files have been included in your project, your project should compile with no errors. At this point, you are ready to use the code. Let's go through some examples. For these examples, let's assume you have two tables in your database, one called To insert a new record into the database: Dim State As New BusinessLayer.State
State.Statename = "California"
State.Save()
That's it! Pretty simple and straightforward, huh? To retrieve a record from the database: Dim State As New BusinessLayer.State(1) ' Retrieve record from State table
MessageBox.Show("The State is: " + State.StateName)
This would retrieve a record whose primary key is To retrieve more than one record at once and bind them to a Dim States As New BusinessLayer.States()
States.GetAll()
States.Sort(FIRSTNAME, ASCENDING) ' Only works if you enabled sorting
dgStates.DataSource=States
dgStates.DataBind()
Notice how when we are retrieving multiple records, we use Here is an example of loading a record and accessing the foreign key: Dim Address As New BusinessLayer.Address(2)
MessageBox.Show(Address.FirstName + " " + Address.LastName + _
" Lives in "+Address.FState.StateName)
All foreign keys can be accessed though You can create your own custom queries in Layergen too. For example, suppose we want to retrieve only records where I've included a sample application which is a very basic application that will show you how to insert, update, delete, and select records. Layergen, as you can see, has a lot of power. If you need any more help or examples, I'm glad to help! Known Bugs/LimitationsThere are a few limitations to Layergen:
The Future of LayerGenHere are some future enhancements/goals for Layergen:
History
|
||||||||||||||||||||||