Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit e5aaadf

Browse files
committed
Aggiunte implementazioni EFCore Transaction
1 parent 19df62b commit e5aaadf

File tree

7 files changed

+160
-2
lines changed

7 files changed

+160
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace NET6CustomLibrary.EFCoreTransaction.Core.Interfaces;
2+
3+
public interface IRepository<TEntity, TKey> where TEntity : class, ITEntity<TKey>, new()
4+
{
5+
//GET
6+
Task<List<TEntity>> GetAllTAsync();
7+
Task<TEntity> GetByIdTAsync(TKey id);
8+
Task<List<TEntity>> GetOrderByIdAscendingTAsync();
9+
Task<List<TEntity>> GetOrderByIdDescendingTAsync();
10+
Task<int> GetCountTAsync();
11+
12+
// SET
13+
Task CreateTAsync(TEntity entity);
14+
Task UpdateTAsync(TEntity entity);
15+
Task DeleteTAsync(TEntity entity);
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NET6CustomLibrary.EFCoreTransaction.Core.Interfaces;
2+
3+
public interface ITEntity<TKey>
4+
{
5+
TKey Id { get; set; }
6+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace NET6CustomLibrary.EFCoreTransaction.Core;
2+
3+
public class Repository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class, ITEntity<TKey>, new()
4+
{
5+
public DbContext DbContext { get; }
6+
7+
public Repository(DbContext dbContext) => DbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
8+
9+
public async Task CreateTAsync(TEntity entity)
10+
{
11+
using var transaction = await DbContext.Database.BeginTransactionAsync();
12+
13+
try
14+
{
15+
DbContext.Set<TEntity>().Add(entity);
16+
await DbContext.SaveChangesAsync();
17+
DbContext.Entry(entity).State = EntityState.Detached;
18+
19+
await transaction.CommitAsync();
20+
}
21+
catch (Exception)
22+
{
23+
await transaction.RollbackAsync();
24+
throw;
25+
}
26+
}
27+
28+
public async Task UpdateTAsync(TEntity entity)
29+
{
30+
using var transaction = await DbContext.Database.BeginTransactionAsync();
31+
32+
try
33+
{
34+
DbContext.Set<TEntity>().Update(entity);
35+
await DbContext.SaveChangesAsync();
36+
DbContext.Entry(entity).State = EntityState.Detached;
37+
38+
transaction.Commit();
39+
}
40+
catch (Exception)
41+
{
42+
await transaction.RollbackAsync();
43+
throw;
44+
}
45+
}
46+
47+
public async Task DeleteTAsync(TEntity entity)
48+
{
49+
using var transaction = await DbContext.Database.BeginTransactionAsync();
50+
51+
try
52+
{
53+
DbContext.Set<TEntity>().Remove(entity);
54+
await DbContext.SaveChangesAsync();
55+
56+
transaction.Commit();
57+
}
58+
catch (Exception)
59+
{
60+
await transaction.RollbackAsync();
61+
throw;
62+
}
63+
}
64+
65+
public async Task<List<TEntity>> GetAllTAsync() => await DbContext.Set<TEntity>()
66+
.AsNoTracking().ToListAsync();
67+
68+
public async Task<TEntity> GetByIdTAsync(TKey id)
69+
{
70+
var entity = await DbContext.Set<TEntity>()
71+
.FindAsync(id);
72+
73+
if (entity == null)
74+
{
75+
return null;
76+
}
77+
78+
DbContext.Entry(entity).State = EntityState.Detached;
79+
80+
return entity;
81+
}
82+
83+
public async Task<List<TEntity>> GetOrderByIdAscendingTAsync() => await DbContext.Set<TEntity>()
84+
.OrderBy(x => x.Id).AsNoTracking().ToListAsync();
85+
86+
public async Task<List<TEntity>> GetOrderByIdDescendingTAsync() => await DbContext.Set<TEntity>()
87+
.OrderByDescending(x => x.Id).AsNoTracking().ToListAsync();
88+
89+
public async Task<int> GetCountTAsync() => await DbContext.Set<TEntity>().CountAsync();
90+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NET6CustomLibrary.EFCoreTransaction.Infrastructure.Interfaces;
2+
3+
public interface ITUnitOfWork<TEntity, TKey> : IDisposable where TEntity : class, ITEntity<TKey>, new()
4+
{
5+
IRepository<TEntity, TKey> Repository { get; }
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace NET6CustomLibrary.EFCoreTransaction.Infrastructure;
2+
3+
public class TUnitOfWork<TEntity, TKey> : ITUnitOfWork<TEntity, TKey> where TEntity : class, ITEntity<TKey>, new()
4+
{
5+
public DbContext DbContext { get; }
6+
public IRepository<TEntity, TKey> Repository { get; }
7+
8+
public TUnitOfWork(DbContext dbContext, IRepository<TEntity, TKey> repository)
9+
{
10+
DbContext = dbContext;
11+
Repository = repository;
12+
}
13+
14+
public void Dispose()
15+
{
16+
Dispose(true);
17+
GC.SuppressFinalize(this);
18+
}
19+
20+
protected virtual void Dispose(bool disposing)
21+
{
22+
if (disposing)
23+
{
24+
DbContext.Dispose();
25+
}
26+
}
27+
}

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ public static IServiceCollection AddDbContextGenericsMethods<TDbContext>(this IS
110110
return services;
111111
}
112112

113+
public static IServiceCollection AddDbContextTransactionMethods<TDbContext>(this IServiceCollection services) where TDbContext : DbContext
114+
{
115+
services.AddScoped<DbContext, TDbContext>();
116+
services.AddScoped(typeof(ITUnitOfWork<,>), typeof(TUnitOfWork<,>));
117+
services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
118+
119+
return services;
120+
}
121+
113122
/// <summary>
114123
/// Extension method per aggiungere un DbContext di tipo TDbContext con il provider MySQL / MariaDB
115124
/// </summary>
@@ -296,7 +305,7 @@ public static IEndpointRouteBuilder AddDatabaseHealthChecks(this IEndpointRouteB
296305
{
297306
ResponseWriter = async (context, report) =>
298307
{
299-
var result = System.Text.Json.JsonSerializer.Serialize(new
308+
var result = JsonSerializer.Serialize(new
300309
{
301310
status = report.Status.ToString(),
302311
details = report.Entries.Select(e => new
@@ -318,7 +327,7 @@ public static IEndpointRouteBuilder AddDatabaseHealthChecks(this IEndpointRouteB
318327
{
319328
ResponseWriter = async (context, report) =>
320329
{
321-
var result = System.Text.Json.JsonSerializer.Serialize(new
330+
var result = JsonSerializer.Serialize(new
322331
{
323332
status = report.Status.ToString(),
324333
details = report.Entries.Select(e => new

src/NET6CustomLibrary/GlobalUsings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
global using NET6CustomLibrary.EFCore.Core.Interfaces;
4242
global using NET6CustomLibrary.EFCore.Infrastructure.Interfaces;
4343
global using NET6CustomLibrary.EFCore.Infrastructure.Repository;
44+
global using NET6CustomLibrary.EFCoreTransaction.Core;
45+
global using NET6CustomLibrary.EFCoreTransaction.Core.Interfaces;
46+
global using NET6CustomLibrary.EFCoreTransaction.Infrastructure;
47+
global using NET6CustomLibrary.EFCoreTransaction.Infrastructure.Interfaces;
4448
global using NET6CustomLibrary.MailKit.Options;
4549
global using NET6CustomLibrary.MailKit.Services;
4650
global using NET6CustomLibrary.MultiLanguage;

0 commit comments

Comments
 (0)