Generated Middle Layer
Listed below are quick descriptions of the Middle Layer class files which includes Business Object
class files AspxFormsGen 4.5 generates that makes up the Middle Tier. In a 3-tier infrastructure,
middle layer code is called by the UI or presentation layer code. It can contain business computations
applicable to the application's purpose. It also contains calls to the data layer code.
One of each of the following objects listed below is generated per table in your database.
Generated Class (.cs/.vb) |
Quick Description (App_Code Folder) |
1. Business Object Base Class
|
A class file containing business methods. The methods encapsulates calls to the respective data layer methods. Used as a base class. |
2. Business Object Class
|
A class file derived from the business object base class. Additional methods may be added here. |
Accessing Code From a Client
AspxFormsGen 4.5 has two code generating engines; AspxFormsGen which generates the Front End (UI, Web Forms), and AspxCodeGen which generates the
Middle and Data Tier and Stored Procedures. Code generated by the AspxCodeGen 4.5 engine can be used by other clients/applications, all you really
have to do is remove them (Middle and Data Tier) from the generated web site and move to another project such as a Class Library project,
Web API, WCF, Web Service, etc. The Middle Tier and Data Layer code are located under the web site's App_Code folder, the Stored Procedures
are already in MS SQL (so you don't need to move them).
The examples below shows how to call the generated Middle Tier code from a client.
Note: We made it even easier by generating all the code examples for each of the method that you can access. More operations can be called as shown
here, and these
operations are generated for each of your database table.
Because everything else is generated for you, all you have to write is the following code. We made it even easier, we also generate examples for each operation,
so all you need to do is copy and paste code like the ones shown below. So if everything here is already generated why would I need to write any of these code? There could be
many scenarios that you may need to access already generated operations. For example, you may need to access these operations when you add new pages or even new classes
in your application. Another scenario could be that you have an Error table where you log errors in the application, you can add a logic in the Global.asax that saves the error
to your table, a few lines of code and you're done. You can also bundle operations in a button click, for example you can add a new item in one table and then update another
table in one click. There could be a gazillion scenarios why you would need to write these code.
The code examples below are based on a "Categories" table found in the Northwind database. These examples are copied from a class
called CategoriesExample (.cs or .vb) generated by AspxFormsGen 4.5. This is how simple it is to call the middle tier objects from your
client (web form, win form, web service, web api etc.). Note: More operations are generated than what's being shown below.
Select Everything
For example, you can assign allCategories to a Grid control or use it in a foreach loop.
C#
var allCategories = Categories.SelectAll();
VB.NET
Dim allCategories = Categories.SelectAll()
Sort Ascending By Property
For example, add this line of code to the Select Everything code shown above.
C#
allCategories.Sort(Categories.ByCategoryName);
VB.NET
allCategories.Sort(Categories.ByCategoryName)
Sort Descending By Property
For example, add these lines of code to the Select Everything code shown above.
C#
allCategories.Sort(Categories.ByDescription);
allCategories.Reverse();
VB.NET
allCategories.Sort(Categories.ByDescription)
allCategories.Reverse()
Select/Skip/Take
For example, sort Categories table by CategoryID in ascending order, Skip the first 20 records, and then
Select 10 records. Also returns the total number of records in the table so a control such as the JQGrid can compensate for paging. You can assign
objCategoriesCol to a Grid control or use it in a foreach loop.
C#
int rows = 10;
int startRowIndex = 20;
int totalRecordCount;
string sortBy = "CategoryID";
CategoriesCollection objCategoriesCol =
Categories.SelectSkipAndTake(rows, startRowIndex, out totalRecordCount, sortBy);
VB.NET
Dim rows As Integer = 10
Dim startRowIndex As Integer = 20
Dim totalRecordCount As Integer
Dim sortBy As String = "CategoryID"
Dim objCategoriesCol As CategoriesCollection = _
Categories.SelectSkipAndTake(rows, startRowIndex, totalRecordCount, sortBy)
Select/Skip/Take (Search)
For example, find records where CategoryID = 1 AND CategoryName Contains "be", and then Sort Categories table by CategoryID in descending order,
don't Skip any records, and then Select 10 records.
C#
int? catID = 1;
string catName = "be";
int rows = 10;
int startRowIndex = 0;
string sortBy = "CategoryID desc";
CategoriesCollection objCategoriesCol =
Categories.SelectSkipAndTakeDynamicWhere(catID, catName, rows, startRowIndex, sortBy);
VB.NET
Dim catID As Integer? = 1
Dim catName As String = "be"
Dim rows As Integer = 10
Dim startRowIndex As Integer = 0
Dim sortBy As String = "CategoryID desc"
Dim objCategoriesCol As CategoriesCollection = _
Categories.SelectSkipAndTakeDynamicWhere(catID, catName, rows, startRowIndex, sortBy)
Select a Record By Primary Key
For example, One (1) here is the primary key.
C#
var cat = Categories.SelectByPrimaryKey(1);
VB.NET
Dim cat = Categories.SelectByPrimaryKey(1)
Delete a Record By Primary Key
For example, One (1) here is the primary key.
C#
Categories.Delete(1);
VB.NET
Categories.Delete(1)
Insert a New Record
For example, upon insertion you can retrieve the inserted primary key*.
C#
Categories objCategories = new Categories();
objCategories.CategoryName = "Shoes";
objCategories.Description = "Something you wear on your foot";
int newlyCreatedPrimaryKey = objCategories.Insert();
VB.NET
Dim objCategories As New Categories()
objCategories.CategoryName = "Shoes"
objCategories.Description = "Something you wear on your foot"
Dim newlyCreatedPrimaryKey As Integer = objCategories.Insert()
Update an Existing Record By Primary Key
For example, One (1) here is the primary key.
C#
Categories objCategories = new Categories();
objCategories.CategoryID = 1;
objCategories.CategoryName = "Shoes";
objCategories.Description = "Something you wear on your foot";
objCategories.Update();
VB.NET
Dim objCategories As New Categories()
objCategories.CategoryID = 1
objCategories.CategoryName = "Shoes"
objCategories.Description = "Something you wear on your foot"
objCategories.Update()