Initial code commit.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Contracts.Repositories;
|
||||
using Sufi.Demo.PeopleDirectory.Domain.Common;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Contexts;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Repositories
|
||||
{
|
||||
public class AsyncRepository<T, TId>(
|
||||
ApplicationDbContext dbContext
|
||||
) : IAsyncRepository<T, TId> where T : AuditableEntity<TId>
|
||||
{
|
||||
public IQueryable<T> Entities => dbContext.Set<T>();
|
||||
|
||||
public async Task<T> AddAsync(T entity)
|
||||
{
|
||||
await dbContext.Set<T>().AddAsync(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync() => await dbContext.Set<T>().CountAsync();
|
||||
|
||||
public Task DeleteAsync(T entity)
|
||||
{
|
||||
dbContext.Set<T>().Remove(entity);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteByIdAsync(TId id)
|
||||
{
|
||||
var rowsAffected = await dbContext.Set<T>()
|
||||
.Where(e => e.Id != null && e.Id.Equals(id))
|
||||
.ExecuteDeleteAsync();
|
||||
|
||||
return rowsAffected;
|
||||
}
|
||||
|
||||
public async Task<List<T>> GetAllAsync()
|
||||
{
|
||||
return await dbContext
|
||||
.Set<T>()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<T?> GetByIdAsync(TId id)
|
||||
{
|
||||
return await dbContext.Set<T>().FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<List<T>> GetPagedResponseAsync(int pageNumber, int pageSize)
|
||||
{
|
||||
return await dbContext
|
||||
.Set<T>()
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public Task UpdateAsync(T entity)
|
||||
{
|
||||
T? exist = dbContext.Set<T>().Find(entity.Id);
|
||||
if (exist != null)
|
||||
{
|
||||
dbContext.Entry<T>(exist).CurrentValues.SetValues(entity);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Sufi.Demo.PeopleDirectory.Application.Contracts.Repositories;
|
||||
using Sufi.Demo.PeopleDirectory.Domain.Common;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Contexts;
|
||||
using System.Collections;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Repositories
|
||||
{
|
||||
public class UnitOfWork<TId>(
|
||||
ApplicationDbContext dbContext
|
||||
) : IUnitOfWork<TId>
|
||||
{
|
||||
private bool disposed;
|
||||
private Hashtable? _repositories;
|
||||
|
||||
public IAsyncRepository<TEntity, TId> Repository<TEntity>() where TEntity : AuditableEntity<TId>
|
||||
{
|
||||
_repositories ??= [];
|
||||
|
||||
var type = typeof(TEntity).Name;
|
||||
|
||||
if (!_repositories.ContainsKey(type))
|
||||
{
|
||||
var repositoryType = typeof(AsyncRepository<,>);
|
||||
|
||||
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(TEntity), typeof(TId)), dbContext);
|
||||
|
||||
_repositories.Add(type, repositoryInstance);
|
||||
}
|
||||
|
||||
return (IAsyncRepository<TEntity, TId>)_repositories[type]!;
|
||||
}
|
||||
|
||||
public async Task<int> Commit(CancellationToken cancellationToken)
|
||||
{
|
||||
return await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<int> CommitAndRemoveCache(CancellationToken cancellationToken, params string[] cacheKeys)
|
||||
{
|
||||
var result = await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task Rollback()
|
||||
{
|
||||
dbContext.ChangeTracker.Entries().ToList().ForEach(x => x.Reload());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
//dispose managed resources
|
||||
dbContext.Dispose();
|
||||
}
|
||||
}
|
||||
//dispose unmanaged resources
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user