Skip to content

Commit 104c13a

Browse files
committed
Add readonly transaction management to ReadonlyUnitOfWork
1 parent 0f1bc8c commit 104c13a

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/CodeOfChaos.Types.UnitOfWork.Contracts/IUnitOfWork.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public interface IUnitOfWork : IAsyncDisposable {
2323
bool TryRollbackTransaction();
2424
ValueTask<bool> TryRollbackTransactionAsync(CancellationToken ct = default);
2525

26+
bool TryCreateSavepoint(Guid id);
27+
ValueTask<bool> TryCreateSavepointAsync(Guid id, CancellationToken ct = default);
28+
2629
bool TryRollbackToSavepoint(Guid id);
2730
ValueTask<bool> TryRollbackToSavepointAsync(Guid id, CancellationToken ct = default);
2831

29-
bool TryCreateSavepoint(Guid id);
30-
ValueTask<bool> TryCreateSavepointAsync(Guid id, CancellationToken ct = default);
31-
3232
TDbContext GetDbContext<TDbContext>() where TDbContext : DbContext;
3333
ValueTask<TDbContext> GetDbContextAsync<TDbContext>(CancellationToken ct = default) where TDbContext : DbContext;
3434

src/CodeOfChaos.Types.UnitOfWork/ReadonlyUnitOfWork.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,47 @@ protected async override ValueTask<TDbContext> GetDbContextAsync(CancellationTok
2121
return dbContext;
2222
}
2323

24+
protected override TDbContext GetDbContext() {
25+
TDbContext dbContext = base.GetDbContext();
26+
dbContext.SetAsReadonly();
27+
return dbContext;
28+
}
29+
2430
// -----------------------------------------------------------------------------------------------------------------
2531
// Methods
2632
// -----------------------------------------------------------------------------------------------------------------
33+
public override void SaveChanges()
34+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot save changes.");
35+
2736
public override ValueTask SaveChangesAsync(CancellationToken ct = default)
2837
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot save changes.");
2938

39+
public override bool TryCommitTransaction()
40+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot commit transactions.");
41+
3042
public override ValueTask<bool> TryCommitTransactionAsync(CancellationToken ct = default)
3143
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot commit transactions.");
32-
44+
45+
public override bool TryCreateTransaction()
46+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create transactions.");
47+
3348
public override ValueTask<bool> TryCreateTransactionAsync(CancellationToken ct = default)
3449
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create transactions.");
50+
51+
public override bool TryRollbackTransaction()
52+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback transactions.");
3553

3654
public override ValueTask<bool> TryRollbackTransactionAsync(CancellationToken ct = default)
3755
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback transactions.");
56+
57+
public override bool TryRollbackToSavepoint(Guid id)
58+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback to savepoints.");
3859

3960
public override ValueTask<bool> TryRollbackToSavepointAsync(Guid id, CancellationToken ct = default)
4061
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback to savepoints.");
62+
63+
public override bool TryCreateSavepoint(Guid id)
64+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create savepoints.");
4165

4266
public override ValueTask<bool> TryCreateSavepointAsync(Guid id, CancellationToken ct = default)
4367
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create savepoints.");

src/CodeOfChaos.Types.UnitOfWork/UnitOfWork.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected virtual async ValueTask<TDbContext> GetDbContextAsync(CancellationToke
4141

4242
protected virtual TDbContext GetDbContext() => _lazyDb.Value;
4343

44-
public bool TryCreateTransaction() {
44+
public virtual bool TryCreateTransaction() {
4545
if (_transaction != null) return false;
4646

4747
var dbContext = GetDbContext<TDbContext>();
@@ -75,7 +75,7 @@ public virtual async ValueTask<bool> TryCreateTransactionAsync(CancellationToken
7575
}
7676
}
7777

78-
public void SaveChanges() {
78+
public virtual void SaveChanges() {
7979
DbContext dbContext = GetDbContext<TDbContext>();
8080
dbContext.SaveChanges();
8181
}
@@ -85,7 +85,7 @@ public virtual async ValueTask SaveChangesAsync(CancellationToken ct = default)
8585
await dbContext.SaveChangesAsync(ct);
8686
}
8787

88-
public bool TryCommitTransaction() {
88+
public virtual bool TryCommitTransaction() {
8989
if (_transaction == null) return false;
9090

9191
lock (_transactionLock) {
@@ -111,7 +111,7 @@ public virtual async ValueTask<bool> TryCommitTransactionAsync(CancellationToken
111111
}
112112
}
113113

114-
public bool TryRollbackTransaction() {
114+
public virtual bool TryRollbackTransaction() {
115115
if (_transaction == null) return false;
116116

117117
_transaction.Rollback();
@@ -131,7 +131,7 @@ public virtual async ValueTask<bool> TryRollbackTransactionAsync(CancellationTok
131131
return true;
132132
}
133133

134-
public bool TryRollbackToSavepoint(Guid id) {
134+
public virtual bool TryRollbackToSavepoint(Guid id) {
135135
if (_transaction == null) return false;
136136
if (!_transaction.SupportsSavepoints) return false;
137137

@@ -148,7 +148,7 @@ public virtual async ValueTask<bool> TryRollbackToSavepointAsync(Guid id, Cancel
148148
return true;
149149
}
150150

151-
public bool TryCreateSavepoint(Guid id) {
151+
public virtual bool TryCreateSavepoint(Guid id) {
152152
if (_transaction == null) return false;
153153
if (!_transaction.SupportsSavepoints) return false;
154154

@@ -165,7 +165,7 @@ public virtual async ValueTask<bool> TryCreateSavepointAsync(Guid id, Cancellati
165165
return true;
166166
}
167167

168-
public T GetDbContext<T>() where T : DbContext {
168+
public virtual T GetDbContext<T>() where T : DbContext {
169169
if (typeof(T) != typeof(TDbContext)) throw new NotSupportedException($"DbContext type '{typeof(T)}' is not supported by this UnitOfWork.");
170170

171171
TDbContext dbContext = GetDbContext();
@@ -181,7 +181,7 @@ public virtual async ValueTask<T> GetDbContextAsync<T>(CancellationToken ct = de
181181
return Unsafe.As<TDbContext, T>(ref dbContext);
182182
}
183183

184-
public TRepo GetRepository<TRepo>() where TRepo : class, IUnitOfWorkRepository {
184+
public virtual TRepo GetRepository<TRepo>() where TRepo : class, IUnitOfWorkRepository {
185185
if (AttachedRepositories.TryGetValue(typeof(TRepo), out IUnitOfWorkRepository? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
186186

187187
var repo = CreateAndAttachRepository<TRepo>();

0 commit comments

Comments
 (0)