Generated Middle Layer
Listed below are quick descriptions of the Middle Layer class files (generated in the Class Library Project) which includes Business Object
class files AspxFormsGen MVC 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 (Infrastructure 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
Although AspxFormsGen MVC 5 can generate Web API code both for public or private consumption, the the middle layer code is generated in a Class Project
so you can share it with other applications. Here are some code examples on how a windows-based client such as an ASP.NET web app, MVC web app, web forms, win forms,
web service (.asmx, wcf), etc can access the API (middle tier). 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.
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()