Skip to content

Commit 4cb9a3c

Browse files
committed
Feat: Readonly UnitOfWork
1 parent 6899e79 commit 4cb9a3c

24 files changed

+342
-38
lines changed

src/CodeOfChaos.Types.DataSeeder/CodeOfChaos.Types.DataSeeder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Main package name -->
1010
<PackageId>CodeOfChaos.Types.DataSeeder</PackageId>
11-
<Version>0.22.0</Version>
11+
<Version>0.24.0</Version>
1212
<Authors>Anna Sas</Authors>
1313
<Description>A small library housing DataSeeder typings</Description>
1414
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>

src/CodeOfChaos.Types.DataSeeder/OneTimeDataSeederService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public IDataSeederService AddSeederGroup(SeederGroup group) {
139139
/// <inheritdoc />
140140
public void AddRemainderSeeders(Assembly assembly) {
141141
Type[] types = CollectTypes(assembly);
142-
var errors = new List<Exception>();
142+
List<Exception> errors = new List<Exception>();
143143

144144
foreach (Type type in types) {
145145
if (SeederTypes.Contains(type)) {
@@ -165,7 +165,7 @@ public void AddRemainderSeeders(Assembly assembly) {
165165
public void AddRemainderSeedersAsOneGroup(Assembly assembly) {
166166
Type[] types = CollectTypes(assembly);
167167
var group = new SeederGroup();
168-
var errors = new List<Exception>();
168+
List<Exception> errors = new List<Exception>();
169169

170170
foreach (Type type in types) {
171171
if (SeederTypes.Contains(type)) {

src/CodeOfChaos.Types.TypedValueStore/CodeOfChaos.Types.TypedValueStore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Main package name -->
1010
<PackageId>CodeOfChaos.Types.TypedValueStore</PackageId>
11-
<Version>0.22.0</Version>
11+
<Version>0.24.0</Version>
1212
<Authors>Anna Sas</Authors>
1313
<Description>A small library housing TypedValueStore typings</Description>
1414
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>

src/CodeOfChaos.Types.UnitOfWork.Contracts/CodeOfChaos.Types.UnitOfWork.Contracts.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Main package name -->
1111
<PackageId>CodeOfChaos.Types.UnitOfWork.Contracts</PackageId>
12-
<Version>0.22.0</Version>
12+
<Version>0.24.0</Version>
1313
<Authors>Anna Sas</Authors>
1414
<Description>A small library housing UnitOfWork typings for EntityFrameworkCore</Description>
1515
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>
@@ -30,7 +30,6 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33-
<PackageReference Include="CodeOfChaos.Extensions.DependencyInjection" Version="0.41.0" />
3433
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
3534
</ItemGroup>
3635

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace CodeOfChaos.Types.UnitOfWork;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public interface IReadonlyCapableDbContext {
10+
bool IsReadonly { get; }
11+
void SetAsReadonly();
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace CodeOfChaos.Types.UnitOfWork;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public interface IReadonlyUnitOfWork : IUnitOfWork;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace CodeOfChaos.Types.UnitOfWork;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public interface IReadonlyUnitOfWorkFactory {
10+
IReadonlyUnitOfWork Create();
11+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4-
using CodeOfChaos.Extensions.DependencyInjection;
5-
64
namespace CodeOfChaos.Types.UnitOfWork;
75
// ---------------------------------------------------------------------------------------------------------------------
86
// Code
97
// ---------------------------------------------------------------------------------------------------------------------
10-
public interface IUnitOfWorkFactory : IFactoryService<IUnitOfWork> {
8+
public interface IUnitOfWorkFactory {
9+
IUnitOfWork Create();
1110
ValueTask<IUnitOfWork> CreateWithTransactionAsync(CancellationToken ct = default);
1211
ValueTask<IUnitOfWork?> TryCreateWithTransactionAsync(CancellationToken ct = default);
1312
}

src/CodeOfChaos.Types.UnitOfWork/CodeOfChaos.Types.UnitOfWork.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<!-- Main package name -->
99
<PackageId>CodeOfChaos.Types.UnitOfWork</PackageId>
10-
<Version>0.22.0</Version>
10+
<Version>0.24.0</Version>
1111
<Authors>Anna Sas</Authors>
1212
<Description>A small library housing UnitOfWork typings for EntityFrameworkCore</Description>
1313
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>
@@ -28,7 +28,6 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="CodeOfChaos.Extensions.DependencyInjection" Version="0.41.0" />
3231
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
3332
</ItemGroup>
3433

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace CodeOfChaos.Types.UnitOfWork;
8+
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
// Code
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
public class ReadonlyUnitOfWork<TDbContext>(
13+
IDbContextFactory<TDbContext> dbContextFactory,
14+
IServiceScope serviceScope
15+
) : UnitOfWork<TDbContext>(dbContextFactory, serviceScope), IReadonlyUnitOfWork
16+
where TDbContext : DbContext, IReadonlyCapableDbContext {
17+
18+
protected override AsyncLazy<TDbContext> LazyDb { get; } = new(async ct => {
19+
TDbContext dbContext = await dbContextFactory.CreateDbContextAsync(ct);
20+
dbContext.SetAsReadonly();
21+
return dbContext;
22+
});
23+
24+
25+
// -----------------------------------------------------------------------------------------------------------------
26+
// Methods
27+
// -----------------------------------------------------------------------------------------------------------------
28+
public override ValueTask SaveChangesAsync(CancellationToken ct = default)
29+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot save changes.");
30+
31+
public override ValueTask<bool> TryCommitTransactionAsync(CancellationToken ct = default)
32+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot commit transactions.");
33+
34+
public override ValueTask<bool> TryCreateTransactionAsync(CancellationToken ct = default)
35+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create transactions.");
36+
37+
public override ValueTask<bool> TryRollbackTransactionAsync(CancellationToken ct = default)
38+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback transactions.");
39+
40+
public override ValueTask<bool> TryRollbackToSavepointAsync(Guid id, CancellationToken ct = default)
41+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot rollback to savepoints.");
42+
43+
public override ValueTask<bool> TryCreateSavepointAsync(Guid id, CancellationToken ct = default)
44+
=> throw new NotSupportedException("ReadonlyUnitOfWork cannot create savepoints.");
45+
}

0 commit comments

Comments
 (0)