【问题标题】:How to call generic class from generic repository如何从通用存储库调用通用类
【发布时间】:2021-01-13 20:13:11
【问题描述】:

我想使用一个泛型类进行分页列表查询。我从这个 URL 找到了解决方案: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;
        }            
    }

}

【问题讨论】:

  • 不确定你在问什么。 CreateAsync 不是PaginatedList 中的方法吗?那么,你不能像其他方法一样从那里调用它吗?
  • 是的 CreateAsync 是一种方法,但我无法访问该方法。我试图首先在通用存储库中使用 PaginatinList,但我无法用通用构造器覆盖它的构造器。
  • 你想达到什么目的?为什么要从PaginatedList 继承RepositoryBase

标签: c# asp.net-core generics pagination


【解决方案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);
    }       
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多