C# 如何从泛型存储库调用泛型类
我想使用泛型类进行分页列表查询。我从这个URL找到了一个解决方案: 如何从PaginationList调用这个静态类“CreateAncy”C# 如何从泛型存储库调用泛型类,c#,asp.net-core,generics,pagination,C#,Asp.net Core,Generics,Pagination,我想使用泛型类进行分页列表查询。我从这个URL找到了一个解决方案: 如何从PaginationList调用这个静态类“CreateAncy” public class PaginatedList<T> { public int CurrentPage { get; private set; } public int From { get; private set; } public List<T> Items { get; private set;
public class PaginatedList<T>
{
public int CurrentPage { get; private set; }
public int From { get; private set; }
public List<T> Items { get; private set; }
public int PageSize { get; private set; }
public int To { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List<T> items, int count, int currentPage, int pageSize)
{
CurrentPage = currentPage;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
TotalCount = count;
PageSize = pageSize;
From = ((currentPage - 1) * pageSize) + 1;
To = (From + pageSize) - 1;
Items = items;
}
public bool HasPreviousPage
{
get
{
return (CurrentPage > 1);
}
}
public bool HasNextPage
{
get
{
return (CurrentPage < TotalPages);
}
}
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int currentPage, int pageSize, string sortOn, string sortDirection)
{
var count = await source.CountAsync();
if (!string.IsNullOrEmpty(sortOn))
{
if (sortDirection.ToUpper() == "ASC")
source = source.OrderBy(sortOn);
else
source = source.OrderByDescending(sortOn);
}
source = source.Skip(
(currentPage - 1) * pageSize)
.Take(pageSize);
var items = await source.ToListAsync();
return new PaginatedList<T>(items, count, currentPage, pageSize);
}
}
公共类分页列表
{
public int CurrentPage{get;private set;}
来自{get;private set;}的公共int
公共列表项{get;private set;}
public int PageSize{get;private set;}
公共int到{get;private set;}
公共整数TotalCount{get;private set;}
公共整型TotalPages{get;private set;}
公共分页列表(列表项、整型计数、整型当前页面、整型页面大小)
{
CurrentPage=CurrentPage;
TotalPages=(int)数学上限(计数/(双)页面大小);
TotalCount=计数;
PageSize=页面大小;
From=((当前页面-1)*页面大小)+1;
To=(From+pageSize)-1;
项目=项目;
}
公共图书馆上一页
{
收到
{
返回(当前页面>1);
}
}
公共图书馆下一页
{
收到
{
返回(当前页面<总页面);
}
}
公共静态异步任务CreateAsync(
IQueryable源、int currentPage、int pageSize、字符串排序、字符串排序方向)
{
var count=await source.CountAsync();
如果(!string.IsNullOrEmpty(sortOn))
{
if(sortDirection.ToUpper()=“ASC”)
source=source.OrderBy(sortOn);
其他的
source=source.OrderByDescending(sortOn);
}
source=source.Skip(
(当前页面-1)*页面大小
。取(页面大小);
var items=await source.ToListAsync();
返回新的分页列表(项目、计数、当前页面、页面大小);
}
}
以及如何将该类添加到通用存储库类
public abstract class RepositoryBase<T> : PaginatedList<T>, IRepositoryBase<T> where T : class
{
protected EasyDoctorContext RepositoryContext { get; set; }
protected PaginatedList<T> PaginatedList { get; set; }
public RepositoryBase(EasyDoctorContext repositoryContext)
{
this.RepositoryContext = repositoryContext;
}
public IQueryable<T> FindAll()
{
return this.RepositoryContext.Set<T>();
}
public IQueryable<T> FindByCondition(Expression<Func<T, bool>> expression)
{
return this.RepositoryContext.Set<T>()
.Where(expression);
}
public void Create(T entity)
{
this.RepositoryContext.Set<T>().Add(entity);
}
public void Update(T entity)
{
this.RepositoryContext.Set<T>().Update(entity);
}
public void Delete(T entity)
{
this.RepositoryContext.Set<T>().Remove(entity);
}
public async Task<Boolean> SaveAsync()
{
try
{
await this.RepositoryContext.SaveChangesAsync();
return true;
}
catch(DbUpdateException e)
{
string model = typeof(T).ToString();
DBExeptionLogger.SetDbErrorLog(model, e.InnerException.Message);
return false;
}
}
}
public抽象类RepositoryBase:PaginatedList,IRepositoryBase其中T:class
{
受保护的EasyDoctorContext RepositoryContext{get;set;}
受保护的分页列表分页列表{get;set;}
公共RepositoryBase(EasyDoctorContext repositoryContext)
{
this.RepositoryContext=RepositoryContext;
}
公共可查询FindAll()
{
返回此.RepositoryContext.Set();
}
公共IQueryable FindByCondition(表达式)
{
返回此.RepositoryContext.Set()
.何处(表达方式);
}
公共无效创建(T实体)
{
this.RepositoryContext.Set().Add(实体);
}
公共无效更新(T实体)
{
this.RepositoryContext.Set().Update(实体);
}
公共作废删除(T实体)
{
this.RepositoryContext.Set().Remove(实体);
}
公共异步任务SaveAsync()
{
尝试
{
等待这个.RepositoryContext.saveChangesSync();
返回true;
}
捕获(DbUpdateException e)
{
字符串模型=typeof(T).ToString();
DBExeptionLogger.SetDbErrorLog(模型,e.InnerException.Message);
返回false;
}
}
}
试试下面的代码是否满足您的要求:
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
protected ApplicationDbContext RepositoryContext { get; set; }
public RepositoryBase(ApplicationDbContext repositoryContext)
{
this.RepositoryContext = repositoryContext;
}
public async Task<PaginatedList<T>> FindAll()
{
return await PaginatedList<T>.CreateAsync(this.RepositoryContext.Set<T>(),1,2,null, null);
}
}
公共类RepositoryBase:IRepositoryBase其中T:class
{
受保护的ApplicationDbContext RepositoryContext{get;set;}
公共RepositoryBase(ApplicationDbContext repositoryContext)
{
this.RepositoryContext=RepositoryContext;
}
公共异步任务FindAll()
{
return wait PaginatedList.CreateAsync(this.RepositoryContext.Set(),1,2,null,null);
}
}
不确定你在问什么。CreateAsync
不是paginedlist
中的一个方法吗?所以,你不能像其他方法一样从那里调用它吗?是的,CreateAync是一个方法,但我不能访问那个方法。我试图首先在泛型存储库中使用PaginationList,但我无法用泛型构造函数覆盖它的构造函数。您希望实现什么?为什么要从PaginatedList
继承RepositoryBase
?