Files
juipnet/Infrastructure/Hncore.Infrastructure/EF/QueryBase.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2024-04-10 13:55:27 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
namespace Hncore.Infrastructure.EF
{
public class QueryBase<TEntity, TId> : IQuery<TEntity, TId> where TEntity : class, IEntity<TId>
{
protected DbContext Dbcontext;
public QueryBase(IQueryDbContext dbContext)
{
Dbcontext = dbContext.DbContext;
}
public TEntity GetOne(Expression<Func<TEntity, bool>> condition)
{
return Dbcontext.GetOne<TEntity, TId>(condition);
}
public Task<TEntity> GetOneAsync(Expression<Func<TEntity, bool>> condition)
{
return Dbcontext.GetOneAsync<TEntity, TId>(condition);
}
public PageData<TEntity> GetList(Expression<Func<TEntity, bool>> condition, int pagesize, int pageindex,
bool istotal)
{
return Dbcontext.GetList<TEntity, TId>(condition, pagesize, pageindex, istotal);
}
public Task<PageData<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> condition, int pagesize,
int pageindex, bool istotal)
{
return Dbcontext.GetListAsync<TEntity, TId>(condition, pagesize, pageindex, istotal);
}
public List<TEntity> GetList(Expression<Func<TEntity, bool>> condition)
{
return Dbcontext.GetList<TEntity, TId>(condition);
}
public Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> condition)
{
return Dbcontext.GetListAsync<TEntity, TId>(condition);
}
public List<TEntity> TopN(Expression<Func<TEntity, bool>> condition, int topN)
{
return Dbcontext.TopN<TEntity, TId>(condition, topN);
}
public Task<List<TEntity>> TopNAsync(Expression<Func<TEntity, bool>> condition, int topN)
{
return Dbcontext.TopNAsync<TEntity, TId>(condition, topN);
}
public IQueryable<TEntity> GetListQueryable(Expression<Func<TEntity, bool>> exp)
{
return Dbcontext.GetListQueryable<TEntity, TId>(exp);
}
public bool Exists(Expression<Func<TEntity, bool>> exp)
{
return Dbcontext.Exists<TEntity, TId>(exp);
}
public Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> condition)
{
return Dbcontext.ExistsAsync<TEntity, TId>(condition);
}
public IQueryable<TEntity> GetQueryable()
{
return Dbcontext.Set<TEntity>().AsNoTracking();
}
public DbContext DbContext()
{
return Dbcontext;
}
}
}