Generated Business Layer


Listed below are quick descriptions of the Business Layer interface and class files (generated in the Class Library Project). In a 3-tier infrastructure, Business Layer (code) is called by the UI or presentation layer. It can contain business computations applicable to the application's purpose. It also contains calls to the data layer.

One of each of the following objects listed below is generated per table in your database.

Generated Business Layer Quick Description (Business Layer Folder)
1. Business Layer Partial Interface

  - Sample Code

A partial interface containing business methods. Used like a base interface.
2. Business Layer Partial Interface

  - Sample Code

A partial interface file. Your own custom (code) properties and/or methods may be added here.
3. Business Layer Partial Class

  - Sample Code

A partial class containing business methods. The methods encapsulates calls to the respective data layer methods. Used like a base class.
4. Business Layer Partial Class

  - Sample Code

A partial class file. Your own custom (code) properties and/or methods may be added here.
Generated Models (.cs) Quick Description (Models Folder)
1. Model Partial Class

  - Sample Code

A partial class containing fields decorated with data annotations (validation, display, data type) properties. Used like a base class.
2. Model Partial Class

  - Sample Code

A partial class. Your own custom (code) fields may be added here.
Generated View Models (.cs) Quick Description (ViewModels Folder)
1. View Model Partial Class

  - Sample Code

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

  - Sample Code

A partial class. Your own custom (code) properties may be added here.
3. Foreach View Model Partial Class

  - Sample Code

A partial class containing model properties used specifically by the List with Manual For Each Loop view, pushed from the controller. Used like a base class.
4. Foreach View Model Partial Class

  - Sample Code

A partial class used specifically by the List with Manual For Each Loop view. Your own custom (code) properties may be added here.


Accessing Code From a Client


Although AspCoreGen 6.0 MVC can generate Web API code both for public or private consumption, the business 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 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.

New Feature: Dependency Injection

Examples below are using the injected Business Layer class _categoryBusinessLayer or (_productBusinessLayer). Dependency injection improves the decoupling of objects. It provides a way to separate the creation of an object from its usage.

To use the Business Layer object, it is injected from MVC's start up application, the program.cs to the constructor of the class using it. For the purpose of these examples, the CategoryBusinessLayer can be injected to the Controller that's going to use it.

Again, all these code are already generated.
public partial class CategoryController : Controller {     private readonly ICategoryBusinessLayer _categoryBusinessLayer;

    // constructor
    public CategoryController(ICategoryBusinessLayer categoryBusinessLayer)
    {
        _categoryBusinessLayer = categoryBusinessLayer;
    }

    // most code removed for brevity
}

Select Everything

For example, you can assign objCategoriesList to a Grid control or use it in a foreach loop.

List<CategoryobjCategoriesList = await _categoryBusinessLayer.SelectAllAsync();

Select Everything, Sorted

Sort the collection in ascending or descending order.

// select all records sorted by CategoryID in ascending order string sortBy = "CategoryID"// ascending order //string sortBy = "CategoryID desc"; // descending order

List<CategoryobjCategoriesList =
     await _categoryBusinessLayer.SelectAllAsync(sortBy);

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 objCategoriesList to a Grid control or use it in a foreach loop.

int numberOfRecordsToRetrieve = 10;
int startRetrievalFromRecordIndex = 19;
string sortByDesc = "CategoryID desc";
//string sortByAsc = "CategoryID";

List<CategoryobjCategoriesList =      await _categoryBusinessLayer.SelectSkipAndTakeAsync(numberOfRecordsToRetrieve,                                              startRetrievalFromRecordIndex, sortByAsc);

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 skip = 0;
string sortBy = "CategoryID desc";
 
List<CategoryobjCategoriesList =
  await _categoryBusinessLayer.SelectSkipAndTakeDynamicWhereAsync(catId, catName, rows, skip, 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 skip = 0;
string sortBy = "ProductName";
 
List<ProductobjProductsList =
    await _productBusinessLayer.SelectSkipAndTakeByCategoryIDAsync(rows, skip, sortBy, catId);

Select a Record By Primary Key

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

Category cat = await _categoryBusinessLayer.SelectByPrimaryKeyAsync(1);

Get the Total Number of Records

int totalRecordCount = await _categoryBusinessLayer.GetRecordCountAsync();

Delete a Record By Primary Key

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

await _categoryBusinessLayer.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<Int32catIdList = ids.Split(",").Select(Int32.Parse).ToList();
 
// delete multiple records based on a list of ids (primary keys)
await _categoryBusinessLayer.DeleteMultipleAsync(catIdList);

Insert a New Record

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

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

Update an Existing Record By Primary Key

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

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