Table of Contents

Class UnitOfWork

Namespace
Builvero.Infrastructure.Data
Assembly
Builvero.Infrastructure.dll

Implementation of the Unit of Work pattern for managing database transactions and entity tracking.

public class UnitOfWork : IUnitOfWork
Inheritance
UnitOfWork
Implements
Inherited Members

Remarks

This class provides a centralized way to manage database operations and ensure consistency across multiple repository operations. It wraps the Entity Framework Core DbContext and provides methods for saving changes and bulk entity removal.

The Unit of Work pattern ensures that all changes made within a single business transaction are committed atomically, maintaining data integrity.

Constructors

UnitOfWork(ApplicationDbContext)

Initializes a new instance of the UnitOfWork class.

public UnitOfWork(ApplicationDbContext context)

Parameters

context ApplicationDbContext

The database context to use for operations.

Methods

Add<T>(T)

Adds a new entity to the context and ensures it's marked as Added. This is useful when adding entities through navigation properties to ensure they're correctly tracked as new entities rather than Modified.

public void Add<T>(T entity) where T : class

Parameters

entity T

The entity to add.

Type Parameters

T

The entity type.

Remarks

This method ensures that the entity is tracked and marked as Added, even if it was previously tracked in a different state. This prevents EF Core from trying to UPDATE entities that should be INSERTed.

If the entity is already tracked in a state other than Added or Detached, it will be reset to Added state. This is safe for new entities that haven't been saved to the database yet.

EnsureEntityNotModified<T>(T)

Ensures that an entity is not marked as Modified if it hasn't been explicitly modified. This is useful to prevent concurrency issues when only related entities are modified.

public void EnsureEntityNotModified<T>(T entity) where T : class

Parameters

entity T

The entity to check and potentially reset to Unchanged state.

Type Parameters

T

The entity type.

Remarks

This method checks if the entity is tracked and marked as Modified, and if so, checks if any non-primary-key scalar properties have actually changed. If no scalar properties were modified, it resets the entity to Unchanged state. This prevents EF Core from generating UPDATE statements for entities that haven't actually been modified (e.g., when only navigation properties are modified).

This is particularly important for InMemory database tests where EF Core's change tracking can incorrectly mark entities as Modified when only navigation properties are modified.

RemoveRange<T>(IEnumerable<T>)

Marks a collection of entities for deletion.

public void RemoveRange<T>(IEnumerable<T> entities) where T : class

Parameters

entities IEnumerable<T>

The collection of entities to remove.

Type Parameters

T

The entity type.

Remarks

The entities are marked for deletion but not removed from the database until SaveChangesAsync(CancellationToken) is called.

SaveChangesAsync(CancellationToken)

Saves all changes made in this unit of work to the database.

public Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token to cancel the operation.

Returns

Task<int>

The number of state entries written to the database.