Umbraco Commerce
CMSCloudHeartcoreDXP
15.latest
15.latest
  • Umbraco Commerce Documentation
  • Release Notes
    • v15.1.0-Rc
    • v15.0.0-Rc
  • Commerce Products
    • Commerce Packages
    • Commerce Payment Providers
    • Commerce Shipping Providers
  • Getting Started
    • Requirements
    • Installation
    • Licensing
    • Configuration
    • User Interface
  • Upgrading
    • Upgrading Umbraco Commerce
    • Version Specific Upgrade Notes
    • Migrate from Vendr to Umbraco Commerce
      • Migrate Umbraco Commerce Checkout
      • Migrate custom Payment Providers
  • Tutorials
    • Build a Store in Umbraco using Umbraco Commerce
      • Installation
      • Creating a Store
        • Configuring your Store
      • Creating your first Product
      • Implementing a Shopping Cart
        • Using the Umbraco.Commerce.Cart Drop-in Shopping Cart
        • Creating a Custom Shopping Cart
      • Implementing a Checkout Flow
        • Using the Umbraco.Commerce.Checkout Drop-in Checkout Flow
        • Creating a Custom Checkout Flow
      • Configuring Store Access Permissions
  • How-To Guides
    • Overview
    • Configure SQLite support
    • Use an Alternative Database for Umbraco Commerce Tables
    • Customizing Templates
    • Configuring Cart Cleanup
    • Limit Order Line Quantity
    • Implementing Product Bundles
    • Implementing Member Based Pricing
    • Implementing Dynamically Priced Products
    • Implementing Personalized Products
    • Implementing a Currency Switcher
    • Building a Members Portal
    • Order Number Customization
    • Sending Payment Links to Customers
    • Create an Order via Code
  • Key Concepts
    • Get to know the main features
    • Base Currency
    • Calculators
    • Currency Exchange Rate Service Provider
    • Dependency Injection
    • Discount Rules / Rewards
    • Events
      • List of validation events
      • List of notification events
    • Fluent API
    • Order Calculation State
    • Payment Forms
    • Payment Providers
    • Pipelines
    • Price/Amount Adjustments
    • Price Freezing
    • Product Adapters
    • Product Bundles
    • Product Variants
      • Complex Variants
    • Properties
    • ReadOnly and Writable Entities
    • Sales Tax Providers
    • Search Specifications
    • Settings Objects
    • Shipping Package Factories
    • Shipping Providers
    • Shipping Range/Rate Providers
    • Tax Sources
    • UI Extensions
      • Analytics Widgets
      • Entity Quick Actions
      • Order Line Actions
      • Order Properties
      • Order Collection Properties
      • Order Line Properties
      • Store Menu Items
    • Umbraco Properties
    • Unit of Work
    • Umbraco Commerce Builder
    • Webhooks
  • Reference
    • Stores
    • Shipping
      • Fixed Rate Shipping
      • Dynamic Rate Shipping
      • Realtime Rate Shipping
    • Payments
      • Configure Refunds
      • Issue Refunds
    • Taxes
      • Fixed Tax Rates
      • Calculated Tax Rates
    • Storefront API
      • Endpoints
        • Order
        • Checkout
        • Product
        • Customer
        • Store
        • Currency
        • Country
        • Payment method
        • Shipping method
        • Content
    • Management API
    • Go behind the scenes
    • Telemetry
Powered by GitBook
On this page
  • Unit of Work
  • Creating a Unit of Work
  • Unit of Work Best Practice

Was this helpful?

Edit on GitHub
Export as PDF
  1. Key Concepts

Unit of Work

Transactional updates using the Unit of Work pattern in Umbraco Commerce.

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 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, 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

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.

Perform all write operations in a single Unit of Work

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();
});

It is not recommended to create a Unit of Work per write operation.

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();
});
PreviousUmbraco PropertiesNextUmbraco Commerce Builder

Last updated 7 months ago

Was this helpful?