> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-commerce/17.latest/key-concepts/unit-of-work.md).

# Unit of Work

## Unit of Work

When working with Umbraco Commerce's API it is important that data integrity is maintained should any errors occur. In order to achieve this Umbraco Commerce uses the [Unit of Work pattern](https://www.martinfowler.com/eaaCatalog/unitOfWork.html) to effectively create a transaction that wraps around sections of your code ensuring that all Umbraco Commerce write operations that occur within that code block must succeed and be persisted in their entirety, otherwise, none of them should, and the database should rollback to its state prior to when those changes were made.

### Creating a Unit of Work

Creating a unit of work will require access to Umbraco Commerce's `IUnitOfWorkProvider` which can be [injected into your Controller directly](/umbraco-commerce/17.latest/key-concepts/dependency-injection.md), or can also be accessed via the `UoW` property on the `IUmbraco CommerceApi` helper.

Once you have access to either of these entry points, you can define a Unit of Work as follows

```csharp
await _uowProvider.ExecuteAsync(async (uow) =>
{
    // Perform your write operations here

    uow.Complete();
});

```

The anatomy of a Unit of Work includes an `ExecuteAsync` method call on the `IUnitOfWorkProvider` instance. This method accepts an async delegate function with a `uow` argument. Inside the delegate, perform tasks and confirm the Unit of Work as complete by calling `uow.Complete()`. If you forget to call `uow.Complete()` or encounter an exception, then any write operations within that code will **not** be persisted in the database.

### Unit of Work Best Practice

When using a Unit of Work it is best practice that you should perform **all** write operations inside a single Unit of Work and **not** create individual Units of Work per write operation.

{% hint style="info" %}
Perform all write operations in a single Unit of Work
{% endhint %}

```csharp
await _uowProvider.ExecuteAsync(async (uow) =>
{
    // Create a Country
    var country = await Country.CreateAsync(uow, storeId, "DK", "Denmark");

    await _countryService.SaveCountryAsync(country);

    // Create a Currency
    var currency = await Currency.CreateAsync(uow, storeId, "DKK", "Danish Kroner", "da-DK");

    await _currencyService.SaveCurrencyAsync(currency);

    uow.Complete();
});
```

{% hint style="info" %}
It is not recommended to create a Unit of Work per write operation.
{% endhint %}

```csharp
await _uowProvider.ExecuteAsync(async (uow) =>
{
    // Create a Country
    var country = await Country.CreateAsync(uow, storeId, "DK", "Denmark");

    await _countryService.SaveCountryAsync(country);

    uow.Complete();
});

await _uowProvider.ExecuteAsync(async (uow) =>
{
    // Create a Currency
    var currency = await Currency.CreateAsync(uow, storeId, "DKK", "Danish Kroner", "da-DK");

    await _currencyService.SaveCurrencyAsync(currency);

    uow.Complete();
});
```


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.umbraco.com/umbraco-commerce/17.latest/key-concepts/unit-of-work.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
