Generated Data Layer

Data Layer


Listed below are quick descriptions of the Data Layer class files (generated in the Class Library Project) AspCoreGen 2.0 Razor generates that makes up the Data Tier. In a 3-tier infrastructure, data layer code is called by the middle layer code. It contains calls to the database (stored procedures/Ad-Hoc SQL/linq-to-entities using Entity Framework Core).

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

Generated Class (.cs) Quick Description (DataLayer Folder)
1. Data Layer Base Class

  - Sample Code

A class file containing methods communicating with the database. The methods encapsulates calls to stored procedures using linq-to-entities queries. Used as a base class.
2. Data Layer Class

  - Sample Code

A class file derived from the data layer base class. Additional methods may be added here.
Generated Entities (.cs) Quick Description (EF Folder)
1. Entity Framework Entities

  - Sample Code

A class file containing entity properties.
Generated Ad-Hoc SQL (.cs) Quick Description (SQL Folder)
1. Ad-Hoc SQL Scripts

  - Sample Code

A class file containing SQL Scripts.


Note: When generating code you can choose whether to generate Linq-to-Entities queries (EF Core), or Stored Procedures, or Ad-Hoc SQL. Stored procedures are generated directly in your Microsoft SQL Server. Linq-to-Entities queries (EF Core) or Ad-Hoc SQL are generated in this Class Library Project.


Linq-to-Entities (Entity Framework - EF Core)
One of the main changes in AspCoreGen is the use of linq-to-entities queries to access the database. Unlike our previous products, AspCoreGen can now use the entity framework to access the database either through linq-to-entities queries. The beauty here is that the Middle Tier has not changed the way it accesses the Data Layer, it's still the same Object-dot notation as shown below (see accessing code from the middle tier).


Instead of (previous software):

public static Products SelectByPrimaryKey(int productID)
{
    Products objProducts = null;
    string dynamicSqlScript = ProductsSql.SelectByPrimaryKey();
 
    using (SqlConnection connection = 
new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"])) { connection.Open(); using (SqlCommand command = 
new SqlCommand(dynamicSqlScript, connection)) { command.CommandType = CommandType.Text; // parameters command.Parameters.AddWithValue("@productID", productID); using (SqlDataAdapter da = new SqlDataAdapter(command)) {     DataTable dt = new DataTable();     da.Fill(dt);     if (dt != null)     {         if (dt.Rows.Count > 0)         {             objProducts = CreateProductsFromDataRowShared(dt.Rows[0]);         }     } } } } return objProducts; }


Now, It also Generates Linq-to-Entities (Entity Framework - EF Core):

internal static Products SelectByPrimaryKey(int productID)
{
    NorthwindContext context = new NorthwindContext();
    return context.Products.Where(p => p.ProductID == productID).FirstOrDefault();
}



Accessing Code From the Middle Tier
To make it really simple, the Data Layer method signatures are very similar to the Middle Layer method signatures, we simply add a string constant "DataLayer" to the middle layer method name to call data layer objects. For example if the middle tier object name is Categories, then the respective data tier object name is CategoriesDataLayer. To call a data layer method you simply add a dot and then the method name; e.g. CategoriesLayer.TheMethodName();


Select Everything:

// we generally don't assign to a variable
var allCategories = CategoriesLayer.SelectAll();

// instead, we simply return the collection to the calling middle tier method
return CategoriesLayer.SelectAll();



Accessing The Database From The Data Layer
As mentioned above, the database can now be accessed using either of the following:

- Using Linq-To-Entities (Entity Framework Core)
- Using Classic ADO.NET:

    - Using Stored Procedures
    - Using Ad-Hoc SQL
* Please see notes.