C# Interview Question #155

What is the Unit of Work Pattern?

Design Patterns & Production Architecture Senior Advanced

Quick Interview Answer

Unit of Work coordinates multiple data changes so they can be committed as one logical transaction.

Detailed Explanation

Unit of Work coordinates multiple data changes so they can be committed as one logical transaction.

Entity Framework Core DbContext already behaves like a Unit of Work: it tracks entity changes and SaveChanges or SaveChangesAsync persists them as a unit.

A separate Unit of Work abstraction may be useful in certain architectures, but it should not be added automatically if it simply duplicates DbContext without providing a meaningful boundary.

Code Example

product.Price = 900;
order.Status = OrderStatus.Completed;

await _context.SaveChangesAsync(
    cancellationToken);