> For the complete documentation index, see [llms.txt](https://aeudoc.gitbook.io/developer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aeudoc.gitbook.io/developer/backend/unit-of-work.md).

# Unit Of Work

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.&#x20;

The registration is done by the following steps:

1. Create an Entity file (see [Data Entity](/developer/backend/data-entity.md) for details)
2. Register the repository in "DataContext.cs" (see [Data Entity](/developer/backend/data-entity.md) 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.&#x20;

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.&#x20;

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:&#x20;

```
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: &#x20;

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aeudoc.gitbook.io/developer/backend/unit-of-work.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
