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 AspCoreGen 3.0 Razor 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) Quick Description (BusinessObjects Folder)
1. Business Object Base Class

  - Sample Code

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

  - Sample Code

A class file derived from the business object base class. Your own custom (code) properties and/or methods may be added here.
Generated Models (.cs) Quick Description (Models Folder)
1. Model Base Class

  - Sample Code

A class file containing fields decorated with data annotations (validation, display, data type) properties used as a base class.
2. Model Class

  - Sample Code

A class file derived from the model base class. Your own custom (code) fields may be added here.


Accessing Code From a Client


Although AspCoreGen 3.0 Razor can generate Web API code both for public or private consumption, 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 Core web app, Razor 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 your own custom Global error catcher 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.

List<Categories> allCategories = await Categories.SelectAllAsync();

Sort Ascending By Property

For example, add this line of code to the Select Everything code shown above.

allCategories.Sort(Categories.ByCategoryName);

Sort Descending By Property

For example, add these lines of code to the Select Everything code shown above.

allCategories.Sort(Categories.ByDescription);
allCategories.Reverse();

Select/Skip/Take

For example, sort Categories table by CategoryID in descending order, Skip the first 20 records, and then Select 10 records. You can assign objCategoriesCol to a Grid control or use it in a foreach loop.

int? catId = 1;
string catName = "be";
int rows = 10;
int startRowIndex = 0;
string sortBy = "CategoryID desc";
        
List<Categories> objCategoriesCol = 
    await Categories.SelectSkipAndTakeDynamicWhereAsync(catId, catName, rows, startRowIndex , 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.

int? catId = 1;
string catName = "be";
int rows = 10;
int startRowIndex = 0;
string sortBy = "CategoryID desc";
 
List<Categories> objCategoriesCol =
    await Categories.SelectSkipAndTakeDynamicWhereAsync(catId, catName, rows, startRowIndex, sortBy);

Select/Skip/Take By Foreign Key

For example, the Products table have a CategoryId foreign key. Get Seven (7) Products/items (from the Products table) that has a CategoryId = 1 (Beverages).

int rows = 7; 
int? catId = 1 // Beverages;
int startRowIndex = 0;
string sortBy = "ProductName";
 
List<Products> objProductsCol =
    await Products.SelectSkipAndTakeByCategoryIDAsync(rows, startRowIndex, sortBy, catId);

Select a Record By Primary Key

For example, One (1) here is the primary key.

var cat = await Categories.SelectByPrimaryKeyAsync(1);

Get the Total Number of Records

int totalRecordCount = await Categories.GetRecordCountAsync();

Delete a Record By Primary Key

For example, One (1) here is the primary key.

await Categories.DeleteAsync(1);

Delete Multiple Records By Primary Keys

For example, 1, 7, and 20 here are primary keys.

string ids = "1, 7, 20";
 
// split ids into a List
List<Int32> catIdList = ids.Split(",").Select(Int32.Parse).ToList();
 
// delete multiple records based on a list of ids (primary keys)
await Categories.DeleteMultipleAsync(catIdList);

Insert a New Record

For example, upon insertion you can retrieve the inserted primary key*.

Categories objCategories = new Categories();
objCategories.CategoryName = "Beverages";
objCategories.Description = "Soft drinks, coffees, teas, beers, and ales";
 
int newlyCreatedPrimaryKey = await objCategories.InsertAsync();

Update an Existing Record By Primary Key

For example, One (1) here is the primary key.

Categories objCategories = new Categories();
 
// assign the existing primary key(s)
// of the record you want updated
objCategories.CategoryID = 1;
 
// assign values you want updated
objCategories.CategoryName = "Beverages";
objCategories.Description = "Soft drinks, coffees, teas, beers, and ales";
 
// finally, update an existing record
await objCategories.UpdateAsync();