chore: project clean
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace GamificationService.Database.Repositories;
|
||||
|
||||
@@ -16,8 +15,8 @@ public class GenericRepository<TEntity> where TEntity : class
|
||||
}
|
||||
|
||||
public virtual IQueryable<TEntity> Get(
|
||||
Expression<Func<TEntity, bool>> filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
|
||||
Expression<Func<TEntity, bool>>? filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
|
||||
string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
@@ -4,76 +4,69 @@ namespace GamificationService.Database.Repositories;
|
||||
|
||||
public class UnitOfWork : IDisposable
|
||||
{
|
||||
#region fields
|
||||
|
||||
private ApplicationContext _context;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private IDbContextTransaction _transaction;
|
||||
|
||||
public UnitOfWork(ApplicationContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
#region fields
|
||||
|
||||
protected ApplicationContext _context;
|
||||
private IDbContextTransaction? _transaction;
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
public UnitOfWork(ApplicationContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public bool Save()
|
||||
{
|
||||
return _context.SaveChanges() > 0;
|
||||
}
|
||||
public bool Save()
|
||||
{
|
||||
return _context.SaveChanges() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SaveAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
public async Task<bool> SaveAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
|
||||
private bool disposed = false;
|
||||
public async Task BeginTransactionAsync()
|
||||
{
|
||||
if (_transaction is not null)
|
||||
throw new InvalidOperationException("A transaction has already been started.");
|
||||
_transaction = await _context.Database.BeginTransactionAsync();
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
public async Task CommitAsync()
|
||||
{
|
||||
if (_transaction is null)
|
||||
throw new InvalidOperationException("A transaction has not been started.");
|
||||
|
||||
try
|
||||
{
|
||||
if (!this.disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
}
|
||||
this.disposed = true;
|
||||
await _transaction.CommitAsync();
|
||||
}
|
||||
public async Task BeginTransactionAsync()
|
||||
finally
|
||||
{
|
||||
if (_transaction is not null)
|
||||
throw new InvalidOperationException("A transaction has already been started.");
|
||||
_transaction = await _context.Database.BeginTransactionAsync();
|
||||
await _transaction.DisposeAsync();
|
||||
_transaction = null;
|
||||
}
|
||||
public async Task CommitAsync()
|
||||
}
|
||||
|
||||
private bool disposed = false;
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (_transaction is null)
|
||||
throw new InvalidOperationException("A transaction has not been started.");
|
||||
|
||||
try
|
||||
{
|
||||
await _transaction.CommitAsync();
|
||||
_transaction.Dispose();
|
||||
_transaction = null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (_transaction is not null)
|
||||
await _transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
if (disposing)
|
||||
{
|
||||
_transaction?.Dispose(); // Dispose transaction if it exists
|
||||
_context.Dispose();
|
||||
}
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user