25 lines
1.0 KiB
C#

using System.Linq.Expressions;
using ApplicationHub.Domain.Contracts;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace ApplicationHub.Data.EF.Utils;
public class BaseRepository<TModel, TEntity>(DbSet<TEntity> dbSet, IMapper mapper) : IRepository<TModel> where TEntity : class
{
public IEnumerable<TModel> Find(Expression<Func<TModel, bool>>? where = null, int? limit = null, int? offset = null, List<KeyValuePair<Expression<Func<TModel, bool>>, OrderDirection>>? order = null)
{
var resolver = new QueryResolver<TModel, TEntity>();
var dbSetWithIncludes = Inculdes();
var result = dbSetWithIncludes == null
? resolver.Find(dbSet, where, limit, offset, order).ToList()
: resolver.Find(dbSetWithIncludes, where, limit, offset, order).ToList();
return mapper.Map<List<TModel>>(result);
}
protected virtual IIncludableQueryable<TEntity, object?>? Inculdes()
{
return null;
}
}