Documentation
SolutionDeveloperComplianceProjects
  • Getting Started
  • Technology
    • Application Framework
    • Object-Relational Mapping
    • Security Framework
    • Architecture Pattern
    • API Management
  • Environment
    • Development Tool
    • Database Server
    • Web Server
  • PRESENTATION
    • Design Material
    • HTML Helpers
    • Icon Images
    • Custom CSS
    • JQuery Plugin
      • DataTables
      • Tabs
      • Date Picker
      • Calendar
      • Chart
      • Treeview
    • Client Script
      • Open Modal
      • Close Modal
      • Handle return data
      • Load Content
      • Change Hash
      • Change Title
      • Get actual Hash
      • Get Content
      • Update Content
      • Delete Content
      • Get Partial Content
      • Get Partial Content Async
      • Update Partial Content
      • Update Partial Content Async
      • Delete Partial Content
      • Get Action
      • Update Action
      • Delete Action
      • Cascading Dropdown
      • Display Document
    • MVC View
  • BACKEND
    • Base Controller
    • Controller
    • Base Model
    • View Model
    • Class Utility
      • Constant Values
      • Global Properties
      • Option Helper
      • Setting Helper
    • Data Repository
      • Read Columns
      • Read All Columns
      • Read First Record
      • Read by Primary Key
      • Find Record
      • Insert Record
      • Update Record
      • Delete Entity
      • Delete by Criteria
      • Delete by Primary Key
      • Counting Record
      • Check Exist
      • Generate Row-Id
    • Unit Of Work
    • Data Entity
  • Application Features
    • Account Management
    • Navigation Editor
    • Access Control
  • Examples
    • Simple Page
    • Master Detail
    • Editor Template
Powered by GitBook
On this page
  • IUnitWork.cs
  • UnitWork.cs
  • Data Repository pointing to common Data Table
  • Data Repository pointing to System-related Data Table
  • Data Repository pointing to API Endpoint
  1. BACKEND

Unit Of Work

PreviousGenerate Row-IdNextData Entity

Last updated 2 years ago

Unit of Work is the place where all Data Repositories are collectively stored. It combines all data sources, so that transactions are made to only a single object of database. So every time a Data Repository is created, it needs to be registered inside of Unit of Work.

The registration is done by the following steps:

  1. Create an Entity file (see for details)

  2. Register the repository in "DataContext.cs" (see for details)

  3. Register the repository in "IUnitWork.cs"

  4. Register the repository in "IUnitWork.cs"

"IUnitWork.cs" and "UnitWork.cs" are found in the "Access" folder under the "Data" directory of an application. In the following, the registration process in each of these files will be described:

IUnitWork.cs

"IUnitWork.cs" is the Interface that establishes the Structure of the Data Repositories without any Implementations.

The class mainly consists of Properties that represent the Data Context and the various Data Repositories. Other than that, it includes the declaration of a Save method.

To register a data repository in "IUnitWork.cs", you only have to declare it as a Property the following way:

IRepository<NameOfEntity> NameOfEntities { get; }

Notice the convention that the Property Name ("NameOfEntities") is the plural of the Data Type ("NameOfEntity"), which is the Entity class corresponding to the Data Repository.

Additionally, within each region they are categorized in groups that are visibly separated by more empty lines, so the newly added repository should be placed in the group it belongs to.

If a Data Repository is pointing to an API endpoint instead of a Data Table, it is placed above the Data Context and is declared like this:

IRequestClient ClientName { get; }

UnitWork.cs

"UnitWork.cs" is the Class that contains the Implementations of the Data Repositories.

The class contains a Data Context Property as well as a Constructor and methods to Save and Dispose. Below the Data Context Property, all the Data Repositories are listed as private Properties and below that List, the methods returning each Repository Property are located.

In the following, the declaration and implementation of different types of Data Repository are illustrated using a specific example:

Data Repository pointing to common Data Table

The Repository Properties are declared like this:

private IRepository<ProductionOrder> pOrderRepo;

The method returning them are implemented like this:

public IRepository<ProductionOrder> ProductionOrders
{
    get
    {
        if (pOrderRepo == null)
            pOrderRepo = new Repository<ProductionOrder>(DataContext);

        return pOrderRepo;
    }
}

Data Repository pointing to System-related Data Table

For System-related Data Repositories, their declaration look like this:

private IRepository<AspNetUser> aspNetUser;

The implementation of the corresponding method is located within the region "SYSTEM" and is done like this:

public IRepository<AspNetUser> AspNetUsers
{
    get => aspNetUser ?? (aspNetUser = new Repository<AspNetUser>(DataContext));
}

Data Repository pointing to API Endpoint

When the repository points to an API Endpoint, the Repository Property is declared like this:

private IRequestClient ctosClient;

Their corresponding method is located within the "API-CLIENT" region and is done like this:

public IRequestClient CtosClient { get => ctosClient ?? (ctosClient = new RequestClient(new WebAccount("CtosWebApi"))); }
Data Entity
Data Entity