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 2.0 MVC 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. Additional 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. Additional fields may be added here.
Generated View Models (.cs) Quick Description (ViewModels Folder)
1. View Model Base Class

  - Sample Code

A class file containing model properties used by the view, pushed from the controller. Used as a base class.
2. View Model Class

  - Sample Code

A class file derived from the view model base class. Additional properties may be added here.


Accessing Code From a Client


Although AspCoreGen 2.0 MVC 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 Core 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.

var allCategories = Categories.SelectAll();

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 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.

int rows = 10;
int startRowIndex = 20;
int totalRecordCount;
string sortBy = "CategoryID";

CategoriesCollection objCategoriesCol =
   Categories.SelectSkipAndTake(rows, startRowIndex, out 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.

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);

Select a Record By Primary Key

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

var cat = Categories.SelectByPrimaryKey(1);

Delete a Record By Primary Key

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

Categories.Delete(1);

Insert a New Record

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

Categories objCategories = new Categories();
objCategories.CategoryName = "Shoes";
objCategories.Description = "Something you wear on your foot";
int newlyCreatedPrimaryKey = objCategories.Insert();

Update an Existing Record By Primary Key

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

Categories objCategories = new Categories();
objCategories.CategoryID = 1;
objCategories.CategoryName = "Shoes";
objCategories.Description = "Something you wear on your foot";
objCategories.Update();