如何从通用存储库调用通用类

时间:2019-06-13 20:47:56

标签: c# asp.net-core pagination generic-repository

我想使用通用类进行分页列表查询。我从以下网址找到了一种解决方案:https://dotnetcultist.com/paging-in-entity-framework-core/?unapproved=181&moderation-hash=c64d661435dc84a39f046cc786888855#comment-181

如何从PaginationList将此静态类称为“ CreateAsync”

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 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;
        }            
    }

}

1 个答案:

答案 0 :(得分:1)

尝试下面的代码是否满足您的要求:

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);
    }       
}