54 lines
1.9 KiB
Markdown
54 lines
1.9 KiB
Markdown
# Transaction Pattern
|
|
|
|
> Part of `Mango.Nop.Data`. See `Mango.Nop.Data/README.md` for project overview.
|
|
> For repository base classes see `docs/REPOSITORIES.md`.
|
|
|
|
## MgDbContextBase
|
|
|
|
Abstract database context base. NOT an EF Core DbContext — wraps `INopDataProvider` and `IRepository<T>` nopCommerce repos.
|
|
|
|
**Constructor:**
|
|
```csharp
|
|
MgDbContextBase(IRepository<Product>, IRepository<Order>, IRepository<OrderItem>, INopDataProvider, IMgLockService, ILogger)
|
|
```
|
|
|
|
### Standard Repositories
|
|
|
|
| Property | Type |
|
|
|---|---|
|
|
| `Orders` | `IRepository<Order>` |
|
|
| `OrderItems` | `IRepository<OrderItem>` |
|
|
| `Products` | `IRepository<Product>` |
|
|
| `DataProvider` | `INopDataProvider` (LinqToDB raw queries) |
|
|
| `Logger` | Mango `ILogger` |
|
|
| `LockService` | `IMgLockService` (global `SemaphoreSlim`) |
|
|
|
|
### 4 Transaction Methods
|
|
|
|
| Method | Lock | Async |
|
|
|---|---|---|
|
|
| `Transaction(callback)` | No | No |
|
|
| `TransactionSafe(callback)` | `SemaphoreSlim` | No |
|
|
| `TransactionAsync(callback)` | No | Yes (thread pool) |
|
|
| `TransactionSafeAsync(callback)` | `SemaphoreSlim` | Yes (thread pool) |
|
|
|
|
**Callback contract:** `Func<TransactionScope, (Task<)bool(>)>` — return `true` to commit (`Complete()`), `false` to rollback.
|
|
|
|
**Isolation level:** `ReadCommitted`
|
|
|
|
**Error handling:** Catches exceptions, logs, returns `false` (unless `throwException = true`).
|
|
|
|
**TransactionSafe variants:** Use `LockService.SemaphoreSlim` for global serialization. Use for order creation, stock adjustment — any operation where concurrent modifications would corrupt data.
|
|
|
|
**Async variants:** Run on thread pool via `TaskHelper.ToThreadPoolTask()`.
|
|
|
|
## MgDalBase\<TDbContext\>
|
|
|
|
Data Access Layer orchestrator. Thin wrapper exposing:
|
|
|
|
| Property | Type | Purpose |
|
|
|---|---|---|
|
|
| `Name` | `string` | DAL instance name |
|
|
| `Context` | `TDbContext` | The DB context (typed) |
|
|
| `MutextLock` | `Mutex` | Cross-process locking |
|