Files
CleanArchitecture/CleanArchitecture/CleanArchitecture.Data/Repositories/RepositoryBase.cs
Alejandro Sarmiento 81a3e91d6e Video 76
Se hizo toda la parte del Unit Of Work y estoy a punto de terminar la de tests unitarios
2024-02-28 22:40:34 +01:00

101 lines
3.7 KiB
C#

using CleanArchitecture.Application.Contracts.Persistence;
using CleanArchitecture.Domain.Common;
using CleanArchitecture.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
namespace CleanArchitecture.Infrastructure.Repositories
{
public class RepositoryBase<T> : IAsyncRepository<T> where T : BaseDomainModel
{
protected readonly StreamerDbContext context;
public RepositoryBase(StreamerDbContext _context)
{
context = _context;
}
public async Task<IReadOnlyList<T>> GetAllAsync()
{
return await context.Set<T>().ToListAsync();
}
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
{
return await context.Set<T>().Where(predicate).ToListAsync();
}
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T,bool>>? predicate = null,
Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null,
string includeString = null,
bool disableTracking = true)
{
IQueryable<T> query = context.Set<T>();
if (disableTracking) query = query.AsNoTracking();
if (!string.IsNullOrWhiteSpace(includeString)) query = query.Include(includeString);
if(predicate!=null) query = query.Where(predicate);
if (orderBy != null)
return await orderBy(query).ToListAsync();
return await query.ToListAsync();
}
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>>? predicate = null,
Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null,
List<Expression<Func<T, object>>>? includes = null,
bool disableTracking = true)
{
IQueryable<T> query = context.Set<T>();
if (disableTracking) query = query.AsNoTracking();
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include));
if(predicate != null) query = query.Where(predicate);
if (orderBy!=null)
return await orderBy(query).ToListAsync();
return await query.ToListAsync();
}
public virtual async Task<T> GetByIdAsync(Guid id)
{
return await context.Set<T>().FindAsync(id);
}
public async Task<T> AddAsync(T entity)
{
context.Set<T>().Add(entity);
await context.SaveChangesAsync();
return entity;
}
public async Task<T> UpdateAsync(T entity)
{
context.Set<T>().Attach(entity);
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
public async Task DeleteAsync(T entity)
{
context.Set<T>().Remove(entity);
await context.SaveChangesAsync();
}
public void AddEntity(T entity)
{
context.Set<T>().Add(entity);
}
public void UpdateEntity(T entity)
{
context.Set<T>().Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
public void DeleteEntity(T entity)
{
context.Set<T>().Remove(entity);
}
}
}