# 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](data-entity) for details)
2. Register the repository in "DataContext.cs" (see [Data Entity](data-entity) 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"))); }
```
