Generated Data Repository


Listed below are quick descriptions of the Data Repository interface and class files (generated in the Class Library Project) that makes up the Data Tier. In a 3-tier infrastructure, the Data Repository is called by the Business Layer. 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 (DataRepository Folder)
1. Data Repository Partial Interface

  - Sample Code

A partial interface containing methods that communicate with the database. Used like a base interface.
2. Data Repository Partial Interface

  - Sample Code

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

  - Sample Code

A partial class containing methods that communicate with the database. The methods encapsulates calls to stored procedures using linq-to-entities queries. Used like a base class.
4. Data Repository Partial Class

  - Sample Code

A partial class file. Your own custom (code) properties and/or 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.

Accessing Code From the Middle Tier


The generated Data Repository (data tier) can only be accessed by the Business Layer (middle tier). This is done by design so that we don't accidentally call any Data Repository method from the presentation layer, or any client that wants to access the database.

To make it really simple, the Data Repository method signatures are very similar to their Business Layer method counterpart.

New Feature: Dependency Injection

Examples below are using the injected Data Repository class _categoryDataRepository. Dependency injection improves the decoupling of objects. It provides a way to separate the creation of an object from its usage.

To use the Data Repository object, it is injected from MVC's start up application, the program.cs to the constructor of the class using it, the Business Layer class. The examples show the CategoryDataRepository is injected to the CategoryBusinessLayer that's going to use it.

Again, all these code are already generated.
public partial class CategoryBusinessLayer : ICategoryBusinessLayer {     private readonly ICategoryRepository _categoryRepository;

    // constructor
    public CategoryBusinessLayer(ICategoryRepository categoryRepository) =>
        _categoryRepository = categoryRepository;

    // most code removed for brevity
}

Select Everything

Shows the Ad-Hoc generated code.

DataTable dt = await _categoryRepository.SelectAllAsync();

Delete A Record By Primary Key

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

await _categoryRepository.DeleteAsync(1);

Accessing the Database from the Data Repository


As mentioned above, the database can be accessed using one of the following:

1. Using Linq-To-Entities (Entity Framework Core)
2. Using Stored Procedures
3. Using Ad-Hoc SQL